Module: Text
- Defined in:
- lib/droxi/text.rb
Overview
Module containing text-manipulation methods.
Constant Summary collapse
- DEFAULT_WIDTH =
The assumed width of the terminal if GNU Readline can’t retrieve it.
72
Class Method Summary collapse
-
.format_table(items, item_width, columns) ⇒ Object
Return an
Arrayof lines of the given items formatted as a table. -
.get_wrap_segment(text, width) ⇒ Object
Return a wrapped line of output from the start of the given text.
-
.table(items) ⇒ Object
Format an
ArrayofStringsas a table and return anArrayof lines in the result. -
.terminal_width ⇒ Object
Return the width of the terminal in columns.
-
.tokenize(string, include_empty: false) ⇒ Object
Split a
Stringinto tokens, allowing for backslash-escaped spaces, and return the resultingArray. -
.wrap(text) ⇒ Object
Wrap a
Stringto fit the terminal and return anArrayof lines in the result.
Class Method Details
.format_table(items, item_width, columns) ⇒ Object
Return an Array of lines of the given items formatted as a table.
54 55 56 57 58 59 60 |
# File 'lib/droxi/text.rb', line 54 def self.format_table(items, item_width, columns) lines, items = [], items.dup until items.empty? lines << items.shift(columns).map { |item| item.ljust(item_width) }.join end lines end |
.get_wrap_segment(text, width) ⇒ Object
Return a wrapped line of output from the start of the given text.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/droxi/text.rb', line 63 def self.get_wrap_segment(text, width) line = '' loop do head, _, text = text.partition(' ') line << "#{head} " break if text.empty? || line.size >= width end line.strip! trim_last_word = line.size > width && line.include?(' ') trim_last_word ? line.rpartition(' ').first : line end |
.table(items) ⇒ Object
Format an Array of Strings as a table and return an Array of lines in the result.
8 9 10 11 12 13 14 |
# File 'lib/droxi/text.rb', line 8 def self.table(items) return [] if items.empty? width = terminal_width item_width = items.map { |item| item.size }.max + 2 items_per_line = [1, width / item_width].max format_table(items, item_width, items_per_line) end |
.terminal_width ⇒ Object
Return the width of the terminal in columns.
45 46 47 48 49 50 51 |
# File 'lib/droxi/text.rb', line 45 def self.terminal_width require 'readline' width = Readline.get_screen_size[1] width > 0 ? width : DEFAULT_WIDTH rescue NotImplementedError DEFAULT_WIDTH end |
.tokenize(string, include_empty: false) ⇒ Object
Split a String into tokens, allowing for backslash-escaped spaces, and return the resulting Array.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/droxi/text.rb', line 30 def self.tokenize(string, include_empty: false) tokens = string.split tokens << '' if include_empty && (string.empty? || string.end_with?(' ')) tokens.reduce([]) do |list, token| list << if !list.empty? && list.last.end_with?('\\') "#{list.pop.chop} #{token}" else token end end end |
.wrap(text) ⇒ Object
Wrap a String to fit the terminal and return an Array of lines in the result.
18 19 20 21 22 23 24 25 26 |
# File 'lib/droxi/text.rb', line 18 def self.wrap(text) width, position = terminal_width, 0 lines = [] while position < text.size lines << get_wrap_segment(text[position, text.size], width) position += lines.last.size + 1 end lines end |