1##===-- commandwin.py ----------------------------------------*- Python -*-===## 2## 3# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4# See https://llvm.org/LICENSE.txt for license information. 5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6## 7##===----------------------------------------------------------------------===## 8 9import cui 10import curses 11import lldb 12from itertools import islice 13 14 15class History(object): 16 17 def __init__(self): 18 self.data = {} 19 self.pos = 0 20 self.tempEntry = '' 21 22 def previous(self, curr): 23 if self.pos == len(self.data): 24 self.tempEntry = curr 25 26 if self.pos < 0: 27 return '' 28 if self.pos == 0: 29 self.pos -= 1 30 return '' 31 if self.pos > 0: 32 self.pos -= 1 33 return self.data[self.pos] 34 35 def next(self): 36 if self.pos < len(self.data): 37 self.pos += 1 38 39 if self.pos < len(self.data): 40 return self.data[self.pos] 41 elif self.tempEntry != '': 42 return self.tempEntry 43 else: 44 return '' 45 46 def add(self, c): 47 self.tempEntry = '' 48 self.pos = len(self.data) 49 if self.pos == 0 or self.data[self.pos - 1] != c: 50 self.data[self.pos] = c 51 self.pos += 1 52 53 54class CommandWin(cui.TitledWin): 55 56 def __init__(self, driver, x, y, w, h): 57 super(CommandWin, self).__init__(x, y, w, h, "Commands") 58 self.command = "" 59 self.data = "" 60 driver.setSize(w, h) 61 62 self.win.scrollok(1) 63 64 self.driver = driver 65 self.history = History() 66 67 def enterCallback(content): 68 self.handleCommand(content) 69 70 def tabCompleteCallback(content): 71 self.data = content 72 matches = lldb.SBStringList() 73 commandinterpreter = self.getCommandInterpreter() 74 commandinterpreter.HandleCompletion( 75 self.data, self.el.index, 0, -1, matches) 76 if matches.GetSize() == 2: 77 self.el.content += matches.GetStringAtIndex(0) 78 self.el.index = len(self.el.content) 79 self.el.draw() 80 else: 81 self.win.move(self.el.starty, self.el.startx) 82 self.win.scroll(1) 83 self.win.addstr("Available Completions:") 84 self.win.scroll(1) 85 for m in islice(matches, 1, None): 86 self.win.addstr(self.win.getyx()[0], 0, m) 87 self.win.scroll(1) 88 self.el.draw() 89 90 self.startline = self.win.getmaxyx()[0] - 2 91 92 self.el = cui.CursesEditLine( 93 self.win, 94 self.history, 95 enterCallback, 96 tabCompleteCallback) 97 self.el.prompt = self.driver.getPrompt() 98 self.el.showPrompt(self.startline, 0) 99 100 def handleCommand(self, cmd): 101 # enter! 102 self.win.scroll(1) # TODO: scroll more for longer commands 103 if cmd == '': 104 cmd = self.history.previous('') 105 elif cmd in ('q', 'quit'): 106 self.driver.terminate() 107 return 108 109 self.history.add(cmd) 110 ret = self.driver.handleCommand(cmd) 111 if ret.Succeeded(): 112 out = ret.GetOutput() 113 attr = curses.A_NORMAL 114 else: 115 out = ret.GetError() 116 attr = curses.color_pair(3) # red on black 117 self.win.addstr(self.startline, 0, out + '\n', attr) 118 self.win.scroll(1) 119 self.el.showPrompt(self.startline, 0) 120 121 def handleEvent(self, event): 122 if isinstance(event, int): 123 if event == curses.ascii.EOT and self.el.content == '': 124 # When the command is empty, treat CTRL-D as EOF. 125 self.driver.terminate() 126 return 127 self.el.handleEvent(event) 128 129 def getCommandInterpreter(self): 130 return self.driver.getCommandInterpreter() 131