module TextAlign
Constants
- IRC_CTRL_PATTERN
- IRC_FILL
Public Instance Methods
Source
# File lib/text_align.rb, line 38 def align_labels(labels, suffix: ":") labels = labels.map(&:to_s) width = labels.map(&:length).max labels.map do |label| "#{pad(label, width, align: :right)}#{suffix}" end end
Source
# File lib/text_align.rb, line 60 def colour_pad(text, width, style:, align: :left, fill: IRC_FILL) plain = pad(text, width, align: align, fill: fill) return plain if style.to_s.empty? plain.chars.map do |ch| ch == fill ? "#{style}#{ch}#{IrcFormat::RESET}" : ch end.join end
Source
# File lib/text_align.rb, line 21 def dotize(text, fill: IRC_FILL) text.to_s.tr(" ", fill) end
Source
# File lib/text_align.rb, line 46 def format_colon_row(label:, value:, label_width: nil) label_text = label.to_s width = label_width || label_text.length "#{pad(label_text, width, align: :right)}: #{value}" end
Source
# File lib/text_align.rb, line 52 def format_colon_rows(rows) labels = rows.map { |row| row.fetch(:label).to_s } width = labels.map(&:length).max rows.map do |row| format_colon_row(label: row.fetch(:label), value: row.fetch(:value), label_width: width) end end
Source
# File lib/text_align.rb, line 25 def pad(text, width, align: :left, fill: IRC_FILL) padding = width - plain_length(text) return text if padding <= 0 pad_str = fill * padding case align when :left then "#{text}#{pad_str}" when :right then "#{pad_str}#{text}" else raise ArgumentError, "align must be :left or :right" end end
Source
# File lib/text_align.rb, line 17 def plain_length(text) text.to_s.gsub(IRC_CTRL_PATTERN, "").length end