[SDAG] Generalize FSINCOS type legalization (NFC) (#116848)
[llvm-project.git] / lldb / utils / lui / commandwin.py
blob8b0ecedc7d33ad94ceac789adab6bafe5779f3a7
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 ##===----------------------------------------------------------------------===##
9 import cui
10 import curses
11 import lldb
12 from itertools import islice
15 class History(object):
16 def __init__(self):
17 self.data = {}
18 self.pos = 0
19 self.tempEntry = ""
21 def previous(self, curr):
22 if self.pos == len(self.data):
23 self.tempEntry = curr
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]
34 def next(self):
35 if self.pos < len(self.data):
36 self.pos += 1
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 ""
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
53 class 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)
60 self.win.scrollok(1)
62 self.driver = driver
63 self.history = History()
65 def enterCallback(content):
66 self.handleCommand(content)
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
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()
89 self.startline = self.win.getmaxyx()[0] - 2
91 self.el = cui.CursesEditLine(
92 self.win, self.history, enterCallback, tabCompleteCallback
94 self.el.prompt = self.driver.getPrompt()
95 self.el.showPrompt(self.startline, 0)
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
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)
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)
126 def getCommandInterpreter(self):
127 return self.driver.getCommandInterpreter()