terminal-menu 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +92 -0
- data/lib/terminal-menu/version.rb +3 -0
- data/lib/terminal-menu.rb +151 -0
- data/terminal-menu.gemspec +30 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 42ebb17b36ef8b8d895711f65fc2cf7379fdba96
|
4
|
+
data.tar.gz: cbc7b91df3a8d07e9bc8e88a3dba6671a5c2f885
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f654dca829f81a091b5022dfc8b808121d9df06e32bc88c6224724f83625cc2d0f1a0e58c6ecc0c0f0ef97f4c4a4a0f70695bbfabb074374924ca4b06d176754
|
7
|
+
data.tar.gz: c6ae9b699e8c475abf4ee2f6db5535cdc404653e9c6e80c7061f8c8acad1e1041d08f55edb0d488e5f8686df9670ed08a035075590f67faa4eebae22703442fd
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Two Bucks Ltd
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Terminal Menu
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/twobucks/terminal-menu.svg)](https://travis-ci.org/twobucks/terminal-menu)
|
4
|
+
|
5
|
+
Retro ANSI terminal menu.
|
6
|
+
|
7
|
+
![example](example.png)
|
8
|
+
|
9
|
+
|
10
|
+
## Install
|
11
|
+
|
12
|
+
```
|
13
|
+
gem install terminal-menu
|
14
|
+
```
|
15
|
+
|
16
|
+
## Example
|
17
|
+
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'terminal-menu'
|
21
|
+
|
22
|
+
menu = TerminalMenu.new(title: 'Rick Astley - Never Gonna Give You Up',
|
23
|
+
description: '',
|
24
|
+
width: 40,
|
25
|
+
fg: 'white',
|
26
|
+
bg: 'red') do |selected|
|
27
|
+
puts "you have selected: #{selected}"
|
28
|
+
menu.quit
|
29
|
+
end
|
30
|
+
|
31
|
+
menu.add('Never gonna give you up') do |selected|
|
32
|
+
puts "first option selected"
|
33
|
+
end
|
34
|
+
menu.add('Never gonna let you down') do |selected|
|
35
|
+
puts "second option selected"
|
36
|
+
end
|
37
|
+
menu.add('Never gonna run around and desert you') do |selected|
|
38
|
+
puts "third option selected"
|
39
|
+
end
|
40
|
+
menu.show
|
41
|
+
```
|
42
|
+
|
43
|
+
# API
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
# Initializes new TerminalMenu instance.
|
47
|
+
#
|
48
|
+
# block - Optional block that will get called with label once some option
|
49
|
+
# is selected
|
50
|
+
#
|
51
|
+
# Options:
|
52
|
+
#
|
53
|
+
# title - The title of this menu (default: '')
|
54
|
+
# description - The description of this menu (default: '')
|
55
|
+
# width - The width of this menu in chars (default: 80)
|
56
|
+
# fg - Foreground color of this menu (default: 'white')
|
57
|
+
# bg - Background color of this menu (default: 'black')
|
58
|
+
# stdin - Input IO instance (default: STDIN)
|
59
|
+
# stdout - Output IO instance (default: STDOUT)
|
60
|
+
menu = TerminalMenu.new(title: '', description: '', width: 80,
|
61
|
+
fg: 'white', bg: 'black',
|
62
|
+
stdin: STDIN, stdout: STDOUT){|selected| block }
|
63
|
+
|
64
|
+
|
65
|
+
# Adds a new item to menu options.
|
66
|
+
#
|
67
|
+
# label - Label for this menu option.
|
68
|
+
# blk - Optional block that will be called once this option is selected.
|
69
|
+
menu.add(label, &blk)
|
70
|
+
|
71
|
+
# Starts read-eval-print loop.
|
72
|
+
menu.show
|
73
|
+
|
74
|
+
# Quits this menu.
|
75
|
+
menu.quit
|
76
|
+
```
|
77
|
+
|
78
|
+
## Thanks
|
79
|
+
|
80
|
+
To [@substack](https://github.com/substack) for creating [terminal-menu](https://github.com/substack/terminal-menu) for Node.
|
81
|
+
|
82
|
+
## License
|
83
|
+
|
84
|
+
MIT
|
85
|
+
|
86
|
+
## Sponsors
|
87
|
+
|
88
|
+
Two Bucks Ltd. © 2016
|
89
|
+
|
90
|
+
<a href="https://twobucks.co">
|
91
|
+
![https://twobucks.co](https://twobucks.co/assets/images/logo-small.png)
|
92
|
+
</a>
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'paint'
|
2
|
+
require 'io/console'
|
3
|
+
|
4
|
+
class TerminalMenu
|
5
|
+
# Public: Initializes new TerminalMenu instance.
|
6
|
+
#
|
7
|
+
# block - Optional block that will get called with label once some option
|
8
|
+
# is selected
|
9
|
+
#
|
10
|
+
# Options:
|
11
|
+
#
|
12
|
+
# title - The title of this menu (default: '')
|
13
|
+
# description - The description of this menu (default: '')
|
14
|
+
# width - The width of this menu in chars (default: 80)
|
15
|
+
# fg - Foreground color of this menu (default: 'white')
|
16
|
+
# bg - Background color of this menu (default: 'black')
|
17
|
+
# stdin - Input IO instance (default: STDIN)
|
18
|
+
# stdout - Output IO instance (default: STDOUT)
|
19
|
+
def initialize(title: '', description: '', width: 80,
|
20
|
+
fg: 'white', bg: 'black',
|
21
|
+
stdin: STDIN, stdout: STDOUT, &selected_callback)
|
22
|
+
@title = title
|
23
|
+
@description = description
|
24
|
+
@width = width
|
25
|
+
@selected_callback = selected_callback || -> (arg) {}
|
26
|
+
@fg = fg
|
27
|
+
@bg = bg
|
28
|
+
@stdin = stdin
|
29
|
+
@stdout = stdout
|
30
|
+
|
31
|
+
@selected_index = 0
|
32
|
+
|
33
|
+
@options = [{label: 'exit', callback: ->{}}]
|
34
|
+
@last_char = []
|
35
|
+
end
|
36
|
+
|
37
|
+
# Public: Adds a new item to menu options.
|
38
|
+
#
|
39
|
+
# label - Label for this menu option.
|
40
|
+
# blk - Optional block that will be called once this option is selected.
|
41
|
+
def add(label, &blk)
|
42
|
+
# preserve exit as the last option
|
43
|
+
last = @options.pop
|
44
|
+
@options << {label: label, callback: blk || -> (arg) {}}
|
45
|
+
@options << last
|
46
|
+
end
|
47
|
+
|
48
|
+
# Public: Starts read-eval-print loop.
|
49
|
+
def show
|
50
|
+
while true do
|
51
|
+
clear_screen
|
52
|
+
print_screen
|
53
|
+
get_input
|
54
|
+
end
|
55
|
+
rescue IOError
|
56
|
+
# do nothing, program has ended
|
57
|
+
end
|
58
|
+
|
59
|
+
# Public: Quits this menu.
|
60
|
+
def quit
|
61
|
+
@stdin.close
|
62
|
+
@stdout.close
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def go_down
|
68
|
+
if @selected_index == @options.length - 1
|
69
|
+
@selected_index = 0
|
70
|
+
else
|
71
|
+
@selected_index += 1
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def go_up
|
76
|
+
if @selected_index == 0
|
77
|
+
@selected_index = @options.length - 1
|
78
|
+
else
|
79
|
+
@selected_index -= 1
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def print_header
|
84
|
+
print(@title.upcase)
|
85
|
+
print(@description)
|
86
|
+
print_delimiter
|
87
|
+
end
|
88
|
+
|
89
|
+
def print_options
|
90
|
+
@options.each_with_index do |option, i|
|
91
|
+
if i == @options.length - 1 # last option AKA exit
|
92
|
+
print_delimiter
|
93
|
+
print("#{option[:label].upcase}", inverted: i == @selected_index)
|
94
|
+
else
|
95
|
+
print(">> #{option[:label].upcase}", inverted: i == @selected_index)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def print_delimiter
|
101
|
+
print('-' * @width)
|
102
|
+
end
|
103
|
+
|
104
|
+
def print_screen
|
105
|
+
print_header
|
106
|
+
print_options
|
107
|
+
end
|
108
|
+
|
109
|
+
def print(text, inverted: false)
|
110
|
+
if inverted
|
111
|
+
@stdout.puts Paint[text.ljust(@width), @bg, @fg]
|
112
|
+
else
|
113
|
+
@stdout.puts Paint[text.ljust(@width), @fg, @bg]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def get_input
|
118
|
+
char = @stdin.getch
|
119
|
+
|
120
|
+
@last_char << char # special characters (like up arrow) get sent
|
121
|
+
# as mulitple characters, so we have to store past
|
122
|
+
# few characters somewhere
|
123
|
+
|
124
|
+
case
|
125
|
+
when @last_char.join == "\e[A" || char == 'k' # up arrow or k
|
126
|
+
go_up
|
127
|
+
@last_char = []
|
128
|
+
when @last_char.join == "\e[B" || char == 'j' # down arrow or j
|
129
|
+
go_down
|
130
|
+
@last_char = []
|
131
|
+
when char == "\r" # enter
|
132
|
+
if @selected_index == @options.length - 1
|
133
|
+
quit
|
134
|
+
return
|
135
|
+
end
|
136
|
+
|
137
|
+
@options[@selected_index][:callback].call(@options[@selected_index][:label])
|
138
|
+
@selected_callback.call(@options[@selected_index][:label])
|
139
|
+
when char == "\u0003" || char == "q" # ctrl+c or q
|
140
|
+
quit
|
141
|
+
when !@last_char.include?("\e") # not a special character, clear array
|
142
|
+
@last_char = []
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def clear_screen
|
147
|
+
@stdout.puts "\e[0m"
|
148
|
+
@stdout.puts "\e[2J"
|
149
|
+
@stdout.puts "\ec"
|
150
|
+
end
|
151
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + "/lib/terminal-menu/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "terminal-menu"
|
7
|
+
s.version = TerminalMenu::VERSION
|
8
|
+
s.authors = ["Hrvoje Simic"]
|
9
|
+
s.email = "shime@twobucks.co"
|
10
|
+
s.homepage = "https://github.com/twobucks/terminal-menu"
|
11
|
+
s.summary = "Terminal menu"
|
12
|
+
s.description = "Retro ANSI terminal menu"
|
13
|
+
s.license = 'MIT'
|
14
|
+
s.require_path = 'lib'
|
15
|
+
|
16
|
+
s.files = %w[
|
17
|
+
README.md
|
18
|
+
LICENSE
|
19
|
+
terminal-menu.gemspec
|
20
|
+
lib/terminal-menu.rb
|
21
|
+
lib/terminal-menu/version.rb
|
22
|
+
]
|
23
|
+
|
24
|
+
s.required_ruby_version = '>= 2.0'
|
25
|
+
|
26
|
+
s.add_runtime_dependency 'paint', '~> 1.0', '>= 1.0.1'
|
27
|
+
|
28
|
+
s.add_development_dependency 'minitest', '~> 5.9', '>= 5.9.0'
|
29
|
+
s.add_development_dependency 'rake', '~> 11.2', '>= 11.2.2'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: terminal-menu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hrvoje Simic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: paint
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.0.1
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.0.1
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: minitest
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '5.9'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 5.9.0
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '5.9'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 5.9.0
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rake
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '11.2'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 11.2.2
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '11.2'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 11.2.2
|
73
|
+
description: Retro ANSI terminal menu
|
74
|
+
email: shime@twobucks.co
|
75
|
+
executables: []
|
76
|
+
extensions: []
|
77
|
+
extra_rdoc_files: []
|
78
|
+
files:
|
79
|
+
- LICENSE
|
80
|
+
- README.md
|
81
|
+
- lib/terminal-menu.rb
|
82
|
+
- lib/terminal-menu/version.rb
|
83
|
+
- terminal-menu.gemspec
|
84
|
+
homepage: https://github.com/twobucks/terminal-menu
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.5.1
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Terminal menu
|
108
|
+
test_files: []
|
109
|
+
has_rdoc:
|