rubiks-cli 1.2.0 → 1.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b31d0f5c4d03e00788128265b7dbb190583673563adce67252c202f9e114f0a5
4
- data.tar.gz: 046d15a7724fa53739edf3c5dde5b226c925ab8ef419f50238b4b85515d732ff
3
+ metadata.gz: b01173da4ddca951dac1a67a9d4eb4a872162ffebd3751fc89d30b5512128eb0
4
+ data.tar.gz: fd61a5af3f725ad9d042514ba252ab206ad3ffce7758acc709c58f68ace07a69
5
5
  SHA512:
6
- metadata.gz: 47d2f102cd05f55b0fc4cd36f47c1e3830c9541ec79e57b10121690a96f969b2a3fd1a50cd3975d2e2e5fe20b75dbce05989bb78097ff26b071f2f66a37a8f07
7
- data.tar.gz: dc0b0fb6338108e15a47cb28752db0783ddd873ab71186c2ab8fdb33d41961780bc730217a28013207f68dc9232d8d1777a2393651a001402c8cf1aeb0e3d4b9
6
+ metadata.gz: 288fa531855f4e33f79bf991893019614068ac46f314a250f812124927fe1752c4f4ba0544f28061014e53632c9541970c49a3007e9ec58e6d32c048ebed483e
7
+ data.tar.gz: 79dfc148a7ffbe9292942346f51585f6560f45c1f2b7ed1c7dc766c3e828b9e2d043264af70cb61235b6e4a081fc54eb8b10835217743d18684589704c78f64c
data/bin/rubiks CHANGED
@@ -2,48 +2,87 @@
2
2
  require 'optparse'
3
3
  require 'rubiks_cli'
4
4
 
5
- options = {
6
- scramble: false,
7
- time: false,
8
- loop: true,
9
- clear: false,
10
- wipe: false,
11
- }
5
+ options = {}
6
+
12
7
  OptionParser.new do |opts|
13
8
  opts.banner = "Usage: rubiks [options]"
14
9
 
15
- opts.on("-s", "--scramble", "Generate scramble") do
16
- options[:scramble] = true
10
+ opts.on("-c", "--clear", "Clear the screen before starting") do
11
+ options[:clear] = true
17
12
  end
18
13
 
19
- opts.on("-t", "--time", "Start timer") do
20
- options[:time] = true
14
+ opts.on("-i", "--inspection", "Include inspection time") do
15
+ options[:inspection] = true
21
16
  end
22
17
 
23
- opts.on("-l", "--loop", "Loop forever (default)") do
18
+ opts.on("-l", "--loop", "Loop forever") do
24
19
  options[:loop] = true
25
20
  end
26
21
 
27
- opts.on("-w", "--wipe", "Wipe output each loop iteration") do
28
- options[:wipe] = true
22
+ opts.on("-s", "--scramble", "Generate scramble") do
23
+ options[:scramble] = true
29
24
  end
30
25
 
31
- opts.on("-c", "--clear", "Clear the screen before starting (default for --loop)") do
32
- options[:clear] = true
26
+ opts.on("-t", "--time", "Start timer") do
27
+ options[:time] = true
33
28
  end
29
+
34
30
  end.parse!
35
31
 
36
- if options[:clear]
37
- RubiksCli::Clear.screen
32
+ if options.values.none?
33
+ options[:scramble] = true
34
+ options[:time] = true
38
35
  end
39
36
 
40
- if options[:scramble] && options[:time]
41
- RubiksCli::Solve.solve
42
- elsif options[:scramble]
43
- puts RubiksCli::Scrambler.get_scramble
44
- elsif options[:time]
45
- RubiksCli::Timer.time
37
+ RubiksCli::Clear.screen if options[:clear]
38
+
39
+ output_scramble = lambda { puts RubiksCli::Scrambler.get_scramble }
40
+ start_timer = lambda { RubiksCli::Timer.start(options[:inspection]) }
41
+ start_timer_then_output_scramble = lambda {
42
+ RubiksCli::Timer.start(options[:inspection])
43
+ puts "\n#{RubiksCli::Scrambler.get_scramble}"
44
+ }
45
+
46
+ if options[:loop]
47
+
48
+ if options[:time] && options[:scramble]
49
+ RubiksCli::Clear.screen
50
+ RubiksCli::Loop.build(
51
+ RubiksCli::Loop.create_action('', start_timer_then_output_scramble, "start solve"),
52
+ [ RubiksCli::Loop.create_action('n', output_scramble, "generate new scramble"), ]
53
+ ).start
54
+
55
+ elsif options[:scramble]
56
+ RubiksCli::Clear.screen
57
+ RubiksCli::Loop.build(
58
+ RubiksCli::Loop.create_action('', output_scramble, "generate a scramble"),
59
+ [ RubiksCli::Loop.create_action('t', start_timer_then_output_scramble, "start timer"), ]
60
+ ).start
61
+
62
+ elsif options[:time]
63
+ RubiksCli::Clear.screen
64
+ RubiksCli::Loop.build(
65
+ RubiksCli::Loop.create_action('', start_timer, "start timer"),
66
+ [ RubiksCli::Loop.create_action('n', output_scramble, "generate a scramble"), ]
67
+ ).start
68
+
69
+ else
70
+ puts "Invalid options for loop mode: must specify --time and/or --scramble"
71
+ end
46
72
  else
