highline 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  Below is a complete listing of changes for each revision of HighLine.
4
4
 
5
+ == 1.2.2
6
+
7
+ * Minor documentation corrections.
8
+ * Applied Thomas Werschleiln's patch to fix termio buffering on Solaris.
9
+ * Applied Justin Bailey's patch to allow canceling paged output.
10
+ * Fixed a documentation bug in the description of character case settings.
11
+ * Added a notice about termios in HighLine::Question#echo.
12
+ * Finally working around the infamous "fast typing" bug
13
+
5
14
  == 1.2.1
6
15
 
7
16
  * Applied Justin Bailey's fix for the page_print() infinite loop bug.
@@ -28,7 +28,7 @@ require "abbrev"
28
28
  #
29
29
  class HighLine
30
30
  # The version of the installed library.
31
- VERSION = "1.2.1".freeze
31
+ VERSION = "1.2.2".freeze
32
32
 
33
33
  # An internal HighLine error. User code does not need to trap this.
34
34
  class QuestionError < StandardError
@@ -572,16 +572,23 @@ class HighLine
572
572
  if @question.echo == true and @question.limit.nil?
573
573
  get_line
574
574
  else
575
+ raw_no_echo_mode if stty = CHARACTER_MODE == "stty"
576
+
575
577
  line = ""
576
- while character = get_character(@input)
577
- line << character.chr
578
- # looking for carriage return (decimal 13) or
579
- # newline (decimal 10) in raw input
580
- break if character == 13 or character == 10 or
581
- (@question.limit and line.size == @question.limit)
582
- @output.print(@question.echo) if @question.echo != false
578
+ begin
579
+ while character = (stty ? @input.getc : get_character(@input))
580
+ line << character.chr
581
+ # looking for carriage return (decimal 13) or
582
+ # newline (decimal 10) in raw input
583
+ break if character == 13 or character == 10 or
584
+ (@question.limit and line.size == @question.limit)
585
+ @output.print(@question.echo) if @question.echo != false
586
+ end
587
+ say("\n")
588
+ ensure
589
+ restore_mode if stty
583
590
  end
584
- say("\n")
591
+
585
592
  @question.change_case(@question.remove_whitespace(line))
586
593
  end
587
594
  elsif @question.character == :getc
@@ -613,12 +620,23 @@ class HighLine
613
620
  while lines.size > @page_at
614
621
  @output.puts lines.slice!(0...@page_at).join
615
622
  @output.puts
616
- HighLine.new(@input, @output).ask("-- press enter/return to continue -- ")
617
- @output.puts
623
+ # Return last line if user wants to abort paging
624
+ return (["...\n"] + lines.slice(-2,1)).join unless continue_paging?
618
625
  end
619
626
  return lines.join
620
627
  end
621
-
628
+
629
+ #
630
+ # Ask user if they wish to continue paging output. Allows them to type "q" to
631
+ # cancel the paging process.
632
+ #
633
+ def continue_paging?
634
+ command = HighLine.new(@input, @output).ask(
635
+ "-- press enter/return to continue or q to stop -- "
636
+ ) { |q| q.character = true }
637
+ command !~ /\A[qQ]\Z/ # Only continue paging if Q was not hit.
638
+ end
639
+
622
640
  #
623
641
  # Wrap a sequence of _lines_ at _wrap_at_ characters per line. Existing
624
642
  # newlines will not be affected by this process, but additional newlines
@@ -89,6 +89,11 @@ class HighLine
89
89
  #
90
90
  # This requires HighLine's character reader. See the _character_
91
91
  # attribute for details.
92
+ #
93
+ # *Note*: When using HighLine to manage echo on Unix based systems, we
94
+ # recommend installing the termios gem. Without it, it's possible to type
95
+ # fast enough to have letters still show up (when reading character by
96
+ # character only).
92
97
  #
93
98
  attr_accessor :echo
94
99
  #
@@ -107,7 +112,7 @@ class HighLine
107
112
  #
108
113
  attr_accessor :whitespace
109
114
  #
110
- # Used to control whitespace processing for the answer to this question.
115
+ # Used to control character case processing for the answer to this question.
111
116
  # See HighLine::Question.change_case() for acceptable settings.
