prb 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 97fa1172495612ccf5109e932970c74bdaf02690e9399f7da5a451f41598cfdf
4
+ data.tar.gz: 1df513efca5f84ac516121e0f720c1522474eb97581a390777ad37786e6caf98
5
+ SHA512:
6
+ metadata.gz: 4c3f24a6477fcca54805d9ed7a799a45f97e2afdf1007882131045f796b4ae9373b9af293fc0a8831bb2623e8398543150d6c7f32d9f03e837bd6787df19c96f
7
+ data.tar.gz: a0995f9549574d236c49dfd5f99f0c927c61de46c0841d4e34dbd2c9b764dddc5311f18d391a50def038ebec010aefa596a0cbb312cd7b7883fb3ea1e29b5f2e
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.0
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in potime.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ prb (0.1.0)
5
+ optimist
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ optimist (3.0.0)
12
+ rake (10.5.0)
13
+ rspec (3.8.0)
14
+ rspec-core (~> 3.8.0)
15
+ rspec-expectations (~> 3.8.0)
16
+ rspec-mocks (~> 3.8.0)
17
+ rspec-core (3.8.0)
18
+ rspec-support (~> 3.8.0)
19
+ rspec-expectations (3.8.2)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-mocks (3.8.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-support (3.8.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 2.0)
32
+ prb!
33
+ rake (~> 10.0)
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Richard Ramsden
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Prb
2
+
3
+ `prb` is a light-weight timer service written in Ruby for the pomodoro
4
+ technique.
5
+
6
+ ## Installation
7
+
8
+ Install the gem
9
+
10
+ $ gem install prb
11
+
12
+ ## Usage
13
+
14
+ ```
15
+ prb v0.1.0 - Pomodoro timer service
16
+
17
+ Usage:
18
+ prb [COMMAND] [SUB_COMMAND]
19
+
20
+ Options:
21
+ -d, --daemonize Start the service in the background
22
+
23
+ Commands:
24
+ start Start pomodoro service
25
+ stop Stop pomodoro service
26
+ status Print status of pomodoro service
27
+ skip Skip the current timer
28
+ reset Reset the current timer
29
+ pause Pause the current timer
30
+ -v, --version Print version and exit
31
+ -h, --help Show this message
32
+ ```
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rramsden/potime.
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "prb"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/exe/prb ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "optimist"
4
+ require "bundler/setup"
5
+ require "prb"
6
+
7
+ COMMAND_MAP = {
8
+ 'start' => 'Start pomodoro service',
9
+ 'stop' => 'Stop pomodoro service',
10
+ 'status' => 'Print status of pomodoro service',
11
+ 'skip' => 'Skip the current timer',
12
+ 'reset' => 'Reset the current timer',
13
+ 'pause' => 'Pause the current timer'
14
+ }
15
+
16
+ opts = Optimist::options do
17
+ version "prb v#{Prb::VERSION}"
18
+ banner "prb v#{Prb::VERSION} - Pomodoro timer service\n \n"
19
+ banner "Usage:"
20
+ banner " prb [COMMAND] [SUB_COMMAND]\n \n"
21
+ banner "Options:"
22
+ opt :daemonize, 'Start the service in the background', short: '-d'
23
+ banner "\nCommands:"
24
+ COMMAND_MAP.each { |cmd, desc| banner format(" %-10s %s", cmd, desc) }
25
+ end
26
+
27
+ commands = COMMAND_MAP.keys & ARGV
28
+ cmd = commands.first
29
+
30
+ if COMMAND_MAP.keys.include?(cmd)
31
+ Prb::CommandRunner.new(opts).send(cmd)
32
+ else
33
+ Optimist.educate
34
+ end
data/lib/prb.rb ADDED
@@ -0,0 +1,8 @@
1
+ require_relative "prb/version"
2
+ require_relative "prb/timer"
3
+ require_relative "prb/timer_control"
4
+ require_relative "prb/server"
5
+ require_relative "prb/command_runner"
6
+
7
+ module Prb
8
+ end
@@ -0,0 +1,57 @@
1
+ module Prb
2
+ class CommandRunner
3
+ def initialize(opts)
4
+ @opts = opts
5
+ end
6
+
7
+ def start
8
+ if Prb::Server.running?
9
+ puts "A prb server is already running."
10
+ exit(1)
11
+ end
12
+
13
+ Process.daemon() if @opts[:daemonize]
14
+
15
+ Prb::Server.new.start
16
+ end
17
+
18
+ def status
19
+ response = send_command('STATUS')
20
+ puts response
21
+ end
22
+
23
+ def skip
24
+ send_command('SKIP')
25
+ end
26
+
27
+ def pause
28
+ send_command('PAUSE')
29
+ end
30
+
31
+ def reset
32
+ send_command('RESET')
33
+ end
34
+
35
+ def stop
36
+ send_command('QUIT')
37
+ end
38
+
39
+ private
40
+
41
+ def send_command(cmd)
42
+ Timeout::timeout(1) do
43
+ begin
44
+ s = TCPSocket.new('127.0.0.1', PRB_PORT)
45
+ s.puts(cmd)
46
+ response = s.gets.chomp
47
+ s.close
48
+ return response
49
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
50
+ puts "A prb server is not running"
51
+ exit(1)
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ end
data/lib/prb/server.rb ADDED
@@ -0,0 +1,76 @@
1
+ require 'socket'
2
+ require 'timeout'
3
+
4
+ module Prb
5
+ PRB_PORT = 3838
6
+
7
+ class Server
8
+ def initialize
9
+ @controller = TimerControl.new
10
+ end
11
+
12
+ def start
13
+ @controller.start
14
+
15
+ server = TCPServer.open('127.0.0.1', PRB_PORT)
16
+ puts "Server listening on #{PRB_PORT}"
17
+
18
+ while true
19
+ Thread.start(server.accept) do |socket|
20
+ while cmd = socket.gets.chomp
21
+ puts "RECV: #{cmd}"
22
+
23
+ output = "OK"
24
+ case cmd
25
+ when 'STATUS'
26
+ output = status_line
27
+ when 'SKIP'
28
+ @controller.toggle
29
+ when 'RESET'
30
+ @controller.reset
31
+ when 'PAUSE'
32
+ @controller.pause
33
+ when 'QUIT'
34
+ socket.puts "OK"
35
+ exit(0)
36
+ else
37
+ puts "ERR: #{cmd} not a command"
38
+ output = "ERR: #{cmd} not a command"
39
+ end
40
+
41
+ socket.puts output
42
+ end
43
+
44
+ socket.close
45
+ end
46
+ end
47
+
48
+ server.close
49
+ end
50
+
51
+ def self.running?
52
+ Timeout::timeout(1) do
53
+ begin
54
+ s = TCPSocket.new('127.0.0.1', PRB_PORT)
55
+ s.close
56
+ return true
57
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
58
+ return false
59
+ end
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def status_line
66
+ status = ""
67
+ status << @controller.timer.render
68
+ status << ","
69
+ status << "WORKING" if @controller.is_working?
70
+ status << "BREAK" if !@controller.is_working?
71
+ status << ",PAUSED" if @controller.paused?
72
+ status << ",RUNNING" if !@controller.paused?
73
+ status
74
+ end
75
+ end
76
+ end
data/lib/prb/timer.rb ADDED
@@ -0,0 +1,41 @@
1
+ module Prb
2
+ class Timer
3
+ def initialize(minutes)
4
+ @paused = false
5
+ set_timer(minutes)
6
+ end
7
+
8
+ def set_timer(minutes)
9
+ @_seconds = minutes * 60
10
+ @seconds = @_seconds
11
+ end
12
+
13
+ def tick
14
+ sleep 1
15
+ @seconds -= 1 unless @paused or finished?
16
+ end
17
+
18
+ def finished?
19
+ @seconds == 0
20
+ end
21
+
22
+ def reset
23
+ @seconds = @_seconds
24
+ end
25
+
26
+ def pause
27
+ @paused = !@paused
28
+ end
29
+
30
+ def paused?
31
+ @paused
32
+ end
33
+
34
+ def render
35
+ min = (@seconds / 60) % 60
36
+ sec = (@seconds % 60)
37
+
38
+ format("%02d:%02d", min, sec)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,45 @@
1
+ module Prb
2
+ class TimerControl
3
+ WORK_MINUTES = 25
4
+ BREAK_MINUTES = 5
5
+
6
+ attr_reader :timer
7
+
8
+ def initialize
9
+ @timer = Timer.new(WORK_MINUTES)
10
+ @is_working = true
11
+ end
12
+
13
+ def start
14
+ Thread.new do
15
+ loop do
16
+ @timer.tick
17
+ toggle if @timer.finished?
18
+ end
19
+ end
20
+ end
21
+
22
+ def reset
23
+ @timer.reset
24
+ end
25
+
26
+ def pause
27
+ @timer.pause
28
+ end
29
+
30
+ def paused?
31
+ @timer.paused?
32
+ end
33
+
34
+ def is_working?
35
+ @is_working
36
+ end
37
+
38
+ def toggle
39
+ @is_working = !@is_working
40
+ @timer.set_timer(@is_working ?
41
+ WORK_MINUTES : BREAK_MINUTES)
42
+ @timer.pause unless @timer.paused?
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Prb
2
+ VERSION = "0.1.0"
3
+ end
data/prb.gemspec ADDED
@@ -0,0 +1,44 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "prb/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "prb"
8
+ spec.version = Prb::VERSION
9
+ spec.authors = ["Richard Ramsden"]
10
+ spec.email = ["richard@rramsden.ca"]
11
+
12
+ spec.summary = "Pomodoro timer CLI written in Ruby"
13
+ spec.description = "Small CLI client for rendering a pomodoro timer"
14
+ spec.homepage = "https://github.com/rramsden/prb"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = spec.homepage
24
+ spec.metadata["changelog_uri"] = spec.homepage
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_dependency "optimist"
40
+
41
+ spec.add_development_dependency "bundler", "~> 2.0"
42
+ spec.add_development_dependency "rake", "~> 10.0"
43
+ spec.add_development_dependency "rspec", "~> 3.0"
44
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard Ramsden
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: optimist
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Small CLI client for rendering a pomodoro timer
70
+ email:
71
+ - richard@rramsden.ca
72
+ executables:
73
+ - prb
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - exe/prb
87
+ - lib/prb.rb
88
+ - lib/prb/command_runner.rb
89
+ - lib/prb/server.rb
90
+ - lib/prb/timer.rb
91
+ - lib/prb/timer_control.rb
92
+ - lib/prb/version.rb
93
+ - prb.gemspec
94
+ homepage: https://github.com/rramsden/prb
95
+ licenses:
96
+ - MIT
97
+ metadata:
98
+ allowed_push_host: https://rubygems.org
99
+ homepage_uri: https://github.com/rramsden/prb
100
+ source_code_uri: https://github.com/rramsden/prb
101
+ changelog_uri: https://github.com/rramsden/prb
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubygems_version: 3.0.3
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Pomodoro timer CLI written in Ruby
121
+ test_files: []