Module: Qcmd::Plaintext
Instance Method Summary collapse
- #ascii_qlab ⇒ Object
- #centered_text(line, char = ' ') ⇒ Object
- #columns ⇒ Object
- #joined_wrapped(*args) ⇒ Object
- #log(level, message = nil) ⇒ Object
- #pluralize(n, word) ⇒ Object
-
#print(message = nil) ⇒ Object
display output unless absolutely silent.
- #print_wrapped(line) ⇒ Object
- #right_text(line) ⇒ Object
- #set_columns(value) ⇒ Object
- #split_text(left, right) ⇒ Object
- #table(headers, rows) ⇒ Object
- #word_wrap(text, options = {}) ⇒ Object
-
#wrapped_text(*args) ⇒ Object
turn line into lines of text of columns length.
Instance Method Details
#ascii_qlab ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/qcmd/plaintext.rb', line 67 def ascii_qlab [' .:::: .:: .:: ', ' .:: .:: .:: .:: ', '.:: .::.:: .:: .:: ', '.:: .::.:: .:: .:: .:: .:: ', '.:: .::.:: .:: .:: .:: .::', ' .:: .: .:: .:: .:: .:: .:: .::', ' .:: :: .:::::::: .:: .:::.:: .:: ', ' .: '].map {|line| print centered_text(line) } end |
#centered_text(line, char = ' ') ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/qcmd/plaintext.rb', line 104 def centered_text line, char=' ' if line.size > columns && line.split(' ').all? {|chunk| chunk.size < columns} # wrap the text then center each line, then join return wrapped_text(line).map {|l| centered_text(l, char)}.join("\n") end diff = (columns - line.size) return line if diff < 0 lpad = diff / 2 rpad = diff - lpad "%s%s%s" % [char * lpad, line, char * rpad] end |
#columns ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/qcmd/plaintext.rb', line 22 def columns @columns || (begin @columns = `stty size`.split.last.to_i rescue @columns = 80 end) end |
#joined_wrapped(*args) ⇒ Object
91 92 93 |
# File 'lib/qcmd/plaintext.rb', line 91 def joined_wrapped *args wrapped_text(*args).join("\n") end |
#log(level, message = nil) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/qcmd/plaintext.rb', line 3 def log level, =nil if Qcmd.log_level_acheived?(level) if puts else puts end end end |
#pluralize(n, word) ⇒ Object
30 31 32 |
# File 'lib/qcmd/plaintext.rb', line 30 def pluralize n, word "#{n} #{n == 1 ? word : word + 's'}" end |
#print(message = nil) ⇒ Object
display output unless absolutely silent
14 15 16 |
# File 'lib/qcmd/plaintext.rb', line 14 def print =nil log(:info, ) unless Qcmd.silent? end |
#print_wrapped(line) ⇒ Object
95 96 97 |
# File 'lib/qcmd/plaintext.rb', line 95 def print_wrapped line print wrapped_text(line) end |
#right_text(line) ⇒ Object
99 100 101 102 |
# File 'lib/qcmd/plaintext.rb', line 99 def right_text line diff = [(columns - line.size), 0].max "%s%s" % [' ' * diff, line] end |
#set_columns(value) ⇒ Object
18 19 20 |
# File 'lib/qcmd/plaintext.rb', line 18 def set_columns value @columns = value end |
#split_text(left, right) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/qcmd/plaintext.rb', line 120 def split_text left, right diff = columns - left.size if (diff - right.size) < 0 left_lines = wrapped_text(left) diff = columns - left_lines.last.size # still? if (diff - right.size) < 0 diff = '' right = "\n" + right_text(right) end left = left_lines.join "\n" end "%s%#{diff}s" % [left, right] end |
#table(headers, rows) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/qcmd/plaintext.rb', line 137 def table headers, rows print columns = headers.map(&:size) # coerce row values to strings rows.each do |row| columns.each_with_index do |col, n| row[n] = row[n].to_s end end rows.each do |row| columns.each_with_index do |col, n| columns[n] = [col, row[n].size].max + 1 end end row_format = columns.map {|n| "%#{n}s\t"}.join('') print row_format % headers print rows.each do |row| print row_format % row end print end |
#word_wrap(text, options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/qcmd/plaintext.rb', line 34 def word_wrap(text, ={}) = { :line_width => columns, :preserve_whitespace => false }.merge unless [:preserve_whitespace] text = text.gsub(/\s+/, ' ') # collapse whitespace end prefix = [:indent] ? [:indent] : '' line_width = [:line_width] lines = [''] space = ' ' space_size = 2 space_left = line_width text.split.each do |word| if (word.size + space.size) >= space_left word = "%s%s" % [prefix, word] space_left = line_width - (word.size + space_size) lines << "" else space_left = space_left - (word.size + space_size) end lines.last << "%s%s" % [word, space] end lines end |
#wrapped_text(*args) ⇒ Object
turn line into lines of text of columns length
81 82 83 84 85 86 87 88 89 |
# File 'lib/qcmd/plaintext.rb', line 81 def wrapped_text *args = { :line_width => columns }.merge args. line = args.shift word_wrap(line, ) end |