highline-sgonyea 1.6.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.gitignore +2 -0
  2. data/AUTHORS +3 -0
  3. data/CHANGELOG +308 -0
  4. data/COPYING +340 -0
  5. data/INSTALL +55 -0
  6. data/LICENSE +7 -0
  7. data/README.rdoc +63 -0
  8. data/Rakefile +50 -0
  9. data/TODO +6 -0
  10. data/examples/ansi_colors.rb +36 -0
  11. data/examples/asking_for_arrays.rb +16 -0
  12. data/examples/basic_usage.rb +73 -0
  13. data/examples/color_scheme.rb +30 -0
  14. data/examples/limit.rb +10 -0
  15. data/examples/menus.rb +63 -0
  16. data/examples/overwrite.rb +17 -0
  17. data/examples/page_and_wrap.rb +320 -0
  18. data/examples/password.rb +5 -0
  19. data/examples/repeat_entry.rb +19 -0
  20. data/examples/trapping_eof.rb +20 -0
  21. data/examples/using_readline.rb +15 -0
  22. data/highline.gemspec +36 -0
  23. data/lib/highline.rb +1000 -0
  24. data/lib/highline/color_scheme.rb +134 -0
  25. data/lib/highline/compatibility.rb +16 -0
  26. data/lib/highline/import.rb +41 -0
  27. data/lib/highline/menu.rb +398 -0
  28. data/lib/highline/question.rb +472 -0
  29. data/lib/highline/simulate.rb +48 -0
  30. data/lib/highline/string_extensions.rb +131 -0
  31. data/lib/highline/style.rb +181 -0
  32. data/lib/highline/system_extensions.rb +186 -0
  33. data/setup.rb +1360 -0
  34. data/site/.cvsignore +1 -0
  35. data/site/highline.css +65 -0
  36. data/site/images/logo.png +0 -0
  37. data/site/index.html +58 -0
  38. data/test/string_methods.rb +32 -0
  39. data/test/tc_color_scheme.rb +96 -0
  40. data/test/tc_highline.rb +1027 -0
  41. data/test/tc_import.rb +52 -0
  42. data/test/tc_menu.rb +427 -0
  43. data/test/tc_string_extension.rb +20 -0
  44. data/test/tc_string_highline.rb +38 -0
  45. data/test/tc_style.rb +567 -0
  46. data/test/ts_all.rb +16 -0
  47. metadata +118 -0
