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 get_directory_icon():
53 return os
.path
.join (ICONSDIR
, 'dir.png')
56 return os
.path
.join (ICONSDIR
, 'generic.png')
58 def shell_quote (*inputs
):
59 '''Quote strings so that they can be suitably martialled
60 off to the shell. This method supports POSIX sh syntax.
61 This is crucial to properly handle command line arguments
62 with spaces, quotes, double-quotes, etc.'''
64 regex
= re
.compile ('[^\w!%+,\-./:@^]')
65 quote_regex
= re
.compile ("((?:'\\''){2,})")
73 raise AssertionError, ('No way to quote strings '
74 'containing null (\\000) bytes')
76 # = does need quoting else in command position it's a
77 # program-local environment setting
78 match
= regex
.search (input)
81 input = input.replace ("'", "'\\''")
83 # make multiple ' in a row look simpler
84 # '\'''\'''\'' -> '"'''"'
85 quote_match
= quote_regex
.match (input)
87 quotes
= match
.group (1)
88 input.replace (quotes
,
89 ("'" * (len(quotes
)/4)) + "\"'")
91 input = "'%s'" % input
92 if input.startswith ("''"):
93 input = input.lstrip ("''")
95 if input.endswith ("''"):
96 input = input.rstrip ("''")
100 ANSI_BACKGROUND_COLOR
= '41'
113 def ansi_to_html (ansi
):
114 '''Converts a block of ANSI text into an equivalent html fragment.'''
117 regex
= re
.compile ('(.*?)\x1b\[(\d*)m')
119 for line
in ansi
.split ('\n'):
121 linecopy
= html_encode(line
)
122 match
= regex
.match (linecopy
)
126 start
, end
= match
.span()
128 prefix
= match
.group (1)
129 middle
= match
.group (2)
130 postfix
= linecopy
[end
:]
132 if middle
in ANSI_TABLE
:
133 color
= ANSI_TABLE
[middle
]
134 middle
= '<span style="color: %s">' % color
137 elif middle
== ANSI_BACKGROUND_COLOR
:
138 middle
= '<span style="background-color:red">'
147 linecopy
= prefix
+ middle
+ postfix
148 match
= regex
.match (linecopy
)
150 html
.append (linecopy
)
152 return '<br/>'.join (html
)
154 def html_header (header
):
156 <p style="color: black;
157 background-color: yellow">
161 def html_encode (ascii
):
162 '''HTML-encodes text. This method explicitly avoids encoding
163 alphanumeric and ANSI-escape sequences.'''
171 # There is no HTML equivalent to a tab, so just
172 # insert eight spaces
173 html
.append ( ' ' * 8 )
175 elif char
== '\x1b' or char
== '[':
176 # Don't encode ANSI characters since these
177 # are stripped out during ansi_to_html
181 html
.append (ENTITIES
[char
])
183 return ''.join (html
)
185 # Keep this at the bottom of the file since it's generated output.
186 # Generated by html2py.pl from HTML::Entities on Wed Dec 5 16:28:55 PST 2007
349 chr(161) : '¡',
351 chr(163) : '£',
352 chr(164) : '¤',
354 chr(166) : '¦',
359 chr(171) : '«',
365 chr(177) : '±',
368 chr(180) : '´',
369 chr(181) : 'µ',
371 chr(183) : '·',
372 chr(184) : '¸',
375 chr(187) : '»',
376 chr(188) : '¼',
377 chr(189) : '½',
378 chr(190) : '¾',
379 chr(191) : '¿',
380 chr(192) : 'À',
381 chr(193) : 'Á',
382 chr(194) : 'Â',
383 chr(195) : 'Ã',
385 chr(197) : 'Å',
386 chr(198) : 'Æ',
387 chr(199) : 'Ç',
388 chr(200) : 'È',
389 chr(201) : 'É',
390 chr(202) : 'Ê',
392 chr(204) : 'Ì',
393 chr(205) : 'Í',
394 chr(206) : 'Î',
397 chr(209) : 'Ñ',
398 chr(210) : 'Ò',
399 chr(211) : 'Ó',
400 chr(212) : 'Ô',
401 chr(213) : 'Õ',
403 chr(215) : '×',
404 chr(216) : 'Ø',
405 chr(217) : 'Ù',
406 chr(218) : 'Ú',
407 chr(219) : 'Û',
409 chr(221) : 'Ý',
410 chr(222) : 'Þ',
411 chr(223) : 'ß',
412 chr(224) : 'à',
413 chr(225) : 'á',
414 chr(226) : 'â',
415 chr(227) : 'ã',
417 chr(229) : 'å',
418 chr(230) : 'æ',
419 chr(231) : 'ç',
420 chr(232) : 'è',
421 chr(233) : 'é',
422 chr(234) : 'ê',
424 chr(236) : 'ì',
425 chr(237) : 'í',
426 chr(238) : 'î',
429 chr(241) : 'ñ',
430 chr(242) : 'ò',
431 chr(243) : 'ó',
432 chr(244) : 'ô',
433 chr(245) : 'õ',
435 chr(247) : '÷',
436 chr(248) : 'ø',
437 chr(249) : 'ù',
438 chr(250) : 'ú',
439 chr(251) : 'û',
441 chr(253) : 'ý',
442 chr(254) : 'þ',