Module: Qcmd::Plaintext
Instance Method Summary collapse
- #ascii_qlab ⇒ Object
- #centered_text(line, char = ' ') ⇒ Object
- #columns ⇒ Object
- #joined_wrapped_text(line) ⇒ Object
- #log(message = nil) ⇒ Object
- #pluralize(n, word) ⇒ Object
-
#print(message = nil) ⇒ Object
always output.
- #right_text(line) ⇒ Object
- #split_text(left, right) ⇒ Object
- #table(headers, rows) ⇒ Object
- #word_wrap(text, options = {}) ⇒ Object
-
#wrapped_text(line) ⇒ Object
turn line into lines of text of columns length.
Instance Method Details
#ascii_qlab ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/qcmd/plaintext.rb', line 38 def ascii_qlab [' .:::: .:: .:: ', ' .:: .:: .:: .:: ', '.:: .::.:: .:: .:: ', '.:: .::.:: .:: .:: .:: .:: ', '.:: .::.:: .:: .:: .:: .::', ' .:: .: .:: .:: .:: .:: .:: .::', ' .:: :: .:::::::: .:: .:::.:: .:: ', ' .: '].map {|line| print centered_text(line) } end |
#centered_text(line, char = ' ') ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/qcmd/plaintext.rb', line 65 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
16 17 18 19 20 21 22 |
# File 'lib/qcmd/plaintext.rb', line 16 def columns begin `stty size`.split.last.to_i rescue 80 end end |
#joined_wrapped_text(line) ⇒ Object
51 52 53 |
# File 'lib/qcmd/plaintext.rb', line 51 def joined_wrapped_text line wrapped_text(line).join "\n" end |
#log(message = nil) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/qcmd/plaintext.rb', line 3 def log =nil if puts else puts end end |
#pluralize(n, word) ⇒ Object
24 25 26 |
# File 'lib/qcmd/plaintext.rb', line 24 def pluralize n, word "#{n} #{n == 1 ? word : word + 's'}" end |
#print(message = nil) ⇒ Object
always output
12 13 14 |
# File 'lib/qcmd/plaintext.rb', line 12 def print =nil log() end |
#right_text(line) ⇒ Object
60 61 62 63 |
# File 'lib/qcmd/plaintext.rb', line 60 def right_text line diff = [(columns - line.size), 0].max "%s%s" % [' ' * diff, line] end |
#split_text(left, right) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/qcmd/plaintext.rb', line 81 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
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/qcmd/plaintext.rb', line 98 def table headers, rows print columns = headers.map(&:size) 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
28 29 30 31 32 33 34 35 36 |
# File 'lib/qcmd/plaintext.rb', line 28 def word_wrap(text, ={}) = { :line_width => columns }.merge text.split("\n").collect do |line| line.length > [:line_width] ? line.gsub(/(.{1,#{[:line_width]}})(\s+|$)/, "\\1\n").strip : line end * "\n" end |
#wrapped_text(line) ⇒ Object
turn line into lines of text of columns length
56 57 58 |
# File 'lib/qcmd/plaintext.rb', line 56 def wrapped_text line word_wrap(line, :line_width => columns).split("\n") end |