highline 2.0.0.pre.develop.2 → 2.0.0.pre.develop.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog.md +28 -0
- data/README.md +188 -0
- data/Rakefile +0 -11
- data/highline.gemspec +2 -4
- data/lib/highline.rb +170 -73
- data/lib/highline/builtin_styles.rb +18 -2
- data/lib/highline/color_scheme.rb +15 -5
- data/lib/highline/compatibility.rb +6 -1
- data/lib/highline/custom_errors.rb +43 -6
- data/lib/highline/import.rb +10 -3
- data/lib/highline/list.rb +95 -7
- data/lib/highline/list_renderer.rb +36 -17
- data/lib/highline/menu.rb +73 -18
- data/lib/highline/paginator.rb +10 -0
- data/lib/highline/question.rb +98 -41
- data/lib/highline/question/answer_converter.rb +19 -0
- data/lib/highline/question_asker.rb +21 -17
- data/lib/highline/simulate.rb +10 -1
- data/lib/highline/statement.rb +62 -38
- data/lib/highline/string.rb +26 -25
- data/lib/highline/string_extensions.rb +60 -32
- data/lib/highline/style.rb +125 -25
- data/lib/highline/template_renderer.rb +25 -1
- data/lib/highline/terminal.rb +124 -1
- data/lib/highline/terminal/io_console.rb +6 -75
- data/lib/highline/terminal/ncurses.rb +7 -8
- data/lib/highline/terminal/unix_stty.rb +35 -81
- data/lib/highline/version.rb +1 -1
- data/lib/highline/wrapper.rb +9 -0
- data/test/test_highline.rb +1 -1
- metadata +6 -13
- data/INSTALL +0 -59
- data/README.rdoc +0 -77
- data/setup.rb +0 -1360
@@ -2,97 +2,28 @@
|
|
2
2
|
|
3
3
|
class HighLine
|
4
4
|
class Terminal
|
5
|
+
# io/console option for HighLine::Terminal.
|
6
|
+
# It's the most used terminal.
|
5
7
|
class IOConsole < Terminal
|
8
|
+
# (see Terminal#terminal_size)
|
6
9
|
def terminal_size
|
7
10
|
output.winsize.reverse
|
8
11
|
end
|
9
12
|
|
10
|
-
|
11
|
-
|
13
|
+
# (see Terminal#raw_no_echo_mode)
|
12
14
|
def raw_no_echo_mode
|
13
15
|
input.echo = false
|
14
16
|
end
|
15
17
|
|
18
|
+
# (see Terminal#restore_mode)
|
16
19
|
def restore_mode
|
17
20
|
input.echo = true
|
18
21
|
end
|
19
22
|
|
23
|
+
# (see Terminal#get_character)
|
20
24
|
def get_character
|
21
25
|
input.getch # from ruby io/console
|
22
26
|
end
|
23
|
-
|
24
|
-
def character_mode
|
25
|
-
"io_console"
|
26
|
-
end
|
27
|
-
|
28
|
-
def get_line(question, highline, options={})
|
29
|
-
raw_answer =
|
30
|
-
if question.readline
|
31
|
-
get_line_with_readline(question, highline, options={})
|
32
|
-
else
|
33
|
-
get_line_default(highline)
|
34
|
-
end
|
35
|
-
|
36
|
-
question.format_answer(raw_answer)
|
37
|
-
end
|
38
|
-
|
39
|
-
def get_line_with_readline(question, highline, options={})
|
40
|
-
require "readline" # load only if needed
|
41
|
-
|
42
|
-
question_string = highline.render_statement(question)
|
43
|
-
|
44
|
-
raw_answer = readline_read(question_string, question)
|
45
|
-
|
46
|
-
if !raw_answer and highline.track_eof?
|
47
|
-
raise EOFError, "The input stream is exhausted."
|
48
|
-
end
|
49
|
-
|
50
|
-
raw_answer || ""
|
51
|
-
end
|
52
|
-
|
53
|
-
def readline_read(string, question)
|
54
|
-
# prep auto-completion
|
55
|
-
unless question.selection.empty?
|
56
|
-
Readline.completion_proc = lambda do |str|
|
57
|
-
question.selection.grep(/\A#{Regexp.escape(str)}/)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
# work-around ugly readline() warnings
|
62
|
-
old_verbose = $VERBOSE
|
63
|
-
$VERBOSE = nil
|
64
|
-
|
65
|
-
raw_answer = run_preserving_stty do
|
66
|
-
Readline.readline(string, true)
|
67
|
-
end
|
68
|
-
|
69
|
-
$VERBOSE = old_verbose
|
70
|
-
|
71
|
-
raw_answer
|
72
|
-
end
|
73
|
-
|
74
|
-
def get_line_default(highline)
|
75
|
-
raise EOFError, "The input stream is exhausted." if highline.track_eof? and
|
76
|
-
highline.input.eof?
|
77
|
-
highline.input.gets
|
78
|
-
end
|
79
|
-
|
80
|
-
private
|
81
|
-
|
82
|
-
def run_preserving_stty
|
83
|
-
save_stty
|
84
|
-
yield
|
85
|
-
ensure
|
86
|
-
restore_stty
|
87
|
-
end
|
88
|
-
|
89
|
-
def save_stty
|
90
|
-
@stty_save = `stty -g`.chomp rescue nil
|
91
|
-
end
|
92
|
-
|
93
|
-
def restore_stty
|
94
|
-
system("stty", @stty_save) if @stty_save
|
95
|
-
end
|
96
27
|
end
|
97
28
|
end
|
98
29
|
end
|
@@ -1,26 +1,25 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
# TODO:
|
4
|
-
# Code below to be discussed.
|
5
|
-
# Will we maintain an ncurses version of HighLine::Terminal?
|
6
|
-
# If so, port it to the new api.
|
7
|
-
|
8
3
|
class HighLine
|
9
|
-
|
10
|
-
|
4
|
+
class Terminal
|
5
|
+
# NCurses HighLine::Terminal
|
6
|
+
# @note Code migrated +UNTESTED+ from the old code base to the new terminal api.
|
7
|
+
class NCurses < Terminal
|
11
8
|
require 'ffi-ncurses'
|
12
|
-
CHARACTER_MODE = "ncurses" # For Debugging purposes only.
|
13
9
|
|
10
|
+
# (see Terminal#raw_no_echo_mode)
|
14
11
|
def raw_no_echo_mode
|
15
12
|
FFI::NCurses.initscr
|
16
13
|
FFI::NCurses.cbreak
|
17
14
|
end
|
18
15
|
|
16
|
+
# (see Terminal#restore_mode)
|
19
17
|
def restore_mode
|
20
18
|
FFI::NCurses.endwin
|
21
19
|
end
|
22
20
|
|
23
21
|
#
|
22
|
+
# (see Terminal#terminal_size)
|
24
23
|
# A ncurses savvy method to fetch the console columns, and rows.
|
25
24
|
#
|
26
25
|
def terminal_size
|
@@ -1,94 +1,48 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
3
|
class HighLine
|
4
|
-
class Terminal
|
5
|
-
|
6
|
-
#
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
4
|
+
class Terminal
|
5
|
+
# HighLine::Terminal option that uses external "stty" program
|
6
|
+
# to control terminal options.
|
7
|
+
class UnixStty < Terminal
|
8
|
+
|
9
|
+
# A Unix savvy method using stty to fetch the console columns, and rows.
|
10
|
+
# ... stty does not work in JRuby
|
11
|
+
# @return (see Terminal#terminal_size)
|
12
|
+
def terminal_size
|
13
|
+
begin
|
14
|
+
require "io/console"
|
15
|
+
winsize = IO.console.winsize.reverse rescue nil
|
16
|
+
return winsize if winsize
|
17
|
+
rescue LoadError
|
18
|
+
end
|
19
|
+
|
20
|
+
if /solaris/ =~ RUBY_PLATFORM and
|
21
|
+
`stty` =~ /\brows = (\d+).*\bcolumns = (\d+)/
|
22
|
+
[$2, $1].map { |x| x.to_i }
|
23
|
+
elsif `stty size` =~ /^(\d+)\s(\d+)$/
|
24
|
+
[$2.to_i, $1.to_i]
|
25
|
+
else
|
26
|
+
[ 80, 24 ]
|
27
|
+
end
|
14
28
|
end
|
15
29
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
[$2.to_i, $1.to_i]
|
21
|
-
else
|
22
|
-
[ 80, 24 ]
|
30
|
+
# (see Terminal#raw_no_echo_mode)
|
31
|
+
def raw_no_echo_mode
|
32
|
+
@state = `stty -g`
|
33
|
+
system "stty raw -echo -icanon isig"
|
23
34
|
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# *WARNING*: This requires the external "stty" program!
|
27
|
-
CHARACTER_MODE = "unix_stty" # For Debugging purposes only.
|
28
|
-
|
29
|
-
def raw_no_echo_mode
|
30
|
-
@state = `stty -g`
|
31
|
-
system "stty raw -echo -icanon isig"
|
32
|
-
end
|
33
|
-
|
34
|
-
def restore_mode
|
35
|
-
system "stty #{@state}"
|
36
|
-
print "\r"
|
37
|
-
end
|
38
35
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
def character_mode
|
44
|
-
"unix_stty"
|
45
|
-
end
|
46
|
-
|
47
|
-
def get_line(question, highline, options={})
|
48
|
-
raw_answer =
|
49
|
-
if question.readline
|
50
|
-
get_line_with_readline(question, highline, options={})
|
51
|
-
else
|
52
|
-
get_line_default(highline)
|
36
|
+
# (see Terminal#restore_mode)
|
37
|
+
def restore_mode
|
38
|
+
system "stty #{@state}"
|
39
|
+
print "\r"
|
53
40
|
end
|
54
41
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
def get_line_with_readline(question, highline, options={})
|
59
|
-
require "readline" # load only if needed
|
60
|
-
|
61
|
-
question_string = highline.render_statement(question)
|
62
|
-
|
63
|
-
raw_answer = readline_read(question_string, question)
|
64
|
-
|
65
|
-
if !raw_answer and highline.track_eof?
|
66
|
-
raise EOFError, "The input stream is exhausted."
|
67
|
-
end
|
68
|
-
|
69
|
-
raw_answer || ""
|
70
|
-
end
|
71
|
-
|
72
|
-
def readline_read(string, question)
|
73
|
-
# prep auto-completion
|
74
|
-
Readline.completion_proc = lambda do |string|
|
75
|
-
question.selection.grep(/\A#{Regexp.escape(string)}/)
|
42
|
+
# (see Terminal#get_character)
|
43
|
+
def get_character( input = STDIN )
|
44
|
+
input.getc
|
76
45
|
end
|
77
|
-
|
78
|
-
# work-around ugly readline() warnings
|
79
|
-
old_verbose = $VERBOSE
|
80
|
-
$VERBOSE = nil
|
81
|
-
raw_answer = Readline.readline(string, true)
|
82
|
-
|
83
|
-
$VERBOSE = old_verbose
|
84
|
-
|
85
|
-
raw_answer
|
86
|
-
end
|
87
|
-
|
88
|
-
def get_line_default(highline)
|
89
|
-
raise EOFError, "The input stream is exhausted." if highline.track_eof? and
|
90
|
-
highline.input.eof?
|
91
|
-
highline.input.gets
|
92
46
|
end
|
93
47
|
end
|
94
48
|
end
|
data/lib/highline/version.rb
CHANGED
data/lib/highline/wrapper.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
3
|
class HighLine
|
4
|
+
|
5
|
+
# A simple Wrapper module that is aware of ANSI escape codes.
|
6
|
+
# It compensates for the ANSI escape codes so it works on the
|
7
|
+
# actual (visual) line length.
|
4
8
|
module Wrapper
|
5
9
|
|
6
10
|
#
|
@@ -8,6 +12,8 @@ class HighLine
|
|
8
12
|
# newlines will not be affected by this process, but additional newlines
|
9
13
|
# may be added.
|
10
14
|
#
|
15
|
+
# @param text [String] text to be wrapped
|
16
|
+
# @param wrap_at [#to_i] column count to wrap the text into
|
11
17
|
def self.wrap(text, wrap_at)
|
12
18
|
return text unless wrap_at
|
13
19
|
wrap_at = Integer(wrap_at)
|
@@ -36,6 +42,9 @@ class HighLine
|
|
36
42
|
# Returns the length of the passed +string_with_escapes+, minus and color
|
37
43
|
# sequence escapes.
|
38
44
|
#
|
45
|
+
# @param string_with_escapes [String] any ANSI colored String
|
46
|
+
# @return [Integer] length based on the visual size of the String
|
47
|
+
# (without the escape codes)
|
39
48
|
def self.actual_length( string_with_escapes )
|
40
49
|
string_with_escapes.to_s.gsub(/\e\[\d{1,2}m/, "").length
|
41
50
|
end
|
data/test/test_highline.rb
CHANGED
@@ -959,7 +959,7 @@ class TestHighLine < Minitest::Test
|
|
959
959
|
end
|
960
960
|
|
961
961
|
def test_mode
|
962
|
-
assert(%w[
|
962
|
+
assert(%w[HighLine::Terminal::IOConsole HighLine::Terminal::NCurses HighLine::Terminal::UnixStty].include?(@terminal.terminal.character_mode),
|
963
963
|
"#{@terminal.terminal.character_mode} not in list")
|
964
964
|
end
|
965
965
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: highline
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0.pre.develop.
|
4
|
+
version: 2.0.0.pre.develop.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Edward Gray II
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: code_statistics
|
@@ -33,8 +33,7 @@ email: james@graysoftinc.com
|
|
33
33
|
executables: []
|
34
34
|
extensions: []
|
35
35
|
extra_rdoc_files:
|
36
|
-
- README.
|
37
|
-
- INSTALL
|
36
|
+
- README.md
|
38
37
|
- TODO
|
39
38
|
- Changelog.md
|
40
39
|
- LICENSE
|
@@ -46,9 +45,8 @@ files:
|
|
46
45
|
- COPYING
|
47
46
|
- Changelog.md
|
48
47
|
- Gemfile
|
49
|
-
- INSTALL
|
50
48
|
- LICENSE
|
51
|
-
- README.
|
49
|
+
- README.md
|
52
50
|
- Rakefile
|
53
51
|
- TODO
|
54
52
|
- appveyor.yml
|
@@ -92,7 +90,6 @@ files:
|
|
92
90
|
- lib/highline/terminal/unix_stty.rb
|
93
91
|
- lib/highline/version.rb
|
94
92
|
- lib/highline/wrapper.rb
|
95
|
-
- setup.rb
|
96
93
|
- site/.cvsignore
|
97
94
|
- site/highline.css
|
98
95
|
- site/images/logo.png
|
@@ -123,11 +120,7 @@ licenses:
|
|
123
120
|
- Ruby
|
124
121
|
metadata: {}
|
125
122
|
post_install_message:
|
126
|
-
rdoc_options:
|
127
|
-
- "--title"
|
128
|
-
- HighLine Documentation
|
129
|
-
- "--main"
|
130
|
-
- README
|
123
|
+
rdoc_options: []
|
131
124
|
require_paths:
|
132
125
|
- lib
|
133
126
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -142,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
135
|
version: 1.3.1
|
143
136
|
requirements: []
|
144
137
|
rubyforge_project: highline
|
145
|
-
rubygems_version: 2.4.
|
138
|
+
rubygems_version: 2.4.8
|
146
139
|
signing_key:
|
147
140
|
specification_version: 4
|
148
141
|
summary: HighLine is a high-level command-line IO library.
|
data/INSTALL
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
= Installing HighLine
|
2
|
-
|
3
|
-
RubyGems is the preferred easy install method for HighLine. However, you can
|
4
|
-
install HighLine manually as described below.
|
5
|
-
|
6
|
-
== Requirements
|
7
|
-
|
8
|
-
HighLine from version >= 1.7.0 requires ruby >= 1.9.3
|
9
|
-
|
10
|
-
== Installing the Gem
|
11
|
-
|
12
|
-
HighLine is intended to be installed via the
|
13
|
-
RubyGems[http://rubyforge.org/projects/rubygems/] system. To get the latest
|
14
|
-
version, simply enter the following into your command prompt:
|
15
|
-
|
16
|
-
$ sudo gem install highline
|
17
|
-
|
18
|
-
You must have RubyGems[http://rubyforge.org/projects/rubygems/] installed for
|
19
|
-
the above to work.
|
20
|
-
|
21
|
-
If you want to build the gem locally, make sure you have
|
22
|
-
Rake[http://rubyforge.org/projects/rake/] installed then run the following
|
23
|
-
command:
|
24
|
-
|
25
|
-
$ rake package
|
26
|
-
|
27
|
-
== Installing Manually
|
28
|
-
|
29
|
-
Download the latest version of HighLine from the
|
30
|
-
{RubyForge project page}[http://rubyforge.org/frs/?group_id=683]. Navigate to
|
31
|
-
the root project directory and enter:
|
32
|
-
|
33
|
-
$ sudo ruby setup.rb
|
34
|
-
|
35
|
-
== Installing HighLine on JRuby
|
36
|
-
|
37
|
-
If you are using HighLine on JRuby, many features will not work properly
|
38
|
-
without a working ncurses installation. First, ensure that you have
|
39
|
-
ncurses installed and then install the ffi-ncurses gem.
|
40
|
-
|
41
|
-
If ffi-ncurses fails to find your ncurses library, you may need to set the
|
42
|
-
RUBY_FFI_NCURSES envirionment variable, i.e:
|
43
|
-
|
44
|
-
RUBY_FFI_NCURSES_LIB=ncursesw ruby examples/hello.rb
|
45
|
-
|
46
|
-
For details, see the ffi-ncurses documentation at:
|
47
|
-
http://github.com/seanohalpin/ffi-ncurses
|
48
|
-
|
49
|
-
== Using termios
|
50
|
-
|
51
|
-
While not a requirement, HighLine will take advantage of the termios library if
|
52
|
-
installed (on Unix). This slightly improves HighLine's character reading
|
53
|
-
capabilities and thus is recommended for all Unix users.
|
54
|
-
|
55
|
-
If using the HighLine gem, you should be able to add termios as easily as:
|
56
|
-
|
57
|
-
$ sudo gem install termios
|
58
|
-
|
59
|
-
For manual installs, consult the termios documentation.
|
data/README.rdoc
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
= HighLine
|
2
|
-
|
3
|
-
by James Edward Gray II
|
4
|
-
|
5
|
-
{<img src="https://travis-ci.org/JEG2/highline.svg" alt="Build Status" />}[https://travis-ci.org/JEG2/highline]
|
6
|
-
{<img src="https://ci.appveyor.com/api/projects/status/4p05fijpah77d28x?svg=true" alt="AppVeyor Build Status" />}[https://ci.appveyor.com/project/abinoam/highline]
|
7
|
-
{<img src="https://img.shields.io/gem/v/highline.svg?style=flat" />}[http://rubygems.org/gems/highline]
|
8
|
-
{<img src="https://codeclimate.com/github/JEG2/highline/badges/gpa.svg" />}[https://codeclimate.com/github/JEG2/highline]
|
9
|
-
{<img src="https://codeclimate.com/github/JEG2/highline/badges/coverage.svg" />}[https://codeclimate.com/github/JEG2/highline/coverage]
|
10
|
-
|
11
|
-
== Description
|
12
|
-
|
13
|
-
Welcome to HighLine.
|
14
|
-
|
15
|
-
HighLine was designed to ease the tedious tasks of doing console input and
|
16
|
-
output with low-level methods like gets() and puts(). HighLine provides a
|
17
|
-
robust system for requesting data from a user, without needing to code all the
|
18
|
-
error checking and validation rules and without needing to convert the typed
|
19
|
-
Strings into what your program really needs. Just tell HighLine what you're
|
20
|
-
after, and let it do all the work.
|
21
|
-
|
22
|
-
== Documentation
|
23
|
-
|
24
|
-
See HighLine and HighLine::Question for documentation.
|
25
|
-
|
26
|
-
Start hacking in your code with HighLine with:
|
27
|
-
|
28
|
-
require 'highline/import'
|
29
|
-
|
30
|
-
== Examples
|
31
|
-
|
32
|
-
Basic usage:
|
33
|
-
|
34
|
-
ask("Company? ") { |q| q.default = "none" }
|
35
|
-
|
36
|
-
Validation:
|
37
|
-
|
38
|
-
ask("Age? ", Integer) { |q| q.in = 0..105 }
|
39
|
-
ask("Name? (last, first) ") { |q| q.validate = /\A\w+, ?\w+\Z/ }
|
40
|
-
|
41
|
-
Type conversion for answers:
|
42
|
-
|
43
|
-
ask("Birthday? ", Date)
|
44
|
-
ask("Interests? (comma sep list) ", lambda { |str| str.split(/,\s*/) })
|
45
|
-
|
46
|
-
Reading passwords:
|
47
|
-
|
48
|
-
ask("Enter your password: ") { |q| q.echo = false }
|
49
|
-
ask("Enter your password: ") { |q| q.echo = "x" }
|
50
|
-
|
51
|
-
ERb based output (with HighLine's ANSI color tools):
|
52
|
-
|
53
|
-
say("This should be <%= color('bold', BOLD) %>!")
|
54
|
-
|
55
|
-
Menus:
|
56
|
-
|
57
|
-
choose do |menu|
|
58
|
-
menu.prompt = "Please choose your favorite programming language? "
|
59
|
-
|
60
|
-
menu.choice(:ruby) { say("Good choice!") }
|
61
|
-
menu.choices(:python, :perl) { say("Not from around here, are you?") }
|
62
|
-
end
|
63
|
-
|
64
|
-
For more examples see the examples/ directory of this project.
|
65
|
-
|
66
|
-
== Requirements
|
67
|
-
|
68
|
-
HighLine from version >= 1.7.0 requires ruby >= 1.9.3
|
69
|
-
|
70
|
-
== Installing
|
71
|
-
|
72
|
-
See the INSTALL file for instructions.
|
73
|
-
|
74
|
-
== Questions and/or Comments
|
75
|
-
|
76
|
-
Feel free to email {James Edward Gray II}[mailto:james@grayproductions.net] or
|
77
|
-
{Gregory Brown}[mailto:gregory.t.brown@gmail.com] with any questions.
|