47
- RubiksCli::Clear.screen
48
- RubiksCli::Solve.loop(options[:wipe])
73
+ if options[:time] && options[:scramble]
74
+ output_scramble.call
75
+ gets
76
+ RubiksCli::Clear.line_above
77
+ start_timer.call
78
+
79
+ elsif options[:scramble]
80
+ output_scramble.call
81
+
82
+ elsif options[:time]
83
+ start_timer.call
84
+
85
+ else
86
+ end
87
+
49
88
  end
@@ -1,11 +1,15 @@
1
1
  module RubiksCli
2
- class Clear
2
+ module Clear
3
3
  def self.screen
4
4
  system("clear") || system("cls")
5
5
  end
6
6
 
7
+ def self.current_line
8
+ print "\r\e[K"
9
+ end
10
+
7
11
  def self.line_above
8
- print "\e[1A\e[K\n"
12
+ print "\e[1A\r\e[K\n"
9
13
  end
10
14
  end
11
15
  end
@@ -0,0 +1,19 @@
1
+ require_relative '../clear'
2
+
3
+ module RubiksCli
4
+ module Loop
5
+ class Action < Data.define(:command, :function, :help)
6
+ private_class_method :new
7
+ end
8
+
9
+ def self.create_action(command, function, description)
10
+ help = "Press '#{command}' to #{description}"
11
+ Action.send(:new, command, function, help)
12
+ end
13
+
14
+ DEFAULT_ACTIONS = [
15
+ create_action('c', RubiksCli::Clear.method(:screen), 'clear the screen'),
16
+ create_action('q', lambda { raise SystemExit }, 'quit'),
17
+ ]
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'action'
2
+ require_relative 'engine'
3
+
4
+ module RubiksCli
5
+ module Loop
6
+ private
7
+ def self.build_help_message(actions)
8
+ actions.map { |action| action.help }.join("\n")
9
+ end
10
+
11
+ def self.build(on_enter, actions)
12
+ help_action = create_action('h', -> { puts build_help_message(actions) }, "show this message")
13
+ Loop::Engine.send(:new, on_enter, actions += DEFAULT_ACTIONS << help_action)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ require_relative '../clear'
2
+
3
+ module RubiksCli
4
+ module Loop
5
+ class Engine
6
+ private_class_method :new
7
+
8
+ def initialize(action_on_enter, other_actions)
9
+ commands = other_actions.map { |action| action.command }
10
+ if commands.uniq.size != commands.size
11
+ raise ArgumentError, "Duplicate command"
12
+ end
13
+
14
+ @actions = Hash.new(action_on_enter.function)
15
+ other_actions.each { |action| @actions[action.command] = action.function }
16
+ end
17
+
18
+ def start
19
+ @actions['h'].call
20
+ loop do
21
+ input = gets.chomp.downcase
22
+ Clear.line_above
23
+ @actions[input].call
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ if __FILE__ == $0
31
+ require_relative 'builder'
32
+
33
+ on_enter = lambda { puts "You pressed Enter!" }
34
+ actions = [
35
+ RubiksCli::Loop.build_action('x', -> { puts "Action X executed" }, "execute action X"),
36
+ RubiksCli::Loop.build_action('y', -> { puts "Action Y executed" }, "execute action Y"),
37
+ ]
38
+ loop_instance = RubiksCli::Loop.build_loop(on_enter, actions)
39
+ loop_instance.start
40
+ end
@@ -2,8 +2,14 @@ require 'io/console'
2
2
  require_relative 'clear'
3
3
 
4
4
  module RubiksCli
5
- class Timer
6
- def self.show
5
+ module Timer
6
+ INSPECTION_TIME = 15
7
+
8
+ def self.start(inspection)
9
+ if inspection
10
+ self.inspection
11
+ end
12
+ Clear.current_line
7
13
  puts "\r#{self.time.round(3)}"
