highline 1.2.5 → 1.2.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  Below is a complete listing of changes for each revision of HighLine.
4
4
 
5
+ == 1.2.6
6
+
7
+ Patch by Jeremy Hinegardner:
8
+
9
+ * Added ColorScheme support.
10
+ * Added HighLine::Question.overwrite mode.
11
+ * Various documentation fixes.
12
+
5
13
  == 1.2.5
6
14
 
7
15
  * Really fixed the bug I tried to fix in 1.2.4.
data/README CHANGED
@@ -60,4 +60,4 @@ See the INSTALL file for instructions.
60
60
  == Questions and/or Comments
61
61
 
62
62
  Feel free to email {James Edward Gray II}[mailto:james@grayproductions.net] or
63
- {Grregory Brown}[mailto:gregory.t.brown@gmail.com] with any questions.
63
+ {Gregory Brown}[mailto:gregory.t.brown@gmail.com] with any questions.
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ # color_scheme.rb
4
+ #
5
+ # Created by Jeremy Hinegardner on 2007-01-24
6
+ # Copyright 2007 Jeremy Hinegardner. All rights reserved
7
+
8
+ require 'rubygems'
9
+ require 'highline/import'
10
+
11
+ # Create a color scheme, naming color patterns with symbol names.
12
+ ft = HighLine::ColorScheme.new do |cs|
13
+ cs[:headline] = [ :bold, :yellow, :on_black ]
14
+ cs[:horizontal_line] = [ :bold, :white, :on_blue]
15
+ cs[:even_row] = [ :green ]
16
+ cs[:odd_row] = [ :magenta ]
17
+ end
18
+
19
+ # Assign that color scheme to HighLine...
20
+ HighLine.color_scheme = ft
21
+
22
+ # ...and use it.
23
+ say("<%= color('Headline', :headline) %>")
24
+ say("<%= color('-'*20, :horizontal_line) %>")
25
+
26
+ # Setup a toggle for rows.
27
+ i = true
28
+ ("A".."D").each do |row|
29
+ row_color = i ? :even_row : :odd_row
30
+ say("<%= color('#{row}', '#{row_color}') %>")
31
+ i = !i
32
+ end
@@ -0,0 +1,19 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # overwrite.rb
4
+ #
5
+ # Created by Jeremy Hinegardner on 2007-01-24
6
+ # Copyright 2007 Jeremy Hinegardner. All rights reserved
7
+
8
+ require 'rubygems'
9
+ require 'highline/import'
10
+
11
+ prompt = "here is your password:"
12
+ ask(
13
+ "#{prompt} <%= color('mypassword', RED, BOLD) %> (Press Any Key to blank) "
14
+ ) do |q|
15
+ q.overwrite = true
16
+ q.echo = false # overwrite works best when echo is false.
17
+ q.character = true # if this is set to :getc then overwrite does not work
18
+ end
19
+ say("<%= color('Look! blanked out!', GREEN) %>")
@@ -12,6 +12,7 @@
12
12
  require "highline/system_extensions"
13
13
  require "highline/question"
14
14
  require "highline/menu"
15
+ require "highline/color_scheme"
15
16
  require "erb"
16
17
  require "optparse"
17
18
  require "stringio"
@@ -28,7 +29,7 @@ require "abbrev"
28
29
  #
29
30
  class HighLine
30
31
  # The version of the installed library.
31
- VERSION = "1.2.5".freeze
32
+ VERSION = "1.2.6".freeze
32
33
 
33
34
  # An internal HighLine error. User code does not need to trap this.
34
35
  class QuestionError < StandardError
@@ -48,6 +49,24 @@ class HighLine
48
49
  @@use_color
49
50
  end
50
51
 
52
+ # The setting used to control color schemes.
53
+ @@color_scheme = nil
54
+
55
+ # Pass ColorScheme to _setting_ to turn set a HighLine color scheme.
56
+ def self.color_scheme=( setting )
57
+ @@color_scheme = setting
58
+ end
59
+
60
+ # Returns the current color scheme.
61
+ def self.color_scheme
62
+ @@color_scheme
63
+ end
64
+
65
+ # Returns +true+ if HighLine is currently using a color scheme.
66
+ def self.using_color_scheme?
67
+ not @@color_scheme.nil?
68
+ end
69
+
51
70
  #
52
71
  # Embed in a String to clear all previous ANSI sequences. This *MUST* be