112
117
  #
113
118
  attr_accessor :case
@@ -1,6 +1,6 @@
1
1
  #!/usr/local/bin/ruby -w
2
2
 
3
- # import.rb
3
+ # system_extensions.rb
4
4
  #
5
5
  # Created by James Edward Gray II on 2006-06-14.
6
6
  # Copyright 2006 Gray Productions. All rights reserved.
@@ -64,8 +64,9 @@ class HighLine
64
64
  def get_character( input = STDIN )
65
65
  old_settings = Termios.getattr(input)
66
66
 
67
- new_settings = old_settings.dup
68
- new_settings.c_lflag &= ~(Termios::ECHO | Termios::ICANON)
67
+ new_settings = old_settings.dup
68
+ new_settings.c_lflag &= ~(Termios::ECHO | Termios::ICANON)
69
+ new_settings.c_cc[Termios::VMIN] = 1
69
70
 
70
71
  begin
71
72
  Termios.setattr(input, Termios::TCSANOW, new_settings)
@@ -83,15 +84,33 @@ class HighLine
83
84
  # *WARNING*: This method requires the external "stty" program!
84
85
  #
85
86
  def get_character( input = STDIN )
86
- state = `stty -g`
87
+ raw_no_echo_mode
87
88
 
88
89
  begin
89
- system "stty raw -echo cbreak"
90
90
  input.getc
91
91
  ensure
92
- system "stty #{state}"
92
+ restore_mode
93
93
  end
94
94
  end
95
+
96
+ #
97
+ # Switched the input mode to raw and disables echo.
98
+ #
99
+ # *WARNING*: This method requires the external "stty" program!
100
+ #
101
+ def raw_no_echo_mode
102
+ @state = `stty -g`
103
+ system "stty raw -echo cbreak"
104
+ end
105
+
106
+ #
107
+ # Restores a previously saved input mode.
108
+ #
109
+ # *WARNING*: This method requires the external "stty" program!
110
+ #
111
+ def restore_mode
112
+ system "stty #{@state}"
113
+ end
95
114
  end
96
115
 
97
116
  # A Unix savvy method to fetch the console columns, and rows.
@@ -450,9 +450,9 @@ class TestHighLine < Test::Unit::TestCase
450
450
 
451
451
  @terminal.say((1..50).map { |n| "This is line #{n}.\n"}.join)
452
452
  assert_equal( (1..22).map { |n| "This is line #{n}.\n"}.join +
453
- "\n-- press enter/return to continue -- \n" +
453
+ "\n-- press enter/return to continue or q to stop -- \n\n" +
454
454
  (23..44).map { |n| "This is line #{n}.\n"}.join +
455
- "\n-- press enter/return to continue -- \n" +
455
+ "\n-- press enter/return to continue or q to stop -- \n\n" +
456
456
  (45..50).map { |n| "This is line #{n}.\n"}.join,
457
457
  @output.string )
458
458
  end
@@ -406,4 +406,24 @@ class TestMenu < Test::Unit::TestCase
406
406
  selected = @terminal.choose(* 1..10)
407
407
  assert_equal(selected, 3)
408
408
  end
409
+
410
+
411
+ def test_cancel_paging
412
+ # Tests that paging can be cancelled halfway through
413
+ @terminal.page_at = 5
414
+ # Will page twice, so stop after first page and make choice 3
415
+ @input << "q\n3\n"
416
+ @input.rewind
417
+
418
+ selected = @terminal.choose(* 1..10)
419
+ assert_equal(selected, 3)
420
+
421
+ # Make sure paging message appeared
422
+ assert( @output.string.index('press enter/return to continue or q to stop'),
423
+ "Paging message did not appear." )
424
+
425
+ # Make sure it only appeared once
426
+ assert( @output.string !~ /q to stop.*q to stop/m,
427
+ "Paging message appeared more than once." )
428
+ end
409
429
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: highline
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.2.1
7
- date: 2006-06-14 00:00:00 -05:00
6
+ version: 1.2.2
7
+ date: 2006-10-13 00:00:00 -05:00
8
8
  summary: HighLine is a high-level command-line IO library.
9
9
  require_paths:
10
10
  - lib