highline 2.0.0.pre.develop.9 → 2.0.0.pre.develop.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +59 -5
- data/.travis.yml +9 -4
- data/Changelog.md +11 -0
- data/Gemfile +12 -19
- data/Rakefile +5 -11
- data/examples/ansi_colors.rb +6 -11
- data/examples/asking_for_arrays.rb +4 -3
- data/examples/basic_usage.rb +29 -22
- data/examples/color_scheme.rb +11 -10
- data/examples/get_character.rb +6 -5
- data/examples/limit.rb +2 -1
- data/examples/menus.rb +11 -11
- data/examples/overwrite.rb +7 -6
- data/examples/page_and_wrap.rb +5 -4
- data/examples/password.rb +2 -1
- data/examples/repeat_entry.rb +7 -5
- data/examples/trapping_eof.rb +2 -1
- data/examples/using_readline.rb +2 -1
- data/highline.gemspec +25 -25
- data/lib/highline.rb +103 -111
- data/lib/highline/builtin_styles.rb +45 -41
- data/lib/highline/color_scheme.rb +32 -28
- data/lib/highline/compatibility.rb +3 -3
- data/lib/highline/custom_errors.rb +2 -1
- data/lib/highline/import.rb +8 -11
- data/lib/highline/list.rb +4 -8
- data/lib/highline/list_renderer.rb +207 -201
- data/lib/highline/menu.rb +75 -63
- data/lib/highline/menu/item.rb +2 -0
- data/lib/highline/paginator.rb +5 -6
- data/lib/highline/question.rb +38 -36
- data/lib/highline/question/answer_converter.rb +2 -2
- data/lib/highline/question_asker.rb +15 -17
- data/lib/highline/simulate.rb +11 -13
- data/lib/highline/statement.rb +12 -10
- data/lib/highline/string.rb +9 -8
- data/lib/highline/string_extensions.rb +30 -14
- data/lib/highline/style.rb +68 -45
- data/lib/highline/template_renderer.rb +5 -5
- data/lib/highline/terminal.rb +24 -31
- data/lib/highline/terminal/io_console.rb +2 -2
- data/lib/highline/terminal/ncurses.rb +4 -3
- data/lib/highline/terminal/unix_stty.rb +12 -9
- data/lib/highline/version.rb +1 -1
- data/lib/highline/wrapper.rb +12 -11
- metadata +34 -43
- data/test/acceptance/acceptance.rb +0 -62
- data/test/acceptance/acceptance_test.rb +0 -69
- data/test/acceptance/at_color_output_using_erb_templates.rb +0 -17
- data/test/acceptance/at_echo_false.rb +0 -23
- data/test/acceptance/at_readline.rb +0 -37
- data/test/io_console_compatible.rb +0 -37
- data/test/string_methods.rb +0 -35
- data/test/test_answer_converter.rb +0 -26
- data/test/test_color_scheme.rb +0 -94
- data/test/test_helper.rb +0 -22
- data/test/test_highline.rb +0 -1627
- data/test/test_import.rb +0 -55
- data/test/test_list.rb +0 -60
- data/test/test_menu.rb +0 -749
- data/test/test_paginator.rb +0 -73
- data/test/test_question_asker.rb +0 -20
- data/test/test_simulator.rb +0 -24
- data/test/test_string_extension.rb +0 -72
- data/test/test_string_highline.rb +0 -42
- data/test/test_style.rb +0 -613
- data/test/test_wrapper.rb +0 -188
data/examples/menus.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
2
3
|
|
3
4
|
require "rubygems"
|
4
5
|
require "highline/import"
|
5
6
|
|
6
|
-
puts "Using: #{
|
7
|
+
puts "Using: #{HighLine.default_instance.terminal.class}"
|
7
8
|
puts
|
8
9
|
|
9
10
|
# The old way, using ask() and say()...
|
10
|
-
choices = %w
|
11
|
+
choices = %w[ruby python perl]
|
11
12
|
say("This is the old way using ask() and say()...")
|
12
13
|
say("Please choose your favorite programming language:")
|
13
14
|
say(choices.map { |c| " #{c}\n" }.join)
|
@@ -24,11 +25,10 @@ say("\nThis is the new mode (default)...")
|
|
24
25
|
choose do |menu|
|
25
26
|
menu.prompt = "Please choose your favorite programming language? "
|
26
27
|
|
27
|
-
menu.choice
|
28
|
-
menu.choices(:python, :perl)
|
28
|
+
menu.choice(:ruby) { say("Good choice!") }
|
29
|
+
menu.choices(:python, :perl) { say("Not from around here, are you?") }
|
29
30
|
|
30
31
|
menu.default = :ruby
|
31
|
-
|
32
32
|
end
|
33
33
|
|
34
34
|
say("\nThis is letter indexing...")
|
@@ -38,8 +38,8 @@ choose do |menu|
|
|
38
38
|
|
39
39
|
menu.prompt = "Please choose your favorite programming language? "
|
40
40
|
|
41
|
-
menu.choice
|
42
|
-
menu.choices(:python, :perl)
|
41
|
+
menu.choice(:ruby) { say("Good choice!") }
|
42
|
+
menu.choices(:python, :perl) { say("Not from around here, are you?") }
|
43
43
|
end
|
44
44
|
|
45
45
|
say("\nThis is with a different layout...")
|
@@ -49,8 +49,8 @@ choose do |menu|
|
|
49
49
|
menu.header = "Languages"
|
50
50
|
menu.prompt = "Favorite? "
|
51
51
|
|
52
|
-
menu.choice
|
53
|
-
menu.choices(:python, :perl)
|
52
|
+
menu.choice(:ruby) { say("Good choice!") }
|
53
|
+
menu.choices(:python, :perl) { say("Not from around here, are you?") }
|
54
54
|
end
|
55
55
|
|
56
56
|
say("\nYou can even build shells...")
|
@@ -60,10 +60,10 @@ loop do
|
|
60
60
|
|
61
61
|
menu.shell = true
|
62
62
|
|
63
|
-
menu.choice(:load, "Load a file.") do |
|
63
|
+
menu.choice(:load, "Load a file.") do |_command, details|
|
64
64
|
say("Loading file with options: #{details}...")
|
65
65
|
end
|
66
|
-
menu.choice(:save, "Save a file.") do |
|
66
|
+
menu.choice(:save, "Save a file.") do |_command, details|
|
67
67
|
say("Saving file with options: #{details}...")
|
68
68
|
end
|
69
69
|
menu.choice(:quit, "Exit program.") { exit }
|
data/examples/overwrite.rb
CHANGED
@@ -1,22 +1,23 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
2
3
|
|
3
4
|
# overwrite.rb
|
4
5
|
#
|
5
6
|
# Created by Jeremy Hinegardner on 2007-01-24
|
6
7
|
# Copyright 2007 Jeremy Hinegardner. All rights reserved
|
7
8
|
|
8
|
-
require
|
9
|
-
require
|
9
|
+
require "rubygems"
|
10
|
+
require "highline/import"
|
10
11
|
|
11
|
-
puts "Using: #{
|
12
|
+
puts "Using: #{HighLine.default_instance.terminal.class}"
|
12
13
|
puts
|
13
14
|
|
14
15
|
prompt = "here is your password:"
|
15
16
|
ask(
|
16
17
|
"#{prompt} <%= color('mypassword', RED, BOLD) %> (Press Any Key to blank) "
|
17
18
|
) do |q|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
q.overwrite = true
|
20
|
+
q.echo = false # overwrite works best when echo is false.
|
21
|
+
q.character = true # if this is set to :getc then overwrite does not work
|
21
22
|
end
|
22
23
|
say("<%= color('Look! blanked out!', GREEN) %>")
|
data/examples/page_and_wrap.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
2
3
|
|
3
4
|
# page_and_wrap.rb
|
4
5
|
#
|
@@ -8,10 +9,10 @@
|
|
8
9
|
require "rubygems"
|
9
10
|
require "highline/import"
|
10
11
|
|
11
|
-
|
12
|
-
|
12
|
+
HighLine.default_instance.wrap_at = 80
|
13
|
+
HighLine.default_instance.page_at = 22
|
13
14
|
|
14
|
-
say(<<
|
15
|
+
say(<<CONSTITUTION)
|
15
16
|
THE UNITED STATES CONSTITUTION
|
16
17
|
|
17
18
|
We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.
|
@@ -319,4 +320,4 @@ James Wilson.
|
|
319
320
|
Gouv Morris
|
320
321
|
|
321
322
|
Attest William Jackson Secretary
|
322
|
-
|
323
|
+
CONSTITUTION
|
data/examples/password.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
2
3
|
|
3
4
|
require "rubygems"
|
4
5
|
require "highline/import"
|
5
6
|
|
6
|
-
puts "Using: #{
|
7
|
+
puts "Using: #{HighLine.default_instance.terminal.class}"
|
7
8
|
puts
|
8
9
|
|
9
10
|
pass = ask("Enter your password: ") { |q| q.echo = false }
|
data/examples/repeat_entry.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
2
3
|
|
3
4
|
require "rubygems"
|
4
5
|
require "highline/import"
|
5
6
|
|
6
|
-
puts "Using: #{
|
7
|
+
puts "Using: #{HighLine.default_instance.terminal.class}"
|
7
8
|
puts
|
8
9
|
|
9
|
-
tounge_twister
|
10
|
+
# tounge_twister
|
11
|
+
ask("... try saying that three times fast") do |q|
|
10
12
|
q.gather = 3
|
11
13
|
q.verify_match = true
|
12
14
|
q.responses[:mismatch] = "Nope, those don't match. Try again."
|
@@ -15,10 +17,10 @@ end
|
|
15
17
|
puts "Ok, you did it."
|
16
18
|
|
17
19
|
pass = ask("<%= key %>: ") do |q|
|
18
|
-
q.echo =
|
20
|
+
q.echo = "*"
|
19
21
|
q.verify_match = true
|
20
|
-
q.gather = {"Enter a password" =>
|
21
|
-
|
22
|
+
q.gather = { "Enter a password" => "",
|
23
|
+
"Please type it again for verification" => "" }
|
22
24
|
end
|
23
25
|
|
24
26
|
puts "Your password is now #{pass}!"
|
data/examples/trapping_eof.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
2
3
|
|
3
4
|
# trapping_eof.rb
|
4
5
|
#
|
@@ -13,7 +14,7 @@ loop do
|
|
13
14
|
name = ask("What's your name?")
|
14
15
|
break if name == "exit"
|
15
16
|
puts "Hello, #{name}!"
|
16
|
-
rescue EOFError
|
17
|
+
rescue EOFError # HighLine throws this if @input.eof?
|
17
18
|
break
|
18
19
|
end
|
19
20
|
end
|
data/examples/using_readline.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
2
3
|
|
3
4
|
# using_readline.rb
|
4
5
|
#
|
@@ -9,7 +10,7 @@ require "rubygems"
|
|
9
10
|
require "highline/import"
|
10
11
|
|
11
12
|
loop do
|
12
|
-
cmd = ask("Enter command: ", %w
|
13
|
+
cmd = ask("Enter command: ", %w[save sample load reset quit]) do |q|
|
13
14
|
q.readline = true
|
14
15
|
end
|
15
16
|
say("Executing \"#{cmd}\"...")
|
data/highline.gemspec
CHANGED
@@ -1,35 +1,35 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'highline/version'
|
5
|
-
|
6
|
-
GEM_VERSION = HighLine::VERSION
|
7
2
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
spec.platform = Gem::Platform::RUBY
|
12
|
-
spec.summary = "HighLine is a high-level command-line IO library."
|
13
|
-
spec.files = `git ls-files`.split("\n")
|
14
|
-
|
15
|
-
spec.test_files = `git ls-files -- test/*.rb`.split("\n")
|
16
|
-
spec.has_rdoc = 'yard'
|
17
|
-
spec.extra_rdoc_files = %w[README.md TODO Changelog.md LICENSE]
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "highline/version"
|
18
6
|
|
19
|
-
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "highline"
|
9
|
+
spec.version = HighLine::VERSION
|
10
|
+
spec.author = "James Edward Gray II"
|
11
|
+
spec.email = "james@graysoftinc.com"
|
20
12
|
|
21
|
-
spec.
|
22
|
-
spec.
|
23
|
-
spec.rubyforge_project = "highline"
|
24
|
-
spec.homepage = "https://github.com/JEG2/highline"
|
25
|
-
spec.license = "Ruby"
|
26
|
-
spec.description = <<END_DESC
|
13
|
+
spec.summary = "HighLine is a high-level command-line IO library."
|
14
|
+
spec.description = <<DESCRIPTION
|
27
15
|
A high-level IO library that provides validation, type conversion, and more for
|
28
16
|
command-line interfaces. HighLine also includes a complete menu system that can
|
29
17
|
crank out anything from simple list selection to complete shells with just
|
30
18
|
minutes of work.
|
31
|
-
|
19
|
+
DESCRIPTION
|
20
|
+
spec.homepage = "https://github.com/JEG2/highline"
|
21
|
+
spec.license = "Ruby"
|
22
|
+
|
23
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
24
|
+
f.match(%r{^(test|spec|features)/})
|
25
|
+
end
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.extra_rdoc_files = %w[README.md TODO Changelog.md LICENSE]
|
29
|
+
|
30
|
+
spec.required_ruby_version = ">= 1.9.3"
|
32
31
|
|
33
|
-
spec.add_development_dependency "
|
34
|
-
spec.
|
32
|
+
spec.add_development_dependency "bundler"
|
33
|
+
spec.add_development_dependency "rake"
|
34
|
+
spec.add_development_dependency "minitest"
|
35
35
|
end
|
data/lib/highline.rb
CHANGED
@@ -10,6 +10,7 @@
|
|
10
10
|
#
|
11
11
|
# This is Free Software. See LICENSE and COPYING for details.
|
12
12
|
|
13
|
+
require "English"
|
13
14
|
require "erb"
|
14
15
|
require "optparse"
|
15
16
|
require "stringio"
|
@@ -29,90 +30,64 @@ require "highline/builtin_styles"
|
|
29
30
|
#
|
30
31
|
# A HighLine object is a "high-level line oriented" shell over an input and an
|
31
32
|
# output stream. HighLine simplifies common console interaction, effectively
|
32
|
-
# replacing {Kernel#puts} and {Kernel#gets}. User code can simply specify the
|
33
|
-
# and any details about user interaction, then leave the rest
|
34
|
-
# HighLine. When {HighLine#ask} returns, you'll have the answer
|
35
|
-
# even if HighLine had to ask many times, validate results,
|
36
|
-
# checking, convert types, etc.
|
33
|
+
# replacing {Kernel#puts} and {Kernel#gets}. User code can simply specify the
|
34
|
+
# question to ask and any details about user interaction, then leave the rest
|
35
|
+
# of the work to HighLine. When {HighLine#ask} returns, you'll have the answer
|
36
|
+
# you requested, even if HighLine had to ask many times, validate results,
|
37
|
+
# perform range checking, convert types, etc.
|
37
38
|
#
|
38
39
|
# @example Basic usage
|
39
40
|
# cli = HighLine.new
|
40
41
|
# answer = cli.ask "What do you think?"
|
41
42
|
# puts "You have answered: #{answer}"
|
42
|
-
|
43
|
+
#
|
43
44
|
class HighLine
|
44
45
|
include BuiltinStyles
|
45
46
|
include CustomErrors
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
@use_color
|
53
|
-
end
|
48
|
+
extend SingleForwardable
|
49
|
+
def_single_delegators :@default_instance, :agree, :ask, :choose, :say,
|
50
|
+
:use_color=, :use_color?, :reset_use_color,
|
51
|
+
:track_eof=, :track_eof?,
|
52
|
+
:color, :uncolor, :color_code
|
54
53
|
|
55
|
-
|
56
|
-
|
57
|
-
@use_color = true
|
58
|
-
end
|
54
|
+
class << self
|
55
|
+
attr_accessor :default_instance
|
59
56
|
|
60
|
-
|
61
|
-
|
62
|
-
# Note: color usage also depends on HighLine.use_color being set
|
63
|
-
def self.supports_rgb_color?
|
64
|
-
true
|
65
|
-
end
|
57
|
+
# Pass ColorScheme to set a HighLine color scheme.
|
58
|
+
attr_accessor :color_scheme
|
66
59
|
|
67
|
-
|
68
|
-
|
60
|
+
# Returns +true+ if HighLine is currently using a color scheme.
|
61
|
+
def using_color_scheme?
|
62
|
+
true if @color_scheme
|
63
|
+
end
|
69
64
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
65
|
+
# Reset color scheme to default (+nil+)
|
66
|
+
def reset_color_scheme
|
67
|
+
self.color_scheme = nil
|
68
|
+
end
|
74
69
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
70
|
+
# Reset HighLine to default.
|
71
|
+
# Clears Style index and resets color_scheme and use_color settings.
|
72
|
+
def reset
|
73
|
+
Style.clear_index
|
74
|
+
reset_color_scheme
|
75
|
+
reset_use_color
|
76
|
+
end
|
79
77
|
|
80
|
-
|
81
|
-
|
82
|
-
|
78
|
+
# For checking if the current version of HighLine supports RGB colors
|
79
|
+
# Usage: HighLine.supports_rgb_color? rescue false
|
80
|
+
# using rescue for compatibility with older versions
|
81
|
+
# Note: color usage also depends on HighLine.use_color being set
|
82
|
+
# TODO: Discuss removing this method
|
83
|
+
def supports_rgb_color?
|
84
|
+
true
|
85
|
+
end
|
83
86
|
end
|
84
87
|
|
85
88
|
# The setting used to control color schemes.
|
86
89
|
@color_scheme = nil
|
87
90
|
|
88
|
-
# Pass ColorScheme to _setting_ to set a HighLine color scheme.
|
89
|
-
def self.color_scheme=( setting )
|
90
|
-
@color_scheme = setting
|
91
|
-
end
|
92
|
-
|
93
|
-
# Returns the current color scheme.
|
94
|
-
def self.color_scheme
|
95
|
-
@color_scheme
|
96
|
-
end
|
97
|
-
|
98
|
-
# Returns +true+ if HighLine is currently using a color scheme.
|
99
|
-
def self.using_color_scheme?
|
100
|
-
!!@color_scheme
|
101
|
-
end
|
102
|
-
|
103
|
-
# Reset HighLine to default.
|
104
|
-
# Clears Style index and resets color_scheme and use_color settings.
|
105
|
-
def self.reset
|
106
|
-
Style.clear_index
|
107
|
-
reset_color_scheme
|
108
|
-
reset_use_color
|
109
|
-
end
|
110
|
-
|
111
|
-
# Reset color scheme to default (+nil+)
|
112
|
-
def self.reset_color_scheme
|
113
|
-
self.color_scheme = nil
|
114
|
-
end
|
115
|
-
|
116
91
|
#
|
117
92
|
# Create an instance of HighLine connected to the given _input_
|
118
93
|
# and _output_ streams.
|
@@ -124,8 +99,9 @@ class HighLine
|
|
124
99
|
# @param page_at [Integer] page size and paginating.
|
125
100
|
# @param indent_size [Integer] indentation size in spaces.
|
126
101
|
# @param indent_level [Integer] how deep is indentated.
|
127
|
-
def initialize(
|
128
|
-
|
102
|
+
def initialize(input = $stdin, output = $stdout,
|
103
|
+
wrap_at = nil, page_at = nil,
|
104
|
+
indent_size = 3, indent_level = 0)
|
129
105
|
@input = input
|
130
106
|
@output = output
|
131
107
|
|
@@ -140,10 +116,32 @@ class HighLine
|
|
140
116
|
@prompt = nil
|
141
117
|
@key = nil
|
142
118
|
@use_color = true
|
119
|
+
@track_eof = true # The setting used to disable EOF tracking.
|
143
120
|
|
144
121
|
@terminal = HighLine::Terminal.get_terminal(input, output)
|
145
122
|
end
|
146
123
|
|
124
|
+
# Set it to false to disable ANSI coloring
|
125
|
+
attr_accessor :use_color
|
126
|
+
|
127
|
+
# Returns truethy if HighLine instance is currently using color escapes.
|
128
|
+
def use_color?
|
129
|
+
use_color
|
130
|
+
end
|
131
|
+
|
132
|
+
# Resets the use of color.
|
133
|
+
def reset_use_color
|
134
|
+
@use_color = true
|
135
|
+
end
|
136
|
+
|
137
|
+
# Pass +false+ to turn off HighLine's EOF tracking.
|
138
|
+
attr_accessor :track_eof
|
139
|
+
|
140
|
+
# Returns true if HighLine is currently tracking EOF for input.
|
141
|
+
def track_eof?
|
142
|
+
true if track_eof
|
143
|
+
end
|
144
|
+
|
147
145
|
# @return [Integer] The current column setting for wrapping output.
|
148
146
|
attr_reader :wrap_at
|
149
147
|
|
@@ -187,11 +185,13 @@ class HighLine
|
|
187
185
|
#
|
188
186
|
# Raises EOFError if input is exhausted.
|
189
187
|
#
|
190
|
-
# @param yes_or_no_question [String] a question that accepts yes and no as
|
191
|
-
#
|
188
|
+
# @param yes_or_no_question [String] a question that accepts yes and no as
|
189
|
+
# answers
|
190
|
+
# @param character [Boolean, :getc] character mode to be passed to
|
191
|
+
# Question#character
|
192
192
|
# @see Question#character
|
193
|
-
def agree(
|
194
|
-
ask(yes_or_no_question,
|
193
|
+
def agree(yes_or_no_question, character = nil)
|
194
|
+
ask(yes_or_no_question, ->(yn) { yn.downcase[0] == "y" }) do |q|
|
195
195
|
q.validate = /\A(?:y(?:es)?|no?)\Z/i
|
196
196
|
q.responses[:not_valid] = 'Please enter "yes" or "no".'
|
197
197
|
q.responses[:ask_on_error] = :question
|
@@ -241,7 +241,7 @@ class HighLine
|
|
241
241
|
# @param items [Array<String>]
|
242
242
|
# @param details [Proc] to be passed to Menu.new
|
243
243
|
# @return [String] answer
|
244
|
-
def choose(
|
244
|
+
def choose(*items, &details)
|
245
245
|
menu = Menu.new(&details)
|
246
246
|
menu.choices(*items) unless items.empty?
|
247
247
|
|
@@ -273,7 +273,7 @@ class HighLine
|
|
273
273
|
# @return [lambda] lambda to be used in autocompletion operations
|
274
274
|
|
275
275
|
def shell_style_lambda(menu)
|
276
|
-
lambda do |command|
|
276
|
+
lambda do |command| # shell-style selection
|
277
277
|
first_word = command.to_s.split.first || ""
|
278
278
|
|
279
279
|
options = menu.options
|
@@ -294,23 +294,20 @@ class HighLine
|
|
294
294
|
# (:blue for BLUE, for example). A CLEAR will automatically be embedded to
|
295
295
|
# the end of the returned String.
|
296
296
|
#
|
297
|
-
# This method returns the original _string_ unchanged if
|
297
|
+
# This method returns the original _string_ unchanged if use_color?
|
298
298
|
# is +false+.
|
299
299
|
#
|
300
300
|
# @param string [String] string to be colored
|
301
301
|
# @param colors [Array<Symbol>] array of colors like [:red, :blue]
|
302
302
|
# @return [String] (ANSI escaped) colored string
|
303
303
|
# @example
|
304
|
-
# HighLine.
|
304
|
+
# cli = HighLine.new
|
305
|
+
# cli.color("Sustainable", :green, :bold)
|
305
306
|
# # => "\e[32m\e[1mSustainable\e[0m"
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
# (see .color)
|
312
|
-
# This method is a clone of the HighLine.color class method.
|
313
|
-
# But it checks for use_color? per instance
|
307
|
+
#
|
308
|
+
# # As class method (delegating to HighLine.default_instance)
|
309
|
+
# HighLine.color("Sustainable", :green, :bold)
|
310
|
+
#
|
314
311
|
def color(string, *colors)
|
315
312
|
return string unless use_color?
|
316
313
|
HighLine.Style(*colors).color(string)
|
@@ -327,29 +324,19 @@ class HighLine
|
|
327
324
|
# s.code # => "\e[31m\e[34m"
|
328
325
|
#
|
329
326
|
# HighLine.color_code(:red, :blue) # => "\e[31m\e[34m"
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
# (see HighLine.color_code)
|
336
|
-
# Convenience instance method. It delegates to the class method.
|
327
|
+
#
|
328
|
+
# cli = HighLine.new
|
329
|
+
# cli.color_code(:red, :blue) # => "\e[31m\e[34m"
|
330
|
+
#
|
337
331
|
def color_code(*colors)
|
338
|
-
|
332
|
+
HighLine.Style(*colors).code
|
339
333
|
end
|
340
334
|
|
341
335
|
# Remove color codes from a string.
|
342
336
|
# @param string [String] to be decolorized
|
343
337
|
# @return [String] without the ANSI escape sequence (colors)
|
344
|
-
def self.uncolor(string)
|
345
|
-
Style.uncolor(string)
|
346
|
-
end
|
347
|
-
|
348
|
-
# (see .uncolor)
|
349
|
-
# Convenience instance method. It delegates to the class method.
|
350
|
-
|
351
338
|
def uncolor(string)
|
352
|
-
|
339
|
+
Style.uncolor(string)
|
353
340
|
end
|
354
341
|
|
355
342
|
# Renders a list of itens using a {ListRenderer}
|
@@ -377,7 +364,7 @@ class HighLine
|
|
377
364
|
statement = render_statement(statement)
|
378
365
|
return if statement.empty?
|
379
366
|
|
380
|
-
statement = (indentation+statement)
|
367
|
+
statement = (indentation + statement)
|
381
368
|
|
382
369
|
# Don't add a newline if statement ends with whitespace, OR
|
383
370
|
# if statement ends with whitespace before a color escape code.
|
@@ -402,7 +389,7 @@ class HighLine
|
|
402
389
|
# set to <tt>:auto</tt>, HighLine will attempt to determine the columns
|
403
390
|
# available for the <tt>@output</tt> or use a sensible default.
|
404
391
|
#
|
405
|
-
def wrap_at=(
|
392
|
+
def wrap_at=(setting)
|
406
393
|
@wrap_at = setting == :auto ? output_cols : setting
|
407
394
|
end
|
408
395
|
|
@@ -412,7 +399,7 @@ class HighLine
|
|
412
399
|
# set to <tt>:auto</tt>, HighLine will attempt to determine the rows available
|
413
400
|
# for the <tt>@output</tt> or use a sensible default.
|
414
401
|
#
|
415
|
-
def page_at=(
|
402
|
+
def page_at=(setting)
|
416
403
|
@page_at = setting == :auto ? output_rows - 2 : setting
|
417
404
|
end
|
418
405
|
|
@@ -420,7 +407,7 @@ class HighLine
|
|
420
407
|
# Outputs indentation with current settings
|
421
408
|
#
|
422
409
|
def indentation
|
423
|
-
|
410
|
+
" " * @indent_size * @indent_level
|
424
411
|
end
|
425
412
|
|
426
413
|
#
|
@@ -431,7 +418,7 @@ class HighLine
|
|
431
418
|
# @param multiline [Boolean]
|
432
419
|
# @return [void]
|
433
420
|
# @see #multi_indent
|
434
|
-
def indent(increase=1, statement=nil, multiline=nil)
|
421
|
+
def indent(increase = 1, statement = nil, multiline = nil)
|
435
422
|
@indent_level += increase
|
436
423
|
multi = @multi_indent
|
437
424
|
@multi_indent ||= multiline
|
@@ -461,7 +448,7 @@ class HighLine
|
|
461
448
|
def output_cols
|
462
449
|
return 80 unless @output.tty?
|
463
450
|
terminal.terminal_size.first
|
464
|
-
rescue
|
451
|
+
rescue NoMethodError
|
465
452
|
return 80
|
466
453
|
end
|
467
454
|
|
@@ -472,7 +459,7 @@ class HighLine
|
|
472
459
|
def output_rows
|
473
460
|
return 24 unless @output.tty?
|
474
461
|
terminal.terminal_size.last
|
475
|
-
rescue
|
462
|
+
rescue NoMethodError
|
476
463
|
return 24
|
477
464
|
end
|
478
465
|
|
@@ -486,7 +473,8 @@ class HighLine
|
|
486
473
|
# Creates a new HighLine instance with the same options
|
487
474
|
#
|
488
475
|
def new_scope
|
489
|
-
self.class.new(@input, @output, @wrap_at,
|
476
|
+
self.class.new(@input, @output, @wrap_at,
|
477
|
+
@page_at, @indent_size, @indent_level)
|
490
478
|
end
|
491
479
|
|
492
480
|
private
|
@@ -523,7 +511,7 @@ class HighLine
|
|
523
511
|
# @param question [Question]
|
524
512
|
# @return [String] response
|
525
513
|
def get_response_line_mode(question)
|
526
|
-
if question.echo == true
|
514
|
+
if question.echo == true && !question.limit
|
527
515
|
get_line(question)
|
528
516
|
else
|
529
517
|
get_line_raw_no_echo_mode(question)
|
@@ -547,13 +535,15 @@ class HighLine
|
|
547
535
|
line = ""
|
548
536
|
|
549
537
|
terminal.raw_no_echo_mode_exec do
|
550
|
-
|
538
|
+
loop do
|
539
|
+
character = terminal.get_character
|
540
|
+
break unless character
|
551
541
|
break if ["\n", "\r"].include? character
|
552
542
|
|
553
543
|
# honor backspace and delete
|
554
544
|
if character == "\b"
|
555
545
|
chopped = line.chop!
|
556
|
-
output_erase_char if chopped
|
546
|
+
output_erase_char if chopped && question.echo
|
557
547
|
else
|
558
548
|
line << character
|
559
549
|
say_last_char_or_echo_char(line, question)
|
@@ -581,11 +571,11 @@ class HighLine
|
|
581
571
|
|
582
572
|
def say_last_char_or_echo_char(line, question)
|
583
573
|
@output.print(line[-1]) if question.echo == true
|
584
|
-
@output.print(question.echo) if question.echo
|
574
|
+
@output.print(question.echo) if question.echo && question.echo != true
|
585
575
|
end
|
586
576
|
|
587
577
|
def line_overflow_for_question?(line, question)
|
588
|
-
question.limit
|
578
|
+
question.limit && line.size == question.limit
|
589
579
|
end
|
590
580
|
|
591
581
|
def output_erase_char
|
@@ -631,4 +621,6 @@ class HighLine
|
|
631
621
|
end
|
632
622
|
end
|
633
623
|
|
624
|
+
HighLine.default_instance = HighLine.new
|
625
|
+
|
634
626
|
require "highline/string"
|