Changeset a35b458 in mainline for tools/xtui.py
- Timestamp:
- 2018-03-02T20:10:49Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1380b7
- Parents:
- 3061bc1
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/xtui.py
r3061bc1 ra35b458 35 35 def call_dlg(dlgcmd, *args, **kw): 36 36 "Wrapper for calling 'dialog' program" 37 37 38 38 indesc, outdesc = os.pipe() 39 39 pid = os.fork() … … 41 41 os.dup2(outdesc, 2) 42 42 os.close(indesc) 43 43 44 44 dlgargs = [dlgcmd] 45 45 for key, val in kw.items(): 46 46 dlgargs.append('--' + key) 47 47 dlgargs.append(val) 48 48 49 49 dlgargs += args 50 50 os.execlp(dlgcmd, *dlgargs) 51 51 52 52 os.close(outdesc) 53 53 54 54 try: 55 55 errout = os.fdopen(indesc, 'r') … … 61 61 os.system('reset') 62 62 raise 63 63 64 64 if (not os.WIFEXITED(status)): 65 65 # Reset terminal 66 66 os.system('reset') 67 67 raise EOFError 68 68 69 69 status = os.WEXITSTATUS(status) 70 70 if (status == 255): 71 71 raise EOFError 72 72 73 73 return (status, data) 74 74 … … 79 79 except ImportError: 80 80 newt = False 81 81 82 82 dlgcmd = os.environ.get('DIALOG', 'dialog') 83 83 if (call_dlg(dlgcmd, '--print-maxsize')[0] != 0): … … 91 91 def width_fix(screen, width): 92 92 "Correct width to screen size" 93 93 94 94 if (width + width_extra > screen.width): 95 95 width = screen.width - width_extra 96 96 97 97 if (width <= 0): 98 98 width = screen.width 99 99 100 100 return width 101 101 102 102 def height_fix(screen, height): 103 103 "Correct height to screen size" 104 104 105 105 if (height + height_extra > screen.height): 106 106 height = screen.height - height_extra 107 107 108 108 if (height <= 0): 109 109 height = screen.height 110 110 111 111 return height 112 112 113 113 def screen_init(): 114 114 "Initialize the screen" 115 115 116 116 if (newt): 117 117 return snack.SnackScreen() 118 118 119 119 return None 120 120 121 121 def screen_done(screen): 122 122 "Cleanup the screen" 123 123 124 124 if (newt): 125 125 screen.finish() … … 127 127 def choice_window(screen, title, text, options, position): 128 128 "Create options menu" 129 129 130 130 maxopt = 0 131 131 for option in options: … … 133 133 if (length > maxopt): 134 134 maxopt = length 135 135 136 136 width = maxopt 137 137 height = len(options) 138 138 139 139 if (newt): 140 140 width = width_fix(screen, width + width_extra) 141 141 height = height_fix(screen, height) 142 142 143 143 if (height > 3): 144 144 large = True 145 145 else: 146 146 large = False 147 147 148 148 buttonbar = snack.ButtonBar(screen, ('Done', 'Cancel')) 149 149 textbox = snack.TextboxReflowed(width, text) 150 150 listbox = snack.Listbox(height, scroll = large, returnExit = 1) 151 151 152 152 cnt = 0 153 153 for option in options: 154 154 listbox.append(option, cnt) 155 155 cnt += 1 156 156 157 157 if (position != None): 158 158 listbox.setCurrent(position) 159 159 160 160 grid = snack.GridForm(screen, title, 1, 3) 161 161 grid.add(textbox, 0, 0) 162 162 grid.add(listbox, 0, 1, padding = (0, 1, 0, 1)) 163 163 grid.add(buttonbar, 0, 2, growx = 1) 164 164 165 165 retval = grid.runOnce() 166 166 167 167 return (buttonbar.buttonPressed(retval), listbox.current()) 168 168 elif (dialog): 169 169 if (width < 35): 170 170 width = 35 171 171 172 172 args = [] 173 173 cnt = 0 … … 175 175 args.append(str(cnt + 1)) 176 176 args.append(option) 177 177 178 178 cnt += 1 179 179 180 180 kw = {} 181 181 if (position != None): 182 182 kw['default-item'] = str(position + 1) 183 183 184 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) 185 185 186 186 if (status == 1): 187 187 return ('cancel', None) 188 188 189 189 try: 190 190 choice = int(data) - 1 191 191 except ValueError: 192 192 return ('cancel', None) 193 193 194 194 if (status == 0): 195 195 return (None, choice) 196 196 197 197 return ('done', choice) 198 198 199 199 sys.stdout.write("\n *** %s *** \n%s\n\n" % (title, text)) 200 200 201 201 maxcnt = len(str(len(options))) 202 202 cnt = 0 … … 204 204 sys.stdout.write("%*s. %s\n" % (maxcnt, cnt + 1, option)) 205 205 cnt += 1 206 206 207 207 sys.stdout.write("\n%*s. Done\n" % (maxcnt, '0')) 208 208 sys.stdout.write("%*s. Quit\n\n" % (maxcnt, 'q')) 209 209 210 210 while True: 211 211 if (position != None): … … 217 217 sys.stdout.write("Selection[0]: ") 218 218 inp = sys.stdin.readline() 219 219 220 220 if (not inp): 221 221 raise EOFError 222 222 223 223 if (not inp.strip()): 224 224 if (position != None): … … 229 229 else: 230 230 inp = '0' 231 231 232 232 if (inp.strip() == 'q'): 233 233 return ('cancel', None) 234 234 235 235 try: 236 236 choice = int(inp.strip()) 237 237 except ValueError: 238 238 continue 239 239 240 240 if (choice == 0): 241 241 return ('done', 0) 242 242 243 243 if (choice < 1) or (choice > len(options)): 244 244 continue 245 245 246 246 return (None, choice - 1) 247 247 248 248 def error_dialog(screen, title, msg): 249 249 "Print error dialog" 250 250 251 251 width = len(msg) 252 252 253 253 if (newt): 254 254 width = width_fix(screen, width) 255 255 256 256 buttonbar = snack.ButtonBar(screen, ['Ok']) 257 257 textbox = snack.TextboxReflowed(width, msg) 258 258 259 259 grid = snack.GridForm(screen, title, 1, 2) 260 260 grid.add(textbox, 0, 0, padding = (0, 0, 0, 1))
Note:
See TracChangeset
for help on using the changeset viewer.