menuboy 0.0.3 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a01fbe07a59776db3444f112f2d9c9e196fa2ee
4
- data.tar.gz: 587dcdad15fe6c92b521a1dfbf7cedeab0d0054d
3
+ metadata.gz: f981149a9b3c5100d98d9185cc0a63672853d2c4
4
+ data.tar.gz: 730e809487411b80f7752f5f4bb09e42a45de2f2
5
5
  SHA512:
6
- metadata.gz: bcff7897dd1845f122067ad8f56c330908e00082f64a7ddd1c5ef4a22958844b805fb1ac431e728750e69080ac93b478b96a153772c049184994a8d183025380
7
- data.tar.gz: cc3667128373d61fd2a0083ed412fc3e2a85a3200aae8931d835f13f6ba25f4243d934d4f8ae469d6e6f73b2e5e181fec31539cef92a49876005facf2dc7e1d8
6
+ metadata.gz: a9055ab61a5209a6459f89cca52cb38114c3b39bb6c0f42321f93b3bc98f224eb358a46ce51acf7fb62f9fb562d58ad69eb6a6862f25534bc2d28ce2ad8a8a51
7
+ data.tar.gz: 38398fc9e2767fda3bffea8d1c20be5e52f22a0a99d06cc7a228cf5b98d0828276503aedd1f481dd94c725356afce82e76cb54f05a23152cc210cbbaf410aa51
data/README.md CHANGED
@@ -8,6 +8,16 @@ managing our node.js/mongodb/redis stack in a super simple way. You can
8
8
  see the basic idea of that in test/example.rb, which was used to develop
9
9
  menuboy.
10
10
 
11
+ ---
12
+
13
+ ### v0.1.0
14
+ * Remove `STDIN.getch` loop approach which was causing formatting
15
+ issues with other programs trying to write to STDOUT
16
+ * Use EventMachine to capture keyboard input
17
+ * Use ruby-termios to enable raw keyboard capture
18
+ * Provide `Menuboy#fix_stdin` public helper method to temporary re-enable
19
+ normal stdin behavior
20
+
11
21
  ## Installation
12
22
 
13
23
  Add this line to your application's Gemfile:
data/lib/menuboy.rb CHANGED
@@ -1,7 +1,56 @@
1
1
  require "menuboy/version"
2
2
  require 'io/console'
3
+ require 'termios'
4
+ require 'eventmachine'
5
+
3
6
 
4
7
  module Menuboy
8
+ @termios_normal_attributes = Termios.tcgetattr($stdin).dup
9
+
10
+ def self.raw_terminal
11
+ attributes = @termios_normal_attributes.dup
12
+ attributes.lflag &= ~Termios::ECHO
13
+ attributes.lflag &= ~Termios::ICANON
14
+ Termios::tcsetattr($stdin, Termios::TCSANOW, attributes)
15
+ end
16
+
17
+ def self.normal_terminal
18
+ Termios::tcsetattr($stdin, Termios::TCSANOW, @termios_normal_attributes)
19
+ end
20
+
21
+ module UnbufferedKeyboardHandler
22
+ def receive_data(k)
23
+ puts k
24
+ menu = Menuboy.menus.last
25
+ if k == "q"
26
+ Menuboy.menus.pop
27
+ if next_menu = Menuboy.menus.last
28
+ next_menu.print_help
29
+ else
30
+ exit # no more menus
31
+ end
32
+ else
33
+ menu.handle_input(k)
34
+ end
35
+ Menuboy.menus.last.prompt
36
+ end
37
+ end
38
+
39
+ ##
40
+ # Use this if you need to re-enable buffering
41
+ # as menuboy disables this by default on STDIN
42
+ # You don't need this when using #system but
43
+ # you'll want it if you get user input directly
44
+ def self.fix_stdin
45
+ self.normal_terminal
46
+ yield
47
+ self.raw_terminal
48
+ end
49
+
50
+ def self.menus
51
+ @menus ||= []
52
+ end
53
+
5
54
  module DSL
6
55
  def option name, &block
7
56
  opt_count = @target.options.count
@@ -17,7 +66,8 @@ module Menuboy
17
66
  option(name) do
18
67
  @target = Menu.new(name)
19
68
  yield
20
- @target.start
69
+ Menuboy.menus.push @target
70
+ @target.print_help
21
71
  end
22
72
  end
23
73
 
@@ -25,10 +75,18 @@ module Menuboy
25
75
  if @mainmenu
26
76
  raise StandardError, "You can only define one main menu"
27
77
  else
28
- @mainmenu = Menu.new(name)
29
- @target = @mainmenu
78
+ @target = @mainmenu = Menu.new(name)
30
79
  yield
31
- @mainmenu.start
80
+ Menuboy.menus.push @target
81
+ @target.print_help
82
+ @target.prompt
83
+
84
+ Signal.trap("INT") { exit }
85
+
86
+ EM.run do
87
+ Menuboy.raw_terminal
88
+ EM.open_keyboard(UnbufferedKeyboardHandler)
89
+ end
32
90
  end
33
91
  end
34
92
  end
@@ -61,24 +119,23 @@ module Menuboy
61
119
  matches.length == 1 ? matches.last : false
62
120
  end
63
121
 
64
- def start
65
- print_help
66
- loop do
67
- print "(#{@name})> "
68
- input = STDIN.getch
69
- puts input
70
- if input == CTRL_C || input == "q"
71
- break
72
- elsif input == "h"
73
- print_help
74
- elsif input == "p" && defined? Pry
122
+ def handle_input k
123
+ if k == "h"
124
+ print_help
125
+ elsif k == "p" && defined? Pry
126
+ Menuboy.fix_stdin do
75
127
  binding.pry
76
- elsif option = option_match?(input)
77
- option.proc.call
78
- else
79
- puts "Unassigned input"
80
128
  end
129
+ elsif option = option_match?(k)
130
+ option.proc.call
131
+ else
132
+ puts "Unassigned input"
81
133
  end
82
134
  end
135
+
136
+ def prompt
137
+ print "(#{@name})> "
138
+ end
83
139
  end
84
140
  end
141
+
@@ -1,3 +1,3 @@
1
1
  module Menuboy
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
data/menuboy.gemspec CHANGED
@@ -18,6 +18,9 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_runtime_dependency 'ruby-termios'
22
+ spec.add_runtime_dependency 'eventmachine'
21
23
  spec.add_development_dependency "bundler", "~> 1.3"
22
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "pry"
23
26
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: menuboy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keyvan Fatehi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-05 00:00:00.000000000 Z
11
+ date: 2013-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-termios
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: eventmachine
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: bundler
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +66,20 @@ dependencies:
38
66
  - - '>='
39
67
  - !ruby/object:Gem::Version
40
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
41
83
  description: Simple CLI-based menu building DSL
42
84
  email:
43
85
  - keyvanfatehi@gmail.com