File: textfmt.rb

package info (click to toggle)
zonecheck 2.0.4-13lenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,424 kB
  • ctags: 1,177
  • sloc: ruby: 9,582; xml: 731; sh: 574; makefile: 66
file content (139 lines) | stat: -rw-r--r-- 3,462 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# $Id: textfmt.rb,v 1.5 2003/11/04 10:57:43 sdalu Exp $

# 
# AUTHOR   : Stephane D'Alu <sdalu@nic.fr>
# CREATED  : 2002/07/19 07:28:13
#
# COPYRIGHT: AFNIC (c) 2003
# CONTACT  : 
# LICENSE  : RUBY
#
# $Revision: 1.5 $ 
# $Date: 2003/11/04 10:57:43 $
#
# INSPIRED BY:
#   Austin Ziegler ruby version of the perl version of Text::Format
#
# CONTRIBUTORS: (see also CREDITS file)
#
#

module Text
    module Formater
        LEFT_ALIGN  = 0
        RIGHT_ALIGN = 1
        FILLED      = 2
        JUSTIFY     = 3
	MaxLineLength = 79


	#
	# Draw an L-shapped box (as below) arround the text
	#
	# | 
	# | 
	# `----- -- -- - -  -
	#
	def self.lbox(text, decoration=[ '|', '`', '-', ' ' ])
	    finalline = decoration[1] + 
		decoration[2]*5 + decoration[3] + 
		decoration[2]*2 + decoration[3] + 
		decoration[2]*2 + decoration[3] + 
		decoration[2]   + decoration[3] + 
		decoration[2]   + decoration[3]*2 + 
		decoration[2]

	    (text.split(/\n/).collect { |l| 
		 "#{decoration[0]} #{l}" } << finalline << '').join("\n")
	end

	#
	# Draw a title box as below
	#        _____________
	#      ,-------------.|
	# ~~~~ |    title    || ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	#      `-------------'
	#
	def self.title(title, maxlen=MaxLineLength)
	    txtlen = [title.length, maxlen-20].min
	    txt    = title[0..txtlen]
	    [   '       ' + '_'*(8+txtlen),
		'     ,' +  '-'*(8+txtlen) + '.|',
		'~~~~ |    '  +  txt  +  '    || ' + '~'*(maxlen-19-txtlen),
		'     `'+ '-' * (8+txtlen) + "'",
		'' ].join("\n")
	end


	#
	# Itemize a text as below
	#
	# => item1 on
	#    several lines
	# => item2
	#
	def self.item(text, bullet="=> ", offset=bullet.size)
	    lines  = text.split(/\n/)
	    spacer = " " * offset

	    ([   bullet + lines[0] ] + 
	     lines[1..-1].collect { |line| spacer + line } + 
	     ['']).join("\n")
	end

	
        def self.paragraph(text, width=78, tag='    ', style=LEFT_ALIGN)
	    out   = [ ]
            words = text.split(/\s+/)
            words.shift if words[0].empty?

            first_width = width - tag.size
            line  = words.shift
            while w = words.shift
                break unless (w.size + line.size) < (first_width - 1)
		line << ' ' << w
            end
            out << build_line(line, width, tag, style, w.nil?) unless line.nil?

            line  = w
            while w = words.shift
                if (w.size + line.size < (width - 1))
		    line << ' ' << w
                else
                    out << build_line(line, width,'', style, w.nil?) unless line.nil?
                    line = w
                end
            end
	    out << build_line(line, width, '', style, true) unless line.nil?

            out.join('')
        end


        def self.build_line(line, width, tag='', style=LEFT_ALIGN, last=false)
	    case style
	    when JUSTIFY
		return line if     last || line.empty?
		return line unless line =~ /\S+\s+\S+/
		spaces = width - line.size - tag.size
		words  = line.split(/(\s+)/)
		ws     = spaces / (words.size / 2)
		spaces = spaces % (words.size / 2) if ws > 0
		words.reverse.each { |rw|
		    next if rw =~ /^\S/
		    if spaces > 0
		    then rw.replace((' ' * (ws+1)) + rw) ; spaces -= 1
		    else rw.replace((' ' * (ws)  ) + rw)
		    end
		}
		tag + words.join('') + "\n"
	    when FILLED
		"#{tag}#{line}".ljust(width) + "\n"
	    when RIGHT_ALIGN
		"#{tag}#{line}".rjust(width) + "\n"
	    else 
		tag + line + "\n"
	    end
        end
    end
end