53
72
  # done before the program exits!
@@ -55,6 +74,8 @@ class HighLine
55
74
  CLEAR = "\e[0m"
56
75
  # An alias for CLEAR.
57
76
  RESET = CLEAR
77
+ # Erase the current line of terminal output.
78
+ ERASE_LINE = "\e[K"
58
79
  # The start of an ANSI bold sequence.
59
80
  BOLD = "\e[1m"
60
81
  # The start of an ANSI dark sequence. (Terminal support uncommon.)
@@ -287,13 +308,15 @@ class HighLine
287
308
  return string unless self.class.use_color?
288
309
 
289
310
  colors.map! do |c|
290
- if c.is_a?(Symbol)
311
+ if self.class.using_color_scheme? and self.class.color_scheme.include? c
312
+ self.class.color_scheme[c]
313
+ elsif c.is_a? Symbol
291
314
  self.class.const_get(c.to_s.upcase)
292
315
  else
293
316
  c
294
317
  end
295
318
  end
296
- "#{colors.join}#{string}#{CLEAR}"
319
+ "#{colors.flatten.join}#{string}#{CLEAR}"
297
320
  end
298
321
 
299
322
  #
@@ -584,7 +607,12 @@ class HighLine
584
607
  (@question.limit and line.size == @question.limit)
585
608
  @output.print(@question.echo) if @question.echo != false
586
609
  end
587
- say("\n")
610
+ if @question.overwrite
611
+ @output.print("\r#{ERASE_LINE}")
612
+ @output.flush
613
+ else
614
+ say("\n")
615
+ end
588
616
  ensure
589
617
  restore_mode if stty
590
618
  end
@@ -595,14 +623,19 @@ class HighLine
595
623
  @question.change_case(@input.getc.chr)
596
624
  else
597
625
  response = get_character(@input).chr
598
- echo = if @question.echo == true
599
- response
600
- elsif @question.echo != false
601
- @question.echo
626
+ if @question.overwrite
627
+ @output.print("\r#{ERASE_LINE}")
628
+ @output.flush
602
629
  else
603
- ""
630
+ echo = if @question.echo == true
631
+ response
632
+ elsif @question.echo != false
633
+ @question.echo
634
+ else
635
+ ""
636
+ end
637
+ say("#{echo}\n")
604
638
  end
605
- say("#{echo}\n")
606
639
  @question.change_case(response)
607
640
  end
608
641
  end
