source: mainline/tools/xtui.py@ 6b18e43

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6b18e43 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[27fb3d6]1#
2# Copyright (c) 2009 Martin Decky
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8#
9# - Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# - Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14# - The name of the author may not be used to endorse or promote products
15# derived from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28"""
29Text User Interface wrapper
30"""
31
32import sys
33import os
34
35def call_dlg(dlgcmd, *args, **kw):
36 "Wrapper for calling 'dialog' program"
[a35b458]37
[27fb3d6]38 indesc, outdesc = os.pipe()
39 pid = os.fork()
40 if (not pid):
[99d8c82]41 os.dup2(outdesc, 2)
[27fb3d6]42 os.close(indesc)
[a35b458]43
[27fb3d6]44 dlgargs = [dlgcmd]
45 for key, val in kw.items():
46 dlgargs.append('--' + key)
47 dlgargs.append(val)
[a35b458]48
[27fb3d6]49 dlgargs += args
50 os.execlp(dlgcmd, *dlgargs)
[a35b458]51
[27fb3d6]52 os.close(outdesc)
[a35b458]53
[27fb3d6]54 try:
55 errout = os.fdopen(indesc, 'r')
56 data = errout.read()
57 errout.close()
58 pid, status = os.wait()
59 except:
60 # Reset terminal
61 os.system('reset')
62 raise
[a35b458]63
[27fb3d6]64 if (not os.WIFEXITED(status)):
65 # Reset terminal
66 os.system('reset')
67 raise EOFError
[a35b458]68
[27fb3d6]69 status = os.WEXITSTATUS(status)
70 if (status == 255):
71 raise EOFError
[a35b458]72
[27fb3d6]73 return (status, data)
74
75try:
76 import snack
77 newt = True
78 dialog = False
79except ImportError:
80 newt = False
[a35b458]81
[27fb3d6]82 dlgcmd = os.environ.get('DIALOG', 'dialog')
83 if (call_dlg(dlgcmd, '--print-maxsize')[0] != 0):
84 dialog = False
85 else:
86 dialog = True
87
88width_extra = 13
89height_extra = 11
90
91def width_fix(screen, width):
92 "Correct width to screen size"
[a35b458]93
[27fb3d6]94 if (width + width_extra > screen.width):
95 width = screen.width - width_extra
[a35b458]96
[27fb3d6]97 if (width <= 0):
98 width = screen.width
[a35b458]99
[27fb3d6]100 return width
101
102def height_fix(screen, height):
103 "Correct height to screen size"
[a35b458]104
[27fb3d6]105 if (height + height_extra > screen.height):
106 height = screen.height - height_extra
[a35b458]107
[27fb3d6]108 if (height <= 0):
109 height = screen.height
[a35b458]110
[27fb3d6]111 return height
112
113def screen_init():
114 "Initialize the screen"
[a35b458]115
[27fb3d6]116 if (newt):
117 return snack.SnackScreen()
[a35b458]118
[27fb3d6]119 return None
120
121def screen_done(screen):
122 "Cleanup the screen"
[a35b458]123
[27fb3d6]124 if (newt):
125 screen.finish()
126
127def choice_window(screen, title, text, options, position):
128 "Create options menu"
[a35b458]129
[27fb3d6]130 maxopt = 0
131 for option in options:
132 length = len(option)
133 if (length > maxopt):
134 maxopt = length
[a35b458]135
[27fb3d6]136 width = maxopt
137 height = len(options)
[a35b458]138
[27fb3d6]139 if (newt):
140 width = width_fix(screen, width + width_extra)
141 height = height_fix(screen, height)
[a35b458]142
[be8b5d6]143 if (height > 3):
144 large = True
145 else:
146 large = False
[a35b458]147
[27fb3d6]148 buttonbar = snack.ButtonBar(screen, ('Done', 'Cancel'))
149 textbox = snack.TextboxReflowed(width, text)
[be8b5d6]150 listbox = snack.Listbox(height, scroll = large, returnExit = 1)
[a35b458]151
[27fb3d6]152 cnt = 0
153 for option in options:
154 listbox.append(option, cnt)
155 cnt += 1
[a35b458]156
[27fb3d6]157 if (position != None):
158 listbox.setCurrent(position)
[a35b458]159
[27fb3d6]160 grid = snack.GridForm(screen, title, 1, 3)
161 grid.add(textbox, 0, 0)
162 grid.add(listbox, 0, 1, padding = (0, 1, 0, 1))
163 grid.add(buttonbar, 0, 2, growx = 1)
[a35b458]164
[27fb3d6]165 retval = grid.runOnce()
[a35b458]166
[27fb3d6]167 return (buttonbar.buttonPressed(retval), listbox.current())
168 elif (dialog):
[96a2e45]169 if (width < 35):
170 width = 35
[a35b458]171
[27fb3d6]172 args = []
173 cnt = 0
174 for option in options:
175 args.append(str(cnt + 1))
176 args.append(option)
[a35b458]177
[27fb3d6]178 cnt += 1
[a35b458]179
[27fb3d6]180 kw = {}
181 if (position != None):
182 kw['default-item'] = str(position + 1)
[a35b458]183
[27fb3d6]184 status, data = call_dlg(dlgcmd, '--title', title, '--extra-button', '--extra-label', 'Done', '--menu', text, str(height + height_extra), str(width + width_extra), str(cnt), *args, **kw)
[a35b458]185
[27fb3d6]186 if (status == 1):
187 return ('cancel', None)
[a35b458]188
[27fb3d6]189 try:
190 choice = int(data) - 1
191 except ValueError:
192 return ('cancel', None)
[a35b458]193
[27fb3d6]194 if (status == 0):
195 return (None, choice)
[a35b458]196
[27fb3d6]197 return ('done', choice)
[a35b458]198
[27fb3d6]199 sys.stdout.write("\n *** %s *** \n%s\n\n" % (title, text))
[a35b458]200
[27fb3d6]201 maxcnt = len(str(len(options)))
202 cnt = 0
203 for option in options:
204 sys.stdout.write("%*s. %s\n" % (maxcnt, cnt + 1, option))
205 cnt += 1
[a35b458]206
[27fb3d6]207 sys.stdout.write("\n%*s. Done\n" % (maxcnt, '0'))
208 sys.stdout.write("%*s. Quit\n\n" % (maxcnt, 'q'))
[a35b458]209
[27fb3d6]210 while True:
211 if (position != None):
212 sys.stdout.write("Selection[%s]: " % str(position + 1))
213 else:
[b766352]214 if (cnt > 0):
215 sys.stdout.write("Selection[1]: ")
216 else:
217 sys.stdout.write("Selection[0]: ")
[27fb3d6]218 inp = sys.stdin.readline()
[a35b458]219
[27fb3d6]220 if (not inp):
221 raise EOFError
[a35b458]222
[27fb3d6]223 if (not inp.strip()):
224 if (position != None):
225 return (None, position)
[b766352]226 else:
227 if (cnt > 0):
228 inp = '1'
229 else:
230 inp = '0'
[a35b458]231
[27fb3d6]232 if (inp.strip() == 'q'):
233 return ('cancel', None)
[a35b458]234
[27fb3d6]235 try:
236 choice = int(inp.strip())
237 except ValueError:
238 continue
[a35b458]239
[27fb3d6]240 if (choice == 0):
241 return ('done', 0)
[a35b458]242
[27fb3d6]243 if (choice < 1) or (choice > len(options)):
244 continue
[a35b458]245
[27fb3d6]246 return (None, choice - 1)
247
248def error_dialog(screen, title, msg):
249 "Print error dialog"
[a35b458]250
[27fb3d6]251 width = len(msg)
[a35b458]252
[27fb3d6]253 if (newt):
254 width = width_fix(screen, width)
[a35b458]255
[27fb3d6]256 buttonbar = snack.ButtonBar(screen, ['Ok'])
257 textbox = snack.TextboxReflowed(width, msg)
[a35b458]258
[27fb3d6]259 grid = snack.GridForm(screen, title, 1, 2)
260 grid.add(textbox, 0, 0, padding = (0, 0, 0, 1))
261 grid.add(buttonbar, 0, 1, growx = 1)
262 grid.runOnce()
263 elif (dialog):
264 call_dlg(dlgcmd, '--title', title, '--msgbox', msg, '6', str(width + width_extra))
[c79d50d]265 else:
266 sys.stdout.write("\n%s: %s\n" % (title, msg))
Note: See TracBrowser for help on using the repository browser.