8 'python': 'script.png',
10 'shell': 'script.png',
13 'assembler': 'binary.png',
14 'binary': 'binary.png',
19 ICONSDIR
= os
.path
.join (os
.path
.dirname (__file__
), 'icons')
21 def ident_file_type (filename
):
22 '''Returns an icon based on the contents of filename.'''
23 if os
.path
.exists (filename
):
24 quoted_filename
= shell_quote (filename
)
25 fileinfo
= commands
.getoutput('file -b %s' % quoted_filename
)
26 for filetype
, iconname
in KNOWN_FILE_TYPES
.iteritems():
27 if filetype
in fileinfo
.lower():
31 # Fallback for modified files of an unknown type
34 def get_icon (filename
):
35 '''Returns the full path to an icon file corresponding to
36 filename's contents.'''
37 icon_file
= ident_file_type (filename
)
38 return os
.path
.join (ICONSDIR
, icon_file
)
40 def get_staged_icon (filename
):
41 '''Special-case method for staged items. These are only
42 ever 'staged' and 'removed' items in the staged list.'''
44 if os
.path
.exists (filename
):
45 return os
.path
.join (ICONSDIR
, 'staged.png')
47 return os
.path
.join (ICONSDIR
, 'removed.png')
49 def get_untracked_icon():
50 return os
.path
.join (ICONSDIR
, 'untracked.png')
52 def shell_quote (*inputs
):
53 '''Quote strings so that they can be suitably martialled
54 off to the shell. This method supports POSIX sh syntax.
55 This is crucial to properly handle command line arguments
56 with spaces, quotes, double-quotes, etc.'''
58 regex
= re
.compile ('[^\w!%+,\-./:@^]')
59 quote_regex
= re
.compile ("((?:'\\''){2,})")
67 raise AssertionError, ('No way to quote strings '
68 'containing null (\\000) bytes')
70 # = does need quoting else in command position it's a
71 # program-local environment setting
72 match
= regex
.search (input)
75 input = input.replace ("'", "'\\''")
77 # make multiple ' in a row look simpler
78 # '\'''\'''\'' -> '"'''"'
79 quote_match
= quote_regex
.match (input)
81 quotes
= match
.group (1)
82 input.replace (quotes
,
83 ("'" * (len(quotes
)/4)) + "\"'")
85 input = "'%s'" % input
86 if input.startswith ("''"):
87 input = input.lstrip ("''")
89 if input.endswith ("''"):
90 input = input.rstrip ("''")
94 ANSI_BACKGROUND_COLOR
= 41
108 def html_document (html_fragment
):
109 return '<body>%s</body>' % html_fragment
111 def ansi_to_html (ansi
):
112 '''Converts a block of text into an equivalent html fragment.'''
115 regex
= re
.compile ('(.*?)\x1b\[(\d*)m')
117 for line
in ansi
.split ('\n'):
119 linecopy
= str (html_encode(line
))
120 match
= regex
.match (linecopy
)
124 start
, end
= match
.span()
126 prefix
= match
.group (1)
127 middle
= match
.group (2)
128 postfix
= linecopy
[end
:]
130 if middle
in ANSI_TABLE
:
131 color
= ANSI_TABLE
[middle
]
132 middle
= '<span style="color: %s">' % color
134 elif middle
== str (ANSI_BACKGROUND_COLOR
):
135 middle
= '<span style="background-color:red">'
143 linecopy
= str (prefix
+ middle
+ postfix
)
144 match
= regex
.match (linecopy
)
146 html
.append (linecopy
)
148 return '<br />\n'.join (html
)
150 def html_header (header
):
152 <p style="color: black;
153 background-color: yellow">
157 def html_encode (ascii
):
159 ANSI
= [ '\x1b', '[' ]
162 html
.append ( ' ' * 8 )
164 if char
not in ANSI
and not char
.isalnum():
165 html
.append (ENTITIES
[char
])
168 return ''.join (html
)
170 # Keep this at the bottom of the file since it's generated output.
171 # Generated by html2py.pl from HTML::Entities on Wed Dec 5 16:28:55 PST 2007
334 chr(161) : '¡',
336 chr(163) : '£',
337 chr(164) : '¤',
339 chr(166) : '¦',
344 chr(171) : '«',
350 chr(177) : '±',
353 chr(180) : '´',
354 chr(181) : 'µ',
356 chr(183) : '·',
357 chr(184) : '¸',
360 chr(187) : '»',
361 chr(188) : '¼',
362 chr(189) : '½',
363 chr(190) : '¾',
364 chr(191) : '¿',
365 chr(192) : 'À',
366 chr(193) : 'Á',
367 chr(194) : 'Â',
368 chr(195) : 'Ã',
370 chr(197) : 'Å',
371 chr(198) : 'Æ',
372 chr(199) : 'Ç',
373 chr(200) : 'È',
374 chr(201) : 'É',
375 chr(202) : 'Ê',
377 chr(204) : 'Ì',
378 chr(205) : 'Í',
379 chr(206) : 'Î',
382 chr(209) : 'Ñ',
383 chr(210) : 'Ò',
384 chr(211) : 'Ó',
385 chr(212) : 'Ô',
386 chr(213) : 'Õ',
388 chr(215) : '×',
389 chr(216) : 'Ø',
390 chr(217) : 'Ù',
391 chr(218) : 'Ú',
392 chr(219) : 'Û',
394 chr(221) : 'Ý',
395 chr(222) : 'Þ',
396 chr(223) : 'ß',
397 chr(224) : 'à',
398 chr(225) : 'á',
399 chr(226) : 'â',
400 chr(227) : 'ã',
402 chr(229) : 'å',
403 chr(230) : 'æ',
404 chr(231) : 'ç',
405 chr(232) : 'è',
406 chr(233) : 'é',
407 chr(234) : 'ê',
409 chr(236) : 'ì',
410 chr(237) : 'í',
411 chr(238) : 'î',
414 chr(241) : 'ñ',
415 chr(242) : 'ò',
416 chr(243) : 'ó',
417 chr(244) : 'ô',
418 chr(245) : 'õ',
420 chr(247) : '÷',
421 chr(248) : 'ø',
422 chr(249) : 'ù',
423 chr(250) : 'ú',
424 chr(251) : 'û',
426 chr(253) : 'ý',
427 chr(254) : 'þ',