Module: Textify
- Defined in:
- lib/textify.rb
Constant Summary collapse
- DefaultScreenWidth =
Textify
Some basic, non-curses-based functions to aid user interaction in text-only ruby scripts. Included are functions for
* strings * List selection, confirmation, data entry * arrays and hashes as tablesUsage
require "textify" class MyClass textify def foo [[1,2,3],[4,5,6],[7,8,9]].table({:headings => %w[ one two three ]}) end end o = MyClass.new o.foo The snippet above outputs: **************************************** * one * two * three * **************************************** * 1 * 2 * 3 * * 4 * 5 * 6 * * 7 * 8 * 9 * **************************************** 40
Instance Method Summary collapse
- #ask(prompt = "\n >", options = {}) ⇒ Object
-
#br(n = 1) ⇒ Object
minor utility fns.
- #cls ⇒ Object
- #confirm(prompt = "Please confirm. ", options = {}) ⇒ Object
- #display_selection(items, prompt) ⇒ Object
-
#justify(direction, text, options = {}) ⇒ Object
Textual.justify (direction, text, options={}) Justifies text within the default screen width.
- #select(items, prompt = " Please select one of the following options: ", options = {}) ⇒ Object
- #tabulate(data, options = {}) ⇒ Object
- #test_ask ⇒ Object
- #test_confirm ⇒ Object
- #test_justify ⇒ Object
- #test_select ⇒ Object
- #test_table ⇒ Object
- #test_title ⇒ Object
- #title(name, options = {}) ⇒ Object
- #unit_test ⇒ Object
Instance Method Details
#ask(prompt = "\n >", options = {}) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/textify.rb', line 170 def ask(prompt="\n >",={}) prompt = prompt.to_s + "? " if prompt.class == Symbol print prompt.capitalize if [:validate] input = gets.chomp if [:validate].downcase == 'numeric' end else return gets.chomp end end |
#br(n = 1) ⇒ Object
minor utility fns
43 |
# File 'lib/textify.rb', line 43 def br n=1; puts until (n-=1)<0; end |
#cls ⇒ Object
44 |
# File 'lib/textify.rb', line 44 def cls; br 50; end |
#confirm(prompt = "Please confirm. ", options = {}) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/textify.rb', line 156 def confirm(prompt = "Please confirm. ", = {}) choices = [:choices] || %w[ Yes No] input = select choices, prompt return true if input == choices[0] return false end |
#display_selection(items, prompt) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/textify.rb', line 121 def display_selection items, prompt title prompt # print "\n"*3 items.each_with_index { |item, n| puts " [#{n+1}] #{item.gsub('_',' ').capitalize} \n" } print "\n > " @input = gets.to_i; end |
#justify(direction, text, options = {}) ⇒ Object
Textual.justify (direction, text, options={})
Justifies text within the default screen width. Accepts in the hash:
:width set a new default width for this justification
:character fill with another characer (default is ' ')
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/textify.rb', line 58 def justify(direction, text, ={}) # puts "Entering justify..." width = [:width] || DefaultScreenWidth if text.length >= width # puts "Returning, text '#{text}' (#{text.size}) too big for width #{width}." return text end orig_text = text char = [:character] || ' ' usable_space = width-text.length offset = char*usable_space left_offset = char * (usable_space.to_f/2).ceil right_offset = char * (usable_space - left_offset.size) #half_offset = offset.slice(0..(offset.size/2).floor) # puts "Usable space: #{usable_space}. Left: #{left_offset.size}. Right: #{right_offset.size}." text = case direction #.downcase.to_sym when :left then "#{text}#{offset}" when :right then "#{offset}#{text}" when :center then "#{left_offset}#{text}#{right_offset}" else text end # puts "- Justifying (#{direction.to_s}) to width (#{width}): '#{orig_text}' => \t'#{text}'" text end |
#select(items, prompt = " Please select one of the following options: ", options = {}) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/textify.rb', line 128 def select(items, prompt=" Please select one of the following options: ", ={}) #title prompt # print "\n"*3 #items.each_with_index { |item, n| puts " [#{n+1}] #{item.gsub('_',' ').capitalize} \n" } #print "\n > " #input = gets.to_i; # display_selection; @input=0; display_selection(items, prompt) until (@input >= 1 and @input <= items.size) #title prompt # print "\n"*3 #items.each_with_index { |item, n| puts " [#{n+1}] #{item.gsub('_',' ').capitalize} \n" } #print "\n > " #input = gets.to_i #end puts "Selected #{@input} (#{items[@input-1]}).. " #{ }"#{options[input-1]}." return items[@input-1] end |
#tabulate(data, options = {}) ⇒ Object
197 |
# File 'lib/textify.rb', line 197 def tabulate(data, ={}); data.table if %w[ Hash Array ].include? data.class.name; end |
#test_ask ⇒ Object
183 184 185 186 187 188 189 190 |
# File 'lib/textify.rb', line 183 def test_ask title " Test Input Gathering " name = ask "What's your name? " puts "Thanks for answering, #{name}!" age = ask "How old are you? " puts "Great! It must be nice to be #{age} years old." true end |
#test_confirm ⇒ Object
163 164 165 166 |
# File 'lib/textify.rb', line 163 def test_confirm puts "Must be nice..." if confirm "Are you human?" true end |
#test_justify ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/textify.rb', line 90 def test_justify title " Testing justify command " text = justify :left, "Left Justifed" puts text puts justify(:right, "Right Justifed") puts justify(:center, "Center Justifed") puts "Unjustified" true end |
#test_select ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/textify.rb', line 145 def test_select title " Selection Test " choices = %w[ apples strawberries bananas mandarin_oranges tangerines oranges cherries watermelons raspberries ] option = select choices title "Thanks for choosing #{option}!" true end |
#test_table ⇒ Object
199 200 201 202 203 204 205 206 207 |
# File 'lib/textify.rb', line 199 def test_table title " Testing tables " tabulate({:one => "1", :two => "2", :three => "3", :four => "4", :five => "512351234123"}); br 2 tabulate([[1,2,3,4],[4,5,6,7],[7,8,9,0]], {:headings => %w[ alpha beta gamma delta ], :width => 100} ); br 3 tabulate([[1,2,3,4],[4,5,6,7],[7,8,9,0]], {:headings => %w[ alpha beta gamma delta ], :width => 50} ); br 3 true end |
#test_title ⇒ Object
112 113 114 115 116 117 |
# File 'lib/textify.rb', line 112 def test_title title " Sample Title 1 " title " Sample Title 2 ", {:character => '@', :offset => 3 } title " Sample Title 3 ", {:character => "&", :offset => 2 } true end |
#title(name, options = {}) ⇒ Object
106 107 108 109 110 |
# File 'lib/textify.rb', line 106 def title name, ={} br [:offset] || 2 print "#{justify :center, name, {:character => options[:character] || '='}}" br [:offset] || 2 end |
#unit_test ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/textify.rb', line 212 def unit_test title " Textual Unit Test " results = {} %w[ title ask select confirm justify table ].each { |operation| results[operation.to_sym] = send("test_"+operation) } =begin results << {:title, test_title} results << {:ask, test_ask} results << {:select, test_select} results << {:confirm, test_confirm} results << {:justify, test_justify} results << {:table, test_table} =end title " Internal Unit Test Results " results.each_pair {|k,v| puts " #{k} => #{v==true ? 'pass' : 'fail'}" } assertions = results.size passed = results.select { |k,v| v=true }.size failed = results.select { |k,v| v=false }.size title " #{assertions} assertions, #{failed} failed, #{passed} passed " return false if failed > 0 true end |