@@ -118,97 +118,4 @@ def display(self):
118118 sys .stdout .write (s + ' ' * (75 - len (s )) + '\r ' )
119119 sys .stdout .flush ()
120120
121- LEFT = 'left'
122- LEFTN = 'left no strip'
123- RIGHT = 'right'
124- CENTER = 'center'
125-
126- def align (line , width = 70 , alignment = LEFTN ):
127- ''' Code from http://www.faqts.com/knowledge_base/view.phtml/aid/4476 '''
128- if alignment == CENTER :
129- line = line .strip ()
130- space = width - len (line )
131- return ' ' * (space / 2 ) + line + ' ' * (space / 2 + space % 2 )
132- elif alignment == RIGHT :
133- line = line .rstrip ()
134- space = width - len (line )
135- return ' ' * space + line
136- else :
137- if alignment == LEFT :
138- line = line .lstrip ()
139- space = width - len (line )
140- return line + ' ' * space
141-
142-
143- def format_line (columns , positions , contents , spacer = ' | ' ,
144- collapse_whitespace = True , wsre = re .compile (r'\s+' )):
145- ''' Fill up a single row with data from the contents '''
146- l = []
147- data = 0
148- for i in range (len (columns )):
149- width , alignment = columns [i ]
150- content = contents [i ]
151- col = ''
152- while positions [i ] < len (content ):
153- word = content [positions [i ]]
154- # if we hit a newline, honor it
155- if '\n ' in word :
156- # chomp
157- positions [i ] += 1
158- break
159-
160- # make sure this word fits
161- if col and len (word ) + len (col ) > width :
162- break
163-
164- # no whitespace at start-of-line
165- if collapse_whitespace and wsre .match (word ) and not col :
166- # chomp
167- positions [i ] += 1
168- continue
169-
170- col += word
171- # chomp
172- positions [i ] += 1
173- if col :
174- data = 1
175- col = align (col , width , alignment )
176- l .append (col )
177-
178- if not data :
179- return ''
180- return spacer .join (l ).rstrip ()
181-
182-
183- def format_columns (columns , contents , spacer = ' | ' , collapse_whitespace = True ,
184- splitre = re .compile (r'(\n|\r\n|\r|[ \t]+|\S+)' )):
185- ''' Format the contents into columns, with 'spacing' between the
186- columns
187- '''
188- assert len (columns ) == len (contents ), \
189- 'columns and contents must be same length'
190-
191- # split the text into words, spaces/tabs and newlines
192- for i in range (len (contents )):
193- contents [i ] = splitre .findall (contents [i ])
194-
195- # now process line by line
196- l = []
197- positions = [0 ]* len (contents )
198- while 1 :
199- l .append (format_line (columns , positions , contents , spacer ,
200- collapse_whitespace ))
201-
202- # are we done?
203- for i in range (len (contents )):
204- if positions [i ] < len (contents [i ]):
205- break
206- else :
207- break
208- return '\n ' .join (l )
209-
210- def wrap (text , width = 75 , alignment = LEFTN ):
211- return format_columns (((width , alignment ),), [text ],
212- collapse_whitespace = False )
213-
214121# vim: set et sts=4 sw=4 :
0 commit comments