data/INSTALL ADDED
@@ -0,0 +1,55 @@
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
+ == Installing the Gem
7
+
8
+ HighLine is intended to be installed via the
9
+ RubyGems[http://rubyforge.org/projects/rubygems/] system. To get the latest
10
+ version, simply enter the following into your command prompt:
11
+
12
+ $ sudo gem install highline
13
+
14
+ You must have RubyGems[http://rubyforge.org/projects/rubygems/] installed for
15
+ the above to work.
16
+
17
+ If you want to build the gem locally, make sure you have
18
+ Rake[http://rubyforge.org/projects/rake/] installed then run the following
19
+ command:
20
+
21
+ $ rake package
22
+
23
+ == Installing Manually
24
+
25
+ Download the latest version of HighLine from the
26
+ {RubyForge project page}[http://rubyforge.org/frs/?group_id=683]. Navigate to
27
+ the root project directory and enter:
28
+
29
+ $ sudo ruby setup.rb
30
+
31
+ == Installing HighLine on JRuby
32
+
33
+ If you are using HighLine on JRuby, many features will not work properly
34
+ without a working ncurses installation. First, ensure that you have
35
+ ncurses installed and then install the ffi-ncurses gem.
36
+
37
+ If ffi-ncurses fails to find your ncurses library, you may need to set the
38
+ RUBY_FFI_NCURSES envirionment variable, i.e:
39
+
40
+ RUBY_FFI_NCURSES_LIB=ncursesw ruby examples/hello.rb
41
+
42
+ For details, see the ffi-ncurses documentation at:
43
+ http://github.com/seanohalpin/ffi-ncurses
44
+
45
+ == Using termios
46
+
47
+ While not a requirement, HighLine will take advantage of the termios library if
48
+ installed (on Unix). This slightly improves HighLine's character reading
49
+ capabilities and thus is recommended for all Unix users.
50
+
51
+ If using the HighLine gem, you should be able to add termios as easily as:
52
+
53
+ $ sudo gem install termios
54
+
55
+ For manual installs, consult the termios documentation.
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ = License Terms
2
+
3
+ Distributed under the user's choice of the {GPL Version 2}[http://www.gnu.org/licenses/old-licenses/gpl-2.0.html] (see COPYING for details) or the
4
+ {Ruby software license}[http://www.ruby-lang.org/en/LICENSE.txt] by
5
+ James Edward Gray II and Greg Brown.
6
+
7
+ Please email James[mailto:james@grayproductions.net] with any questions.
data/README.rdoc ADDED
@@ -0,0 +1,63 @@
1
+ = Read Me
2
+
3
+ by James Edward Gray II
4
+
5
+ == Description
6
+
7
+ Welcome to HighLine.
8
+
9
+ HighLine was designed to ease the tedious tasks of doing console input and
10
+ output with low-level methods like gets() and puts(). HighLine provides a
11
+ robust system for requesting data from a user, without needing to code all the
12
+ error checking and validation rules and without needing to convert the typed
13
+ Strings into what your program really needs. Just tell HighLine what you're
14
+ after, and let it do all the work.
15
+
16
+ == Documentation
17
+
18
+ See HighLine and HighLine::Question for documentation.
19
+
20
+ == Examples
21
+
22
+ Basic usage:
23
+
24
+ ask("Company? ") { |q| q.default = "none" }
25
+
26
+ Validation:
27
+
28
+ ask("Age? ", Integer) { |q| q.in = 0..105 }
29
+ ask("Name? (last, first) ") { |q| q.validate = /\A\w+, ?\w+\Z/ }
30
+
31
+ Type conversion for answers:
32
+
33
+ ask("Birthday? ", Date)
34
+ ask("Interests? (comma sep list) ", lambda { |str| str.split(/,\s*/) })
35
+
36
+ Reading passwords:
37
+
38
+ ask("Enter your password: ") { |q| q.echo = false }
39
+ ask("Enter your password: ") { |q| q.echo = "x" }
40
+
41
+ ERb based output (with HighLine's ANSI color tools):
42
+
43
+ say("This should be <%= color('bold', BOLD) %>!")
44
+
45
+ Menus:
46
+
47
+ choose do |menu|
48
+ menu.prompt = "Please choose your favorite programming language? "
49
+
50
+ menu.choice(:ruby) { say("Good choice!") }
51
+ menu.choices(:python, :perl) { say("Not from around here, are you?") }
52
+ end
53
+
54
+ For more examples see the examples/ directory of this project.
55
+
56
+ == Installing
57
+
58
+ See the INSTALL file for instructions.
59
+
60
+ == Questions and/or Comments
61
+
62
+ Feel free to email {James Edward Gray II}[mailto:james@grayproductions.net] or
63
+ {Gregory Brown}[mailto:gregory.t.brown@gmail.com] with any questions.
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require "rdoc/task"
2
+ require "rake/testtask"
3
+ require "rubygems/package_task"
4
+
5
+ require "rubygems"
6
+
7
+ task :default => [:test]
8
+
9
+ Rake::TestTask.new do |test|
10
+ test.libs << "test"
11
+ test.test_files = [ "test/ts_all.rb"]
12
+ test.verbose = true
13
+ test.ruby_opts << "-w"
14
+ end
15
+
16
+ Rake::RDocTask.new do |rdoc|
17
+ rdoc.rdoc_files.include( "README.rdoc", "INSTALL",
18
+ "TODO", "CHANGELOG",
19
+ "AUTHORS", "COPYING",
20
+ "LICENSE", "lib/" )
21
+ rdoc.main = "README.rdoc"
22
+ rdoc.rdoc_dir = "doc/html"
23
+ rdoc.title = "HighLine Documentation"
24
+ end
25
+
26
+ desc "Upload current documentation to Rubyforge"
27
+ task :upload_docs => [:rdoc] do
28
+ sh "scp -r doc/html/* " +
29
+ "bbazzarrakk@rubyforge.org:/var/www/gforge-projects/highline/doc/"
30
+ sh "scp -r site/* " +
31
+ "bbazzarrakk@rubyforge.org:/var/www/gforge-projects/highline/"
32
+ end
33
+
34
+ load(File.join(File.dirname(__FILE__), "highline.gemspec"))
35
+ Gem::PackageTask.new(SPEC) do |package|
36
+ # do nothing: I just need a gem but this block is required
37
+ end
38
+
39
+ desc "Show library's code statistics"
40
+ task :stats do
41
+ require 'code_statistics'
42
+ CodeStatistics.new( ["HighLine", "lib"],
43
+ ["Functionals", "examples"],
44
+ ["Units", "test"] ).to_s
45
+ end
46
+
47
+ desc "Add new files to Subversion"
48
+ task :add_to_svn do
49
+ sh %Q{svn status | ruby -nae 'system "svn add \#{$F[1]}" if $F[0] == "?"' }
50
+ end
data/TODO ADDED
@@ -0,0 +1,6 @@
1
+ = To Do List
2
+
3
+ The following is a list of planned expansions for HighLine, in no particular
4
+ order.
5
+
6
+ * Rent this space.
@@ -0,0 +1,36 @@
1
+ # ansi_colors.rb
2
+ #
3
+ # Created by James Edward Gray II on 2005-05-03.
4
+ # Copyright 2005 Gray Productions. All rights reserved.
5
+
6
+ require "rubygems"
7
+ require "highline/import"
8
+
9
+ # Supported color sequences.
10
+ colors = %w{black red green yellow blue magenta cyan white}
11
+
12
+ # Using color() with symbols.
13
+ colors.each_with_index do |c, i|
14
+ say("This should be <%= color('#{c}', :#{c}) %>!")
15
+ if i == 0
16
+ say( "This should be " +
17
+ "<%= color('white on #{c}', :white, :on_#{c}) %>!")
18
+ else
19
+ say( "This should be " +
20
+ "<%= color( '#{colors[i - 1]} on #{c}',
21
+ :#{colors[i - 1]}, :on_#{c} ) %>!")
22
+ end
23
+ end
24
+
25
+ # Using color with constants.
26
+ say("This should be <%= color('bold', BOLD) %>!")
27
+ say("This should be <%= color('underlined', UNDERLINE) %>!")
28
+
29
+ # Using constants only.
30
+ say("This might even <%= BLINK %>blink<%= CLEAR %>!")
31
+
32
+ # It even works with list wrapping.
33
+ erb_digits = %w{Zero One Two Three Four} +
34
+ ["<%= color('Five', :blue) %%>"] +
35
+ %w{Six Seven Eight Nine}
36
+ say("<%= list(#{erb_digits.inspect}, :columns_down, 3) %>")
@@ -0,0 +1,16 @@
1
+ # asking_for_arrays.rb
2
+ #
3
+ # Created by James Edward Gray II on 2005-07-05.
4
+ # Copyright 2005 Gray Productions. All rights reserved.
5
+
6
+ require "rubygems"
7
+ require "highline/import"
8
+ require "pp"
9
+
10
+ grades = ask( "Enter test scores (or a blank line to quit):",
11
+ lambda { |ans| ans =~ /^-?\d+$/ ? Integer(ans) : ans} ) do |q|
12
+ q.gather = ""
13
+ end
14
+
15
+ say("Grades:")
16
+ pp grades
@@ -0,0 +1,73 @@
1
+ # basic_usage.rb
2
+ #
3
+ # Created by James Edward Gray II on 2005-04-28.
4
+ # Copyright 2005 Gray Productions. All rights reserved.
5
+
6
+ require "rubygems"
7
+ require "highline/import"
8
+ require "yaml"
9
+
10
+ contacts = [ ]
11
+
12
+ class NameClass
13
+ def self.parse( string )
14
+ if string =~ /^\s*(\w+),\s*(\w+)\s*$/
15
+ self.new($2, $1)
16
+ else
17
+ raise ArgumentError, "Invalid name format."
18
+ end
19
+ end
20
+
21
+ def initialize(first, last)
22
+ @first, @last = first, last
23
+ end
24
+
25
+ attr_reader :first, :last
26
+ end
27
+
28
+ begin
29
+ entry = Hash.new
30
+
31
+ # basic output
32
+ say("Enter a contact:")
33
+
34
+ # basic input
35
+ entry[:name] = ask("Name? (last, first) ", NameClass) do |q|
36
+ q.validate = /\A\w+, ?\w+\Z/
37
+ end
38
+ entry[:company] = ask("Company? ") { |q| q.default = "none" }
39
+ entry[:address] = ask("Address? ")
40
+ entry[:city] = ask("City? ")
41
+ entry[:state] = ask("State? ") do |q|
42
+ q.case = :up
43
+ q.validate = /\A[A-Z]{2}\Z/
44
+ end
45
+ entry[:zip] = ask("Zip? ") do |q|
46
+ q.validate = /\A\d{5}(?:-?\d{4})?\Z/
47
+ end
48
+ entry[:phone] = ask( "Phone? ",
49
+ lambda { |p| p.delete("^0-9").
50
+ sub(/\A(\d{3})/, '(\1) ').
51
+ sub(/(\d{4})\Z/, '-\1') } ) do |q|
52
+ q.validate = lambda { |p| p.delete("^0-9").length == 10 }
53
+ q.responses[:not_valid] = "Enter a phone numer with area code."
54
+ end
55
+ entry[:age] = ask("Age? ", Integer) { |q| q.in = 0..105 }
56
+ entry[:birthday] = ask("Birthday? ", Date)
57
+ entry[:interests] = ask( "Interests? (comma separated list) ",
58
+ lambda { |str| str.split(/,\s*/) } )
59
+ entry[:description] = ask("Enter a description for this contact.") do |q|
60
+ q.whitespace = :strip_and_collapse
61
+ end
62
+
63
+ contacts << entry
64
+ # shortcut for yes and no questions
65
+ end while agree("Enter another contact? ", true)
66
+
67
+ if agree("Save these contacts? ", true)
68
+ file_name = ask("Enter a file name: ") do |q|
69
+ q.validate = /\A\w+\Z/
70
+ q.confirm = true
71
+ end
72
+ File.open("#{file_name}.yaml", "w") { |file| YAML.dump(contacts, file) }
73
+ end
@@ -0,0 +1,30 @@
1
+ # color_scheme.rb
2
+ #
3
+ # Created by Jeremy Hinegardner on 2007-01-24
4
+ # Copyright 2007 Jeremy Hinegardner. All rights reserved
5
+
6
+ require 'rubygems'
7
+ require 'highline/import'
8
+
9
+ # Create a color scheme, naming color patterns with symbol names.
10
+ ft = HighLine::ColorScheme.new do |cs|
11
+ cs[:headline] = [ :bold, :yellow, :on_black ]
12
+ cs[:horizontal_line] = [ :bold, :white, :on_blue]
13
+ cs[:even_row] = [ :green ]
14
+ cs[:odd_row] = [ :magenta ]
15
+ end
16
+
17
+ # Assign that color scheme to HighLine...
18
+ HighLine.color_scheme = ft
19
+
20
+ # ...and use it.
21
+ say("<%= color('Headline', :headline) %>")
22
+ say("<%= color('-'*20, :horizontal_line) %>")
23
+
24
+ # Setup a toggle for rows.
25
+ i = true
26
+ ("A".."D").each do |row|
27
+ row_color = i ? :even_row : :odd_row
28
+ say("<%= color('#{row}', '#{row_color}') %>")
29
+ i = !i
30
+ end
data/examples/limit.rb ADDED
@@ -0,0 +1,10 @@
1
+ # limit.rb
2
+ #
3
+ # Created by James Edward Gray II on 2008-11-12.
4
+ # Copyright 2008 Gray Productions. All rights reserved.
5
+
6
+ require "rubygems"
7
+ require "highline/import"
8
+
9
+ text = ask("Enter text (max 10 chars): ") { |q| q.limit = 10 }
10
+ puts "You entered: #{text}!"
data/examples/menus.rb ADDED
@@ -0,0 +1,63 @@
1
+ require "rubygems"
2
+ require "highline/import"
3
+
4
+ # The old way, using ask() and say()...
5
+ choices = %w{ruby python perl}
6
+ say("This is the old way using ask() and say()...")
7
+ say("Please choose your favorite programming language:")
8
+ say(choices.map { |c| " #{c}\n" }.join)
9
+
10
+ case ask("? ", choices)
11
+ when "ruby"
12
+ say("Good choice!")
13
+ else
14
+ say("Not from around here, are you?")
15
+ end
16
+
17
+ # The new and improved choose()...
18
+ say("\nThis is the new mode (default)...")
19
+ choose do |menu|
20
+ menu.prompt = "Please choose your favorite programming language? "
21
+
22
+ menu.choice :ruby do say("Good choice!") end
23
+ menu.choices(:python, :perl) do say("Not from around here, are you?") end
24
+ end
25
+
26
+ say("\nThis is letter indexing...")
27
+ choose do |menu|
28
+ menu.index = :letter
29
+ menu.index_suffix = ") "
30
+
31
+ menu.prompt = "Please choose your favorite programming language? "
32
+
33
+ menu.choice :ruby do say("Good choice!") end
34
+ menu.choices(:python, :perl) do say("Not from around here, are you?") end
35
+ end
36
+
37
+ say("\nThis is with a different layout...")
38
+ choose do |menu|
39
+ menu.layout = :one_line
40
+
41
+ menu.header = "Languages"
42
+ menu.prompt = "Favorite? "
43
+
44
+ menu.choice :ruby do say("Good choice!") end
45
+ menu.choices(:python, :perl) do say("Not from around here, are you?") end
46
+ end
47
+
48
+ say("\nYou can even build shells...")
49
+ loop do
50
+ choose do |menu|
51
+ menu.layout = :menu_only
52
+
53
+ menu.shell = true
54
+
55
+ menu.choice(:load, "Load a file.") do |command, details|
56
+ say("Loading file with options: #{details}...")
57
+ end
58
+ menu.choice(:save, "Save a file.") do |command, details|
59
+ say("Saving file with options: #{details}...")
60
+ end
61
+ menu.choice(:quit, "Exit program.") { exit }
62
+ end
63
+ end
@@ -0,0 +1,17 @@
1
+ # overwrite.rb
2
+ #
3
+ # Created by Jeremy Hinegardner on 2007-01-24
4
+ # Copyright 2007 Jeremy Hinegardner. All rights reserved
5
+
6
+ require 'rubygems'
7
+ require 'highline/import'
8
+
9
+ prompt = "here is your password:"
10
+ ask(
11
+ "#{prompt} <%= color('mypassword', RED, BOLD) %> (Press Any Key to blank) "
12
+ ) do |q|
13
+ q.overwrite = true
14
+ q.echo = false # overwrite works best when echo is false.
15
+ q.character = true # if this is set to :getc then overwrite does not work
16
+ end
17
+ say("<%= color('Look! blanked out!', GREEN) %>")