8
14
  end
9
15
 
@@ -12,13 +18,35 @@ module RubiksCli
12
18
 
13
19
  STDIN.raw do
14
20
  loop do
15
- print "\r#{(Time.now - start).round(1)}"
16
- break if STDIN.wait_readable(0.01)
21
+ elapsed = Time.now - start
22
+ print "\r#{elapsed.round(1)}"
23
+ return elapsed if STDIN.wait_readable(0.1)
17
24
  end
18
- STDIN.read_nonblock(1) rescue nil
25
+ ensure
26
+ STDIN.getch rescue nil
19
27
  end
28
+ end
29
+
30
+ def self.inspection
31
+ STDIN.raw do
32
+ (0...INSPECTION_TIME).each do |i|
33
+ print "\rInspection: #{i}"
34
+ return if STDIN.wait_readable(1)
35
+ end
20
36
 
21
- return Time.now - start
37
+ print "\r+2"
38
+ 2.times do
39
+ return if STDIN.wait_readable(1)
40
+ end
41
+
42
+ print "\rDNF"
43
+ ensure
44
+ STDIN.getch rescue nil
45
+ end
22
46
  end
23
47
  end
24
48
  end
49
+
50
+ if __FILE__ == $0
51
+ RubiksCli::Timer.start(true)
52
+ end
@@ -1,3 +1,3 @@
1
1
  module RubiksCli
2
- VERSION = "1.2.0"
2
+ VERSION = "1.4.0"
3
3
  end
data/lib/rubiks_cli.rb CHANGED
@@ -2,8 +2,9 @@ require_relative "rubiks_cli/version"
2
2
  require_relative "rubiks_cli/scrambler"
3
3
  require_relative "rubiks_cli/timer"
4
4
  require_relative "rubiks_cli/clear"
5
- require_relative "rubiks_cli/help"
6
- require_relative "rubiks_cli/solve"
5
+ require_relative "rubiks_cli/loop/builder"
6
+ require_relative "rubiks_cli/loop/action"
7
+ require_relative "rubiks_cli/loop/engine"
7
8
 
8
9
  module RubiksCli
9
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubiks-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noah Scott
@@ -23,9 +23,10 @@ files:
23
23
  - bin/rubiks
24
24
  - lib/rubiks_cli.rb
25
25
  - lib/rubiks_cli/clear.rb
26
- - lib/rubiks_cli/help.rb
26
+ - lib/rubiks_cli/loop/action.rb
27
+ - lib/rubiks_cli/loop/builder.rb
28
+ - lib/rubiks_cli/loop/engine.rb
27
29
  - lib/rubiks_cli/scrambler.rb
28
- - lib/rubiks_cli/solve.rb
29
30
  - lib/rubiks_cli/timer.rb
30
31
  - lib/rubiks_cli/version.rb
31
32
  - rubiks-cli.gemspec
@@ -1,9 +0,0 @@
1
- module RubiksCli
2
- class Help
3
- LOOP = "Press Enter to start timing\nPress any key to stop timing\nType 'n' for a new scramble\nType 'c' to clear the screen\nType 'h' or '?' for this message\nType 'q' to quit"
4
-
5
- def self.loop
6
- puts LOOP
7
- end
8
- end
9
- end
@@ -1,49 +0,0 @@
1
- require_relative 'clear'
2
- require_relative 'help'
3
- require_relative 'timer'
4
- require_relative 'scrambler'
5
-
6
- module RubiksCli
7
- class Solve
8
- def self.solve
9
- puts Scrambler.get_scramble
10
- gets
11
- Clear.line_above
12
- Timer.show
13
- end
14
-
15
- def self.loop(clear_screen)
16
- Help.loop
17
- Kernel.loop do
18
- begin
19
- input = gets.chomp
20
- clear_screen ? Clear.screen : Clear.line_above
21
- process_input(input)
22
- rescue SystemExit
23
- exit
24
- end
25
- end
26
- end
27
-
28
- private
29
- def self.process_input(cmd)
30
- case cmd.downcase
31
- when 'n'
32
- puts Scrambler.get_scramble
33
- when 'c'
34
- Clear.screen
35
- when 'h', '?'
36
- Help.loop
37
- when 'q'
38
- raise SystemExit
39
- else
40
- Timer.show
41
- puts "\n#{Scrambler.get_scramble}"
42
- end
43
- end
44
- end
45
- end
46
-
47
- if __FILE__ == $0
48
- RubiksCli::Solve.solve
49
- end