brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.9 KiB · 8b0eced Raw
128 lines · python
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-exception6##7##===----------------------------------------------------------------------===##8 9import cui10import curses11import lldb12from itertools import islice13 14 15class History(object):16    def __init__(self):17        self.data = {}18        self.pos = 019        self.tempEntry = ""20 21    def previous(self, curr):22        if self.pos == len(self.data):23            self.tempEntry = curr24 25        if self.pos < 0:26            return ""27        if self.pos == 0:28            self.pos -= 129            return ""30        if self.pos > 0:31            self.pos -= 132            return self.data[self.pos]33 34    def next(self):35        if self.pos < len(self.data):36            self.pos += 137 38        if self.pos < len(self.data):39            return self.data[self.pos]40        elif self.tempEntry != "":41            return self.tempEntry42        else:43            return ""44 45    def add(self, c):46        self.tempEntry = ""47        self.pos = len(self.data)48        if self.pos == 0 or self.data[self.pos - 1] != c:49            self.data[self.pos] = c50            self.pos += 151 52 53class CommandWin(cui.TitledWin):54    def __init__(self, driver, x, y, w, h):55        super(CommandWin, self).__init__(x, y, w, h, "Commands")56        self.command = ""57        self.data = ""58        driver.setSize(w, h)59 60        self.win.scrollok(1)61 62        self.driver = driver63        self.history = History()64 65        def enterCallback(content):66            self.handleCommand(content)67 68        def tabCompleteCallback(content):69            self.data = content70            matches = lldb.SBStringList()71            commandinterpreter = self.getCommandInterpreter()72            commandinterpreter.HandleCompletion(73                self.data, self.el.index, 0, -1, matches74            )75            if matches.GetSize() == 2:76                self.el.content += matches.GetStringAtIndex(0)77                self.el.index = len(self.el.content)78                self.el.draw()79            else:80                self.win.move(self.el.starty, self.el.startx)81                self.win.scroll(1)82                self.win.addstr("Available Completions:")83                self.win.scroll(1)84                for m in islice(matches, 1, None):85                    self.win.addstr(self.win.getyx()[0], 0, m)86                    self.win.scroll(1)87                self.el.draw()88 89        self.startline = self.win.getmaxyx()[0] - 290 91        self.el = cui.CursesEditLine(92            self.win, self.history, enterCallback, tabCompleteCallback93        )94        self.el.prompt = self.driver.getPrompt()95        self.el.showPrompt(self.startline, 0)96 97    def handleCommand(self, cmd):98        # enter!99        self.win.scroll(1)  # TODO: scroll more for longer commands100        if cmd == "":101            cmd = self.history.previous("")102        elif cmd in ("q", "quit"):103            self.driver.terminate()104            return105 106        self.history.add(cmd)107        ret = self.driver.handleCommand(cmd)108        if ret.Succeeded():109            out = ret.GetOutput()110            attr = curses.A_NORMAL111        else:112            out = ret.GetError()113            attr = curses.color_pair(3)  # red on black114        self.win.addstr(self.startline, 0, out + "\n", attr)115        self.win.scroll(1)116        self.el.showPrompt(self.startline, 0)117 118    def handleEvent(self, event):119        if isinstance(event, int):120            if event == curses.ascii.EOT and self.el.content == "":121                # When the command is empty, treat CTRL-D as EOF.122                self.driver.terminate()123                return124            self.el.handleEvent(event)125 126    def getCommandInterpreter(self):127        return self.driver.getCommandInterpreter()128