dialog_tui 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/dialog_tui.gemspec +36 -0
- data/lib/dialog_tui/neighbors.rb +105 -0
- data/lib/dialog_tui/user_action/action.rb +114 -0
- data/lib/dialog_tui/user_action/read_char.rb +76 -0
- data/lib/dialog_tui/user_action.rb +130 -0
- data/lib/dialog_tui/version.rb +3 -0
- data/lib/dialog_tui.rb +177 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cb44c246b8c595d9080d02a941e937f1c0929aff
|
4
|
+
data.tar.gz: d1b788e3a2b8a96db07fa135dae71f123e2128a3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3a158b405922118529c6e6df886c7ff4404dd24a64904173d4c18526511bd77378e785f10df146d3f59c6259e081317207f8d9b7660dc533bb8162fc66119b21
|
7
|
+
data.tar.gz: ceb0862cfce4131c7b5e93136ca34064a6b3fedaf703ee9c42d41ee7af1605b4e1fca678e76092e18a1c3a47b4a8a2b8da67fa9569fb3b9d1eebc359dccf8094
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Alexander K
|
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,39 @@
|
|
1
|
+
# DialogTui
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dialog_tui`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'dialog_tui'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install dialog_tui
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/[my-github-username]/dialog_tui/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dialog_tui"
|
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
|
data/bin/setup
ADDED
data/dialog_tui.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dialog_tui/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dialog_tui"
|
8
|
+
spec.version = DialogTui::VERSION
|
9
|
+
spec.authors = ["Alexander K"]
|
10
|
+
spec.email = ["xpyro@ya.ru"]
|
11
|
+
|
12
|
+
spec.summary = %q{
|
13
|
+
menu for terminal ui
|
14
|
+
}.gsub("\n",' ').squeeze ' '
|
15
|
+
|
16
|
+
spec.description = %q{
|
17
|
+
dsl for simple dialogs/menus
|
18
|
+
for simple terminal ui apps
|
19
|
+
with no other deps currently
|
20
|
+
}.gsub("\n",' ').squeeze ' ' # NOTE: deps in desc
|
21
|
+
|
22
|
+
spec.homepage = 'https://github.com/sowcow/dialog_tui'
|
23
|
+
spec.license = 'MIT'
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
|
33
|
+
spec.add_development_dependency 'maxitest' #, '~> 2.4.6'
|
34
|
+
# ↑ NOTE: should be ok
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module DialogTui
|
2
|
+
|
3
|
+
class Neighbors
|
4
|
+
def initialize array
|
5
|
+
neighbors =\
|
6
|
+
array.zip( array.rotate, array.rotate(-1) )
|
7
|
+
|
8
|
+
keys_values =\
|
9
|
+
neighbors.map { |entry, nexxt, prev|
|
10
|
+
[
|
11
|
+
entry, # key
|
12
|
+
{ next: nexxt, prev: prev } # value
|
13
|
+
]
|
14
|
+
}
|
15
|
+
|
16
|
+
@neighbors_of = Hash[keys_values]
|
17
|
+
end
|
18
|
+
|
19
|
+
def next from
|
20
|
+
neighbors(from)[:next]
|
21
|
+
end
|
22
|
+
|
23
|
+
def prev from
|
24
|
+
neighbors(from)[:prev]
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def neighbors entry
|
29
|
+
@neighbors_of.fetch entry
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
# too much tests for one-liners class :)
|
37
|
+
# (that was written first btw)
|
38
|
+
# (upd: initialize is not a one-liner after refactoring,
|
39
|
+
# but private method is simpler now and others
|
40
|
+
# are more readable!)
|
41
|
+
#
|
42
|
+
# but I guess confidence and ability to rely on something
|
43
|
+
# costs it, and this simple one
|
44
|
+
# is a good point to start
|
45
|
+
#
|
46
|
+
# not that simple effect of tests
|
47
|
+
# is that I can write more complex code under them...
|
48
|
+
# hm...
|
49
|
+
# - so refactor wizely! and freely!
|
50
|
+
|
51
|
+
|
52
|
+
if __FILE__ == $0
|
53
|
+
require 'maxitest/autorun'
|
54
|
+
include DialogTui
|
55
|
+
|
56
|
+
describe Neighbors do
|
57
|
+
def subj
|
58
|
+
Neighbors
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'constructor' do
|
62
|
+
def call array
|
63
|
+
subj.new array
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'takes an array' do
|
67
|
+
call [1,2,3]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#next' do
|
72
|
+
def call array, entry
|
73
|
+
subj.new(array).next entry
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'takes an entry and returns next to it' do
|
77
|
+
call([1,2,3], 1).must_equal 2
|
78
|
+
call([1,2,3], 2).must_equal 3
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'the next after the last is the first' do
|
82
|
+
call([1,2,3], 3).must_equal 1
|
83
|
+
end
|
84
|
+
|
85
|
+
#it 'react in some way on unknown entry...'
|
86
|
+
# not sure what behavior will be default
|
87
|
+
# and not going to fix with tests that first
|
88
|
+
# that came to my mind (Hash#fetch currently)
|
89
|
+
end
|
90
|
+
|
91
|
+
describe '#prev' do
|
92
|
+
def call array, entry
|
93
|
+
subj.new(array).prev entry
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'is reverse of #next' do
|
97
|
+
call([1,2,3], 1).must_equal 3
|
98
|
+
call([1,2,3], 2).must_equal 1
|
99
|
+
call([1,2,3], 3).must_equal 2
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
module DialogTui
|
2
|
+
module UserAction
|
3
|
+
|
4
|
+
class Action
|
5
|
+
def initialize name, mapping=CHARS, &block
|
6
|
+
@matches = mapping.fetch name.to_sym
|
7
|
+
@block = block
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :matches
|
11
|
+
private :matches
|
12
|
+
|
13
|
+
def match? input
|
14
|
+
matches == input
|
15
|
+
end
|
16
|
+
|
17
|
+
alias matches? match?
|
18
|
+
|
19
|
+
def call
|
20
|
+
@block.call
|
21
|
+
end
|
22
|
+
|
23
|
+
# any other mapping can be passed into constructor!
|
24
|
+
# sounds odd to pass it here,
|
25
|
+
# builder is a better idea,
|
26
|
+
# anyway strange class + builder here...
|
27
|
+
#
|
28
|
+
CHARS = {
|
29
|
+
ctrl_c: "\u0003",
|
30
|
+
space: ' ',
|
31
|
+
|
32
|
+
esc: "\e",
|
33
|
+
escape: "\e",
|
34
|
+
|
35
|
+
enter: "\r",
|
36
|
+
return: "\r",
|
37
|
+
|
38
|
+
up: "\e[A",
|
39
|
+
down: "\e[B",
|
40
|
+
right: "\e[C",
|
41
|
+
left: "\e[D",
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
if __FILE__ == $0
|
50
|
+
require 'maxitest/autorun'
|
51
|
+
|
52
|
+
describe DialogTui::UserAction::Action do
|
53
|
+
def subj
|
54
|
+
DialogTui::UserAction::Action
|
55
|
+
end
|
56
|
+
|
57
|
+
describe 'constructor' do
|
58
|
+
def call *a, &b
|
59
|
+
subj.new *a, &b
|
60
|
+
end
|
61
|
+
|
62
|
+
# FIXME user-actions sounds more abstract
|
63
|
+
# but it is coupled to keys...
|
64
|
+
#
|
65
|
+
it 'takes a key name and action block' do
|
66
|
+
call :enter do puts :hey end
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'optionally takes a mapping of actions' do
|
70
|
+
mapping = {
|
71
|
+
:any => '*'
|
72
|
+
}
|
73
|
+
call(:any, mapping).match?('*').must_equal true
|
74
|
+
# odd test?
|
75
|
+
# even if to ignore it uses other methods
|
76
|
+
# (not certain about that yet)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
describe '#matches?' do
|
82
|
+
|
83
|
+
it 'is aliased with #match?' do
|
84
|
+
action = subj.new :enter do end
|
85
|
+
|
86
|
+
action.method(:matches?).must_equal\
|
87
|
+
action.method(:match?)
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'checks if a given key is handled by the action' do
|
91
|
+
action = subj.new :enter do end
|
92
|
+
action.matches?("\r").must_equal true
|
93
|
+
action.matches?(" ").must_equal false
|
94
|
+
action.matches?("\n").must_equal false
|
95
|
+
# the last one looks non-intuitive
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
describe '#call' do
|
102
|
+
it 'calls an action block' do
|
103
|
+
called = false
|
104
|
+
action = subj.new :enter do
|
105
|
+
called = true
|
106
|
+
end
|
107
|
+
called.must_equal false
|
108
|
+
action.call
|
109
|
+
called.must_equal true
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
#
|
2
|
+
# ↓ almost copy-pasted from a gist
|
3
|
+
#
|
4
|
+
|
5
|
+
|
6
|
+
require 'io/console'
|
7
|
+
|
8
|
+
|
9
|
+
module DialogTui
|
10
|
+
module UserAction
|
11
|
+
|
12
|
+
module_function
|
13
|
+
|
14
|
+
# Reads keypresses from the user including 2 and 3 escape character sequences.
|
15
|
+
def read_char
|
16
|
+
STDIN.echo = false
|
17
|
+
STDIN.raw!
|
18
|
+
|
19
|
+
input = STDIN.getc.chr
|
20
|
+
if input == "\e" then
|
21
|
+
input << STDIN.read_nonblock(3) rescue nil
|
22
|
+
input << STDIN.read_nonblock(2) rescue nil
|
23
|
+
end
|
24
|
+
ensure
|
25
|
+
STDIN.echo = true
|
26
|
+
STDIN.cooked!
|
27
|
+
|
28
|
+
return input
|
29
|
+
end
|
30
|
+
|
31
|
+
# oringal case statement from:
|
32
|
+
# http://www.alecjacobson.com/weblog/?p=75
|
33
|
+
def show_single_key
|
34
|
+
c = read_char
|
35
|
+
|
36
|
+
case c
|
37
|
+
when " "
|
38
|
+
puts "SPACE"
|
39
|
+
when "\t"
|
40
|
+
puts "TAB"
|
41
|
+
when "\r"
|
42
|
+
puts "RETURN"
|
43
|
+
when "\n"
|
44
|
+
puts "LINE FEED"
|
45
|
+
when "\e"
|
46
|
+
puts "ESCAPE"
|
47
|
+
when "\e[A"
|
48
|
+
puts "UP ARROW"
|
49
|
+
when "\e[B"
|
50
|
+
puts "DOWN ARROW"
|
51
|
+
when "\e[C"
|
52
|
+
puts "RIGHT ARROW"
|
53
|
+
when "\e[D"
|
54
|
+
puts "LEFT ARROW"
|
55
|
+
when "\177"
|
56
|
+
puts "BACKSPACE"
|
57
|
+
when "\004"
|
58
|
+
puts "DELETE"
|
59
|
+
when "\e[3~"
|
60
|
+
puts "ALTERNATE DELETE"
|
61
|
+
when "\u0003"
|
62
|
+
puts "CONTROL-C"
|
63
|
+
exit 0
|
64
|
+
when /^.$/
|
65
|
+
puts "SINGLE CHAR HIT: #{c.inspect}"
|
66
|
+
else
|
67
|
+
puts "SOMETHING ELSE: #{c.inspect}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
if __FILE__ == $0
|
75
|
+
DialogTui::UserAction.show_single_key while true
|
76
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require_relative 'user_action/action'
|
2
|
+
require_relative 'user_action/read_char'
|
3
|
+
|
4
|
+
# if it failed - needs to be killed manually
|
5
|
+
# because ctrl+c is frozen
|
6
|
+
# okish?
|
7
|
+
|
8
|
+
module DialogTui
|
9
|
+
module UserAction
|
10
|
+
module_function
|
11
|
+
|
12
|
+
def user_action &block
|
13
|
+
UserActions.run &block
|
14
|
+
end
|
15
|
+
|
16
|
+
class UserActions
|
17
|
+
def self.run &block
|
18
|
+
new(&block).run
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
@actions = []
|
23
|
+
yield self
|
24
|
+
end
|
25
|
+
|
26
|
+
DSL_METHODS = %i[
|
27
|
+
|
28
|
+
ctrl_c
|
29
|
+
|
30
|
+
enter
|
31
|
+
esc
|
32
|
+
|
33
|
+
up
|
34
|
+
down
|
35
|
+
left
|
36
|
+
right
|
37
|
+
]
|
38
|
+
|
39
|
+
DSL_METHODS.each { |name|
|
40
|
+
define_method name do |&block|
|
41
|
+
action = Action.new name, &block
|
42
|
+
@actions << action
|
43
|
+
end
|
44
|
+
}
|
45
|
+
|
46
|
+
def run
|
47
|
+
loop {
|
48
|
+
input = UserAction.read_char
|
49
|
+
matched = @actions.select { |x| x.matches? input }
|
50
|
+
|
51
|
+
if matched.any?
|
52
|
+
matched.each &:call
|
53
|
+
break
|
54
|
+
else
|
55
|
+
# just ignore and wait for next input
|
56
|
+
end
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
if __FILE__ == $0
|
66
|
+
require 'maxitest/autorun'
|
67
|
+
|
68
|
+
describe DialogTui::UserAction do
|
69
|
+
def subj
|
70
|
+
DialogTui::UserAction
|
71
|
+
end
|
72
|
+
|
73
|
+
describe 'user_action method' do
|
74
|
+
def this_method
|
75
|
+
:user_action
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'can be included or called directly' do
|
79
|
+
|
80
|
+
subj = self.subj
|
81
|
+
|
82
|
+
_My = Class.new do
|
83
|
+
include subj
|
84
|
+
end
|
85
|
+
|
86
|
+
(_My.new.method(:non_existing) rescue :err).
|
87
|
+
must_equal :err
|
88
|
+
|
89
|
+
_My.new.method(this_method).
|
90
|
+
wont_equal nil
|
91
|
+
|
92
|
+
# actually not a deep testing of this one
|
93
|
+
# full testing is possible TODO maybe
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'usage example' do
|
98
|
+
|
99
|
+
puts " = press arrows to manually test it,\
|
100
|
+
enter to finish,\
|
101
|
+
esc if test is failed = ".squeeze(' ')
|
102
|
+
|
103
|
+
finished = false
|
104
|
+
|
105
|
+
begin
|
106
|
+
subj.user_action { |key|
|
107
|
+
key.up do p :up end
|
108
|
+
key.down do p :down end
|
109
|
+
key.left do p :left end
|
110
|
+
key.right do p :right end
|
111
|
+
|
112
|
+
key.enter do
|
113
|
+
finished = true
|
114
|
+
end
|
115
|
+
key.ctrl_c do
|
116
|
+
exit 0 # xz
|
117
|
+
end
|
118
|
+
|
119
|
+
key.esc do
|
120
|
+
'user decided the test is failed'.
|
121
|
+
must_equal 'user said enter if test is ok'
|
122
|
+
#raise 'user-driven test failed'
|
123
|
+
end
|
124
|
+
}
|
125
|
+
end until finished
|
126
|
+
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
data/lib/dialog_tui.rb
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
require_relative 'dialog_tui/neighbors'
|
2
|
+
require_relative 'dialog_tui/user_action'
|
3
|
+
|
4
|
+
|
5
|
+
module DialogTui
|
6
|
+
module_function # include or call explicitly
|
7
|
+
|
8
|
+
|
9
|
+
# preferred public interface
|
10
|
+
#
|
11
|
+
# usage example:
|
12
|
+
#
|
13
|
+
# dialog { |an|
|
14
|
+
#
|
15
|
+
# an.option '1. first' do
|
16
|
+
# puts 'hey, user just chosen this one!'
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# an.option '2. second' do
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# an.option 'you got it' do
|
23
|
+
# end
|
24
|
+
# }
|
25
|
+
#
|
26
|
+
def dialog &block
|
27
|
+
Dialog.run &block
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
class Dialog
|
32
|
+
include UserAction
|
33
|
+
|
34
|
+
def self.run &block
|
35
|
+
new(&block).run
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
def initialize &block
|
40
|
+
@options = []
|
41
|
+
|
42
|
+
if block.arity == 0
|
43
|
+
instance_eval &block
|
44
|
+
else
|
45
|
+
block.call self
|
46
|
+
end
|
47
|
+
|
48
|
+
raise unless @options.count > 0
|
49
|
+
# options list is assumed to be fixed now
|
50
|
+
|
51
|
+
@current = @options.first
|
52
|
+
@neighbors = Neighbors.new @options
|
53
|
+
end
|
54
|
+
|
55
|
+
def run
|
56
|
+
begin
|
57
|
+
# print_usage #...
|
58
|
+
print_options # would be nice to have a printer...
|
59
|
+
|
60
|
+
done = false
|
61
|
+
|
62
|
+
user_action { |key|
|
63
|
+
|
64
|
+
key.up do
|
65
|
+
@current = prev
|
66
|
+
end
|
67
|
+
|
68
|
+
key.down do
|
69
|
+
@current = nexxt
|
70
|
+
end
|
71
|
+
|
72
|
+
#key.ctrl_c do exit 0 end
|
73
|
+
#key.esc do exit 0 end
|
74
|
+
|
75
|
+
key.enter do
|
76
|
+
done = true
|
77
|
+
end
|
78
|
+
}
|
79
|
+
end until done
|
80
|
+
|
81
|
+
@current.call
|
82
|
+
end
|
83
|
+
|
84
|
+
def chosen? option
|
85
|
+
@current == option # so current or chosen?)
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
def option text, &reaction
|
90
|
+
option = Option.new self, text, &reaction # order?
|
91
|
+
@options.push option
|
92
|
+
end
|
93
|
+
|
94
|
+
def print_options
|
95
|
+
puts '-'*10
|
96
|
+
@options.each &:print
|
97
|
+
puts '-'*10
|
98
|
+
end
|
99
|
+
|
100
|
+
def prev
|
101
|
+
@neighbors.prev @current
|
102
|
+
end
|
103
|
+
|
104
|
+
def nexxt
|
105
|
+
@neighbors.next @current
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
# NOTE: not sure about perfomance implications
|
111
|
+
# of that chain of &block passing instead of
|
112
|
+
# just using variable w/o `&`
|
113
|
+
# but & looks very explicit and is not as annoying
|
114
|
+
# as additional parameter
|
115
|
+
|
116
|
+
class Option
|
117
|
+
def initialize dialog, text, &reaction
|
118
|
+
@dialog = dialog
|
119
|
+
@text = text.to_s
|
120
|
+
@reaction = reaction
|
121
|
+
end
|
122
|
+
|
123
|
+
def call
|
124
|
+
@reaction.call
|
125
|
+
end
|
126
|
+
|
127
|
+
def print printer=nil # just to be here for future
|
128
|
+
# refactoring
|
129
|
+
if chosen?
|
130
|
+
puts '=> ' + @text
|
131
|
+
else
|
132
|
+
puts ' ' + @text
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def chosen?
|
137
|
+
@dialog.chosen? self
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
if __FILE__ == $0
|
146
|
+
|
147
|
+
class My
|
148
|
+
include DialogTui
|
149
|
+
|
150
|
+
def act
|
151
|
+
|
152
|
+
dialog {
|
153
|
+
|
154
|
+
option '1. hey' do
|
155
|
+
puts 'you choose the first one!'
|
156
|
+
end
|
157
|
+
|
158
|
+
option '2. hi!' do
|
159
|
+
puts 'you choose the second one!'
|
160
|
+
end
|
161
|
+
|
162
|
+
option '3. bye' do
|
163
|
+
puts '*waving*'
|
164
|
+
end
|
165
|
+
}.
|
166
|
+
tap { |result|
|
167
|
+
raise unless result.nil?
|
168
|
+
# because it works for effect, not for return value
|
169
|
+
}
|
170
|
+
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
puts 'manual testing here - use arrows and enter'
|
175
|
+
puts 'no way to fail it - just look at behavior'
|
176
|
+
3.times { My.new.act }
|
177
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dialog_tui
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexander K
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: maxitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: " dsl for simple dialogs/menus for simple terminal ui apps with no other
|
56
|
+
deps currently "
|
57
|
+
email:
|
58
|
+
- xpyro@ya.ru
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- dialog_tui.gemspec
|
73
|
+
- lib/dialog_tui.rb
|
74
|
+
- lib/dialog_tui/neighbors.rb
|
75
|
+
- lib/dialog_tui/user_action.rb
|
76
|
+
- lib/dialog_tui/user_action/action.rb
|
77
|
+
- lib/dialog_tui/user_action/read_char.rb
|
78
|
+
- lib/dialog_tui/version.rb
|
79
|
+
homepage: https://github.com/sowcow/dialog_tui
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.4.6
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: menu for terminal ui
|
103
|
+
test_files: []
|