@@ -0,0 +1,120 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # color_scheme.rb
4
+ #
5
+ # Created by Jeremy Hinegardner on 2007-01-24
6
+ # Copyright 2007. All rights reserved
7
+ #
8
+ # This is Free Software. See LICENSE and COPYING for details
9
+
10
+ require 'highline'
11
+
12
+ class HighLine
13
+ #
14
+ # ColorScheme objects encapsulate a named set of colors to be used in the
15
+ # HighLine.colors() method call. For example, by applying a ColorScheme that
16
+ # has a <tt>:warning</tt> color then the following could be used:
17
+ #
18
+ # colors("This is a warning", :warning)
19
+ #
20
+ # A ColorScheme contains named sets of HighLine color constants.
21
+ #
22
+ # Example: Instantiating a color scheme, applying it to HighLine,
23
+ # and using it:
24
+ #
25
+ # ft = HighLine::ColorScheme.new do |cs|
26
+ # cs[:headline] = [ :bold, :yellow, :on_black ]
27
+ # cs[:horizontal_line] = [ :bold, :white ]
28
+ # cs[:even_row] = [ :green ]
29
+ # cs[:odd_row] = [ :magenta ]
30
+ # end
31
+ #
32
+ # HighLine.color_scheme = ft
33
+ # say("<%= color('Headline', :headline) %>")
34
+ # say("<%= color('-'*20, :horizontal_line) %>")
35
+ # i = true
36
+ # ("A".."D").each do |row|
37
+ # if i then
38
+ # say("<%= color('#{row}', :even_row ) %>")
39
+ # else
40
+ # say("<%= color('#{row}', :odd_row) %>")
41
+ # end
42
+ # i = !i
43
+ # end
44
+ #
45
+ #
46
+ class ColorScheme
47
+ #
48
+ # Create an instance of HighLine::ColorScheme. The customization can
49
+ # happen as a passed in Hash or via the yielded block. Key's are
50
+ # converted to <tt>:symbols</tt> and values are converted to HighLine
51
+ # constants.
52
+ #
53
+ def initialize( h = nil )
54
+ @scheme = Hash.new
55
+ load_from_hash(h) unless h.nil?
56
+ yield self if block_given?
57
+ end
58
+
59
+ # Load multiple colors from key/value pairs.
60
+ def load_from_hash( h )
61
+ h.each_pair do |color_tag, constants|
62
+ self[color_tag] = constants
63
+ end
64
+ end
65
+
66
+ # Does this color scheme include the given tag name?
67
+ def include?( color_tag )
68
+ @scheme.keys.include?(to_symbol(color_tag))
69
+ end
70
+
71
+ # Allow the scheme to be accessed like a Hash.
72
+ def []( color_tag )
73
+ @scheme[to_symbol(color_tag)]
74
+ end
75
+
76
+ # Allow the scheme to be set like a Hash.
77
+ def []=( color_tag, constants )
78
+ @scheme[to_symbol(color_tag)] = constants.map { |c| to_constant(c) }
79
+ end
80
+
81
+ private
82
+
83
+ # Return a normalized representation of a color name.
84
+ def to_symbol( t )
85
+ t.to_s.downcase
86
+ end
87
+
88
+ # Return a normalized representation of a color setting.
89
+ def to_constant( v )
90
+ v = v.to_s if v.is_a?(Symbol)
91
+ if v.is_a?(String) then
92
+ HighLine.const_get(v.upcase)
93
+ else
94
+ v
95
+ end
96
+ end
97
+ end
98
+
99
+ # A sample ColorScheme.
100
+ class SampleColorScheme < ColorScheme
101
+ #
102
+ # Builds the sample scheme with settings for <tt>:critical</tt>,
103
+ # <tt>:error</tt>, <tt>:warning</tt>, <tt>:notice</tt>, <tt>:info</tt>,
104
+ # <tt>:debug</tt>, <tt>:row_even</tt>, and <tt>:row_odd</tt> colors.
105
+ #
106
+ def initialize( h = nil )
107
+ scheme = {
108
+ :critical => [ :yellow, :on_red ],
109
+ :error => [ :bold, :red ],
110
+ :warning => [ :bold, :yellow ],
111
+ :notice => [ :bold, :magenta ],
112
+ :info => [ :bold, :cyan ],
113
+ :debug => [ :bold, :green ],
114
+ :row_even => [ :cyan ],
115
+ :row_odd => [ :magenta ]
116
+ }
117
+ super(scheme)
118
+ end
119
+ end
120
+ end
@@ -13,13 +13,13 @@ require "forwardable"
13
13
  $terminal = HighLine.new
14
14
 
15
15
  #
16
- # <tt>require "highline/import"</tt> adds shorcut methods to Kernel, making
16
+ # <tt>require "highline/import"</tt> adds shortcut methods to Kernel, making
17
17
  # agree(), ask(), choose() and say() globally available. This is handy for
18
18
  # quick and dirty input and output. These methods use the HighLine object in
19
19
  # the global variable <tt>$terminal</tt>, which is initialized to used
20
20
  # <tt>$stdin</tt> and <tt>$stdout</tt> (you are free to change this).
21
- # Otherwise, these methods areidentical to their HighLine counterparts, see that
22
- # class for detailed explinations.
21
+ # Otherwise, these methods are identical to their HighLine counterparts, see that
22
+ # class for detailed explanations.
23
23
  #
24
24
  module Kernel
25
25
  extend Forwardable
@@ -40,4 +40,4 @@ class Object
40
40
  details.call(question) unless details.nil?
41
41
  end
42
42
  end
43
- end
43
+ end
@@ -53,6 +53,7 @@ class HighLine
53
53
  @directory = Pathname.new(File.expand_path(File.dirname($0)))
54
54
  @glob = "*"
55
55
  @responses = Hash.new
56
+ @overwrite = false
56
57
 
57
58
  # allow block to override settings
58
59
  yield self if block_given?
@@ -71,8 +72,8 @@ class HighLine
71
72
  # character reader *ONLY* supports STDIN on Windows and Unix.) Can also
72
73
  # be set to <tt>:getc</tt> to use that method on the input stream.
73
74
  #
