MCPcopy Create free account
hub / github.com/ActiveState/code / echo

Function echo

recipes/Python/574451_Handling_ANSI_Terminal/recipe-574451.py:15–65  ·  view source on GitHub ↗

"arg" is a string or None if "arg" is None : the terminal is reset to his default values. if "arg" is a string it must contain "sep" separated values. if args are found in globals "attrs" or "colors", or start with "@" \ they are interpreted as ANSI commands else

(arg=None, sep=' ', end='\n')

Source from the content-addressed store, hash-verified

13).items(): globals()[k]=dict((s,i) for i,s in enumerate(v.split()))
14
15def echo(arg=None, sep=' ', end='\n'):
16 '''
17 "arg" is a string or None
18 if "arg" is None : the terminal is reset to his default values.
19 if "arg" is a string it must contain "sep" separated values.
20 if args are found in globals "attrs" or "colors", or start with "@" \
21 they are interpreted as ANSI commands else they are output as text.
22 colors, if any, must be first (foreground first then background)
23 you can not specify a background color alone ; \
24 if you specify only one color, it will be the foreground one.
25 @* commands handle the screen and the cursor :
26 @x;y : go to xy
27 @ : go to 1;1
28 @@ : clear screen and go to 1;1
29
30 examples:
31 echo('red') : set red as the foreground color
32 echo('red blue') : red on blue
33 echo('red blink') : blinking red
34 echo() : restore terminal default values
35 echo('reverse') : swap default colors
36 echo('cyan blue reverse') : blue on cyan <=> echo(&#x27;blue cyan)
37 echo('red reverse') : a way to set up the background only
38 echo('red reverse blink') : you can specify any combinaison of \
39 attributes in any order with or without colors
40 echo('blink Python') : output a blinking 'Python'
41 echo('@@ hello') : clear the screen and print 'hello' at 1;1
42 ''&#x27;
43
44 cmd, txt = [reset], []
45 if arg:
46 arglist=arg.split(sep)
47 for offset in (fgoffset, bgoffset):
48 if arglist and arglist[0] in colors:
49 cmd.append(format % (colors[arglist.pop(0)]+offset))
50 for a in arglist:
51 c=format % attrs[a] if a in attrs else None
52 if c and c not in cmd:
53 cmd.append(c)
54 else:
55 if a.startswith('@'):
56 a=a[1:]
57 if a=='@':
58 cmd.append('2J')
59 cmd.append('H')
60 else:
61 cmd.append('%sH'%a)
62 else:
63 txt.append(a)
64 if txt and end: txt[-1]+=end
65 stdout.write(esc.join(cmd)+sep.join(txt))
66
67if __name__ == '__main__':
68

Callers 1

recipe-574451.pyFile · 0.70

Calls 6

startswithMethod · 0.80
splitMethod · 0.45
appendMethod · 0.45
popMethod · 0.45
writeMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected