Sha256: 24505685d0d7138a31cd34881aeb52ab53a23c4764997ef7e26b9e4e7ef85ff8
Contents?: true
Size: 1.88 KB
Versions: 7
Compression:
Stored size: 1.88 KB
Contents
## \file wordwrap.rb small word-wrapping utility # copyright (c) 2009 by Vincent Fourmond # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details (in the COPYING file). require 'ctioga2/utils' require 'ctioga2/commands/commands' require 'ctioga2/commands/parsers/command-line' module CTioga2 Version::register_svn_info('$Revision: 131 $', '$Date: 2010-01-14 22:51:09 +0100 (Thu, 14 Jan 2010) $') module Commands module Documentation # A small utility class to do word wrapping. # # \todo Maybe this belongs in Utils ? class WordWrapper # A regex matching word separation. attr_accessor :word_sep # What to replace the separator with attr_accessor :new_sep def initialize(ws = /\s+/, ns = " ") @word_sep = ws @new_sep = ns end # Split strings into an array of string whose length is each # less than _cols_ def wrap(str, cols) words = str.split(@word_sep) lines = [words.shift] while w = words.shift if (lines.last.size + w.size + @new_sep.size) <= cols lines.last.concat("#{@new_sep}#{w}") else lines << w end end return lines end # Calls #wrap for default values of the parameters def self.wrap(str, cols) return WordWrapper.new.wrap(str, cols) end end end end end
Version data entries
7 entries across 7 versions & 1 rubygems