74
- # *WARNING*: The _echo_ attribute for a question is ignored when using
75
- # thw <tt>:getc</tt> method.
75
+ # *WARNING*: The _echo_ and _overwrite_ attributes for a question are
76
+ # ignored when using the <tt>:getc</tt> method.
76
77
  #
77
78
  attr_accessor :character
78
79
  #
@@ -193,7 +194,14 @@ class HighLine
193
194
  # validation checks fail.
194
195
  #
195
196
  attr_reader :responses
196
-
197
+ #
198
+ # When set to +true+ the question is asked, but output does not progress to
199
+ # the next line. The Cursor is moved back to the beginning of the question
200
+ # line and it is cleared so that all the contents of the line disappear from
201
+ # the screen.
202
+ #
203
+ attr_accessor :overwrite
204
+
197
205
  #
198
206
  # Returns the provided _answer_string_ or the default answer for this
199
207
  # Question if a default was set and the answer is empty.
@@ -0,0 +1,56 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # tc_color_scheme.rb
4
+ #
5
+ # Created by Jeremy Hinegardner on 2007-01-24.
6
+ # Copyright 2007 Jeremy Hinegardner. All rights reserved.
7
+ #
8
+ # This is Free Software. See LICENSE and COPYING for details.
9
+
10
+ require "test/unit"
11
+
12
+ require "highline"
13
+ require "stringio"
14
+
15
+ class TestColorScheme < Test::Unit::TestCase
16
+ def setup
17
+ @input = StringIO.new
18
+ @output = StringIO.new
19
+ @terminal = HighLine.new(@input, @output)
20
+
21
+ @old_color_scheme = HighLine.color_scheme
22
+ end
23
+
24
+ def teardown
25
+ HighLine.color_scheme = @old_color_scheme
26
+ end
27
+
28
+ def test_using_color_scheme
29
+ assert_equal(false,HighLine.using_color_scheme?)
30
+
31
+ HighLine.color_scheme = HighLine::ColorScheme.new
32
+ assert_equal(true,HighLine.using_color_scheme?)
33
+ end
34
+
35
+ def test_scheme
36
+ HighLine.color_scheme = HighLine::SampleColorScheme.new
37
+
38
+ @terminal.say("This should be <%= color('warning yellow', :warning) %>.")
39
+ assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",@output.string)
40
+ @output.rewind
41
+
42
+ @terminal.say("This should be <%= color('warning yellow', 'warning') %>.")
43
+ assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",@output.string)
44
+ @output.rewind
45
+
46
+ @terminal.say("This should be <%= color('warning yellow', 'WarNing') %>.")
47
+ assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",@output.string)
48
+ @output.rewind
49
+
50
+ # turn it back off, should raise an exception
51
+ HighLine.color_scheme = @old_color_scheme
52
+ assert_raises(NameError) {
53
+ @terminal.say("This should be <%= color('nothing at all', :error) %>.")
54
+ }
55
+ end
56
+ end
@@ -12,3 +12,4 @@ require "test/unit"
12
12
  require "tc_highline"
13
13
  require "tc_import"
14
14
  require "tc_menu"
15
+ require "tc_color_scheme"
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
2
+ rubygems_version: 0.9.1
3
3
  specification_version: 1
4
4
  name: highline
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.2.5
7
- date: 2007-01-17 00:00:00 -06:00
6
+ version: 1.2.6
7
+ date: 2007-01-25 00:00:00 -06:00
8
8
  summary: HighLine is a high-level command-line IO library.
9
9
  require_paths:
10
10
  - lib
@@ -32,16 +32,20 @@ files:
32
32
  - examples/ansi_colors.rb
33
33
  - examples/asking_for_arrays.rb
34
34
  - examples/basic_usage.rb
35
+ - examples/color_scheme.rb
35
36
  - examples/menus.rb
37
+ - examples/overwrite.rb
36
38
  - examples/page_and_wrap.rb
37
39
  - examples/password.rb
38
40
  - examples/trapping_eof.rb
39
41
  - examples/using_readline.rb
40
42
  - lib/highline.rb
43
+ - lib/highline/color_scheme.rb
41
44
  - lib/highline/import.rb
42
45
  - lib/highline/menu.rb
43
46
  - lib/highline/question.rb
44
47
  - lib/highline/system_extensions.rb
48
+ - test/tc_color_scheme.rb
45
49
  - test/tc_highline.rb
46
50
  - test/tc_import.rb
47
51
  - test/tc_menu.rb