xref: /llvm-project/lldb/utils/lui/commandwin.py (revision 602e47c5f9fd2e14c7bfb6111e6558fa0d27c87f)
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    def __init__(self):
17        self.data = {}
18        self.pos = 0
19        self.tempEntry = ""
20
21    def previous(self, curr):
22        if self.pos == len(self.data):
23            self.tempEntry = curr
24
25        if self.pos < 0:
26            return ""
27        if self.pos == 0:
28            self.pos -= 1
29            return ""
30        if self.pos > 0:
31            self.pos -= 1
32            return self.data[self.pos]
33
34    def next(self):
35        if self.pos < len(self.data):
36            self.pos += 1
37
38        if self.pos < len(self.data):
39            return self.data[self.pos]
40        elif self.tempEntry != "":
41            return self.tempEntry
42        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] = c
50            self.pos += 1
51
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 = driver
63        self.history = History()
64
65        def enterCallback(content):
66            self.handleCommand(content)
67
68        def tabCompleteCallback(content):
69            self.data = content
70            matches = lldb.SBStringList()
71            commandinterpreter = self.getCommandInterpreter()
72            commandinterpreter.HandleCompletion(
73                self.data, self.el.index, 0, -1, matches
74            )
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] - 2
90
91        self.el = cui.CursesEditLine(
92            self.win, self.history, enterCallback, tabCompleteCallback
93        )
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 commands
100        if cmd == "":
101            cmd = self.history.previous("")
102        elif cmd in ("q", "quit"):
103            self.driver.terminate()
104            return
105
106        self.history.add(cmd)
107        ret = self.driver.handleCommand(cmd)
108        if ret.Succeeded():
109            out = ret.GetOutput()
110            attr = curses.A_NORMAL
111        else:
112            out = ret.GetError()
113            attr = curses.color_pair(3)  # red on black
114        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                return
124            self.el.handleEvent(event)
125
126    def getCommandInterpreter(self):
127        return self.driver.getCommandInterpreter()
128