1 ##===-- commandwin.py ----------------------------------------*- Python -*-===##
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
7 ##===----------------------------------------------------------------------===##
12 from itertools
import islice
15 class History(object):
21 def previous(self
, curr
):
22 if self
.pos
== len(self
.data
):
32 return self
.data
[self
.pos
]
35 if self
.pos
< len(self
.data
):
38 if self
.pos
< len(self
.data
):
39 return self
.data
[self
.pos
]
40 elif 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
53 class CommandWin(cui
.TitledWin
):
54 def __init__(self
, driver
, x
, y
, w
, h
):
55 super(CommandWin
, self
).__init
__(x
, y
, w
, h
, "Commands")
63 self
.history
= History()
65 def enterCallback(content
):
66 self
.handleCommand(content
)
68 def tabCompleteCallback(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
)
80 self
.win
.move(self
.el
.starty
, self
.el
.startx
)
82 self
.win
.addstr("Available Completions:")
84 for m
in islice(matches
, 1, None):
85 self
.win
.addstr(self
.win
.getyx()[0], 0, m
)
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
):
99 self
.win
.scroll(1) # TODO: scroll more for longer commands
101 cmd
= self
.history
.previous("")
102 elif cmd
in ("q", "quit"):
103 self
.driver
.terminate()
106 self
.history
.add(cmd
)
107 ret
= self
.driver
.handleCommand(cmd
)
109 out
= ret
.GetOutput()
110 attr
= curses
.A_NORMAL
113 attr
= curses
.color_pair(3) # red on black
114 self
.win
.addstr(self
.startline
, 0, out
+ "\n", attr
)
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()
124 self
.el
.handleEvent(event
)
126 def getCommandInterpreter(self
):
127 return self
.driver
.getCommandInterpreter()