1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 from libxyz.core.plugins import BasePlugin
18 from libxyz.core import dsl
19 from libxyz.core.utils import bstring
20 from libxyz.core import Queue
21 from libxyz.ui import lowui
22
23 import libxyz.ui as ui
24
26 "Plugin console"
27
28 NAME = u"console"
29 AUTHOR = u"Max E. Kuznecov <syhpoon@syhpoon.name>"
30 VERSION = u"0.1"
31 BRIEF_DESCRIPTION = u"Interactive management console"
32 FULL_DESCRIPTION = u"Provides interactive management console"
33 NAMESPACE = u"core"
34
36 super(XYZPlugin, self).__init__(xyz)
37
38 self.attr = lambda x: self.xyz.skin.get_palette(u"plugin.console", x)
39
40 self._keys= ui.Keys()
41 self._index = 1
42 self.output = []
43 self.edit = lowui.Edit(self.conf["prompt"], wrap="clip")
44 self._input = lowui.AttrWrap(self.edit, self.attr("input"))
45 self._header = lowui.AttrWrap(lowui.Text(_(u"Management console")),
46 self.attr("header"))
47 self._history = Queue(self.conf["history_depth"])
48
49 self.export(self.show)
50
51
52
54 """
55 Show console window
56 """
57
58 def _get_cmd(k):
59 """
60 Fetch previous command from history
61 """
62
63 _i = -1 if k == self._keys.UP else 1
64
65 _pos = len(self._history) - 1 + (self._index + _i)
66
67 if _pos < 0:
68 return None
69 elif _pos > len(self._history):
70 return None
71 else:
72 self._index += _i
73
74 if _pos == len(self._history):
75 return ""
76
77 try:
78 cmd = self._history[_pos]
79 except Exception:
80 return None
81 else:
82 return cmd
83
84
85
86 _stop = False
87
88 while True:
89 walker = lowui.SimpleListWalker(self.output)
90 walker.focus = len(walker) - 1
91 lbox = lowui.AttrWrap(lowui.ListBox(walker), self.attr("output"))
92
93 console = lowui.Frame(lbox, header=self._header,
94 footer=self._input, focus_part='footer')
95 dim = self.xyz.screen.get_cols_rows()
96
97 self.xyz.screen.draw_screen(dim, console.render(dim, True))
98
99 data = self.xyz.input.get()
100
101 for k in data:
102 if k in (self._keys.UP, self._keys.DOWN):
103 cmd = _get_cmd(k)
104
105 if cmd is not None:
106 self.edit.set_edit_text("")
107 self.edit.insert_text(cmd)
108 elif k == self._keys.ENTER:
109 self._index = 1
110 chunk = self.edit.get_edit_text()
111 self.edit.set_edit_text("")
112 compiled = None
113
114 if not chunk:
115 continue
116
117 self._history.push(chunk)
118
119 self._write("> %s" % chunk)
120
121 try:
122 compiled = compile(chunk, "<input>", "eval")
123 except Exception, e:
124 self._write(str(e))
125 break
126 else:
127
128 if compiled is None:
129 break
130 else:
131 chunk = ""
132 try:
133 self._write(
134 eval(compiled, dsl.XYZ.get_env()))
135 except Exception, e:
136 self._write(str(e))
137 elif k == self._keys.ESCAPE:
138 _stop = True
139 break
140 else:
141 self._input.keypress((dim[0],), k)
142
143 if _stop:
144 break
145
146
147
149 """
150 Write text to output
151 """
152
153 self.output.extend([lowui.Text(x) for x in bstring(msg).split("\n")])
154