summary refs log tree commit diff stats
path: root/ranger/gui
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-02 14:29:10 +0100
committerhut <hut@lavabit.com>2009-12-02 14:29:10 +0100
commite6dfc442a984bc50a70be5017f4ab9a0e6efadef (patch)
treef2cf064015ca93b6070c5253378af0826ce16937 /ranger/gui
parent09bba8ddbe8af1cd6bf7dede1c6c7c53a01fc2a8 (diff)
downloadranger-e6dfc442a984bc50a70be5017f4ab9a0e6efadef.tar.gz
implemented a console (without commands)
Diffstat (limited to 'ranger/gui')
-rw-r--r--ranger/gui/defaultui.py28
-rw-r--r--ranger/gui/ui.py55
-rw-r--r--ranger/gui/wconsole.py105
-rw-r--r--ranger/gui/widget.py10
4 files changed, 165 insertions, 33 deletions
diff --git a/ranger/gui/defaultui.py b/ranger/gui/defaultui.py
index d4d89047..547f20ff 100644
--- a/ranger/gui/defaultui.py
+++ b/ranger/gui/defaultui.py
@@ -1,11 +1,12 @@
-from ranger.gui.ui import UI as SuperClass
-from ranger.gui.wdisplay import WDisplay
-from ranger.gui.wtitlebar import WTitleBar
 
 RATIO = ( 0.15, 0.15, 0.4, 0.3 )
 
+from ranger.gui.ui import UI as SuperClass
 class DefaultUI(SuperClass):
 	def setup(self):
+		from ranger.gui.wdisplay import WDisplay
+		from ranger.gui.wtitlebar import WTitleBar
+		from ranger.gui.wconsole import WConsole
 		self.titlebar = WTitleBar(self.win, self.colorscheme)
 		self.add_widget(self.titlebar)
 
@@ -19,9 +20,9 @@ class DefaultUI(SuperClass):
 		self.displays[2].main_display = True
 		for disp in self.displays:
 			self.add_widget(disp)
-	
-	def scroll(self, relative):
-		self.main_display.scroll(relative)
+
+		self.console = WConsole(self.win, self.colorscheme)
+		self.add_widget(self.console)
 
 	def resize(self):
 		SuperClass.resize(self)
@@ -33,11 +34,24 @@ class DefaultUI(SuperClass):
 		for ratio in RATIO:
 			wid = int(ratio * x)
 			try:
-				self.displays[i].setdim(1, leftborder, y-1, wid - 1)
+				self.displays[i].setdim(1, leftborder, y-2, wid - 1)
 			except KeyError:
 				pass
 			leftborder += wid
 			i += 1
 
 		self.titlebar.setdim(0, 0, 1, x)
+		self.console.setdim(y-1, 0, 1, x)
+
+	# ---specials---
+	def open_console(self, mode):
+		if self.console.open(mode):
+			self.console.on_close = self.close_console
+			self.console.visible = True
+
+	def close_console(self):
+		self.console.visible = False
+
+	def scroll(self, relative):
+		self.main_display.scroll(relative)
 
diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py
index a04455ab..79077cbc 100644
--- a/ranger/gui/ui.py
+++ b/ranger/gui/ui.py
@@ -1,5 +1,22 @@
 import curses
 
+class MouseEvent():
+	import curses
+	PRESSED = [ 0,
+			curses.BUTTON1_PRESSED,
+			curses.BUTTON2_PRESSED,
+			curses.BUTTON3_PRESSED,
+			curses.BUTTON4_PRESSED ]
+
+	def __init__(self, getmouse):
+		_, self.x, self.y, _, self.bstate = getmouse
+	
+	def pressed(self, n):
+		try:
+			return (self.bstate & MouseEvent.PRESSED[n]) != 0
+		except:
+			return False
+
 class UI():
 	def __init__(self, env, commandlist, colorscheme):
 		self.env = env
@@ -15,7 +32,7 @@ class UI():
 		os.environ['ESCDELAY'] = '25'
 
 		self.win = curses.initscr()
-		self.win.leaveok(1)
+		self.win.leaveok(0)
 		self.win.keypad(1)
 
 		curses.noecho()
@@ -51,10 +68,10 @@ class UI():
 			else:
 				fm.scroll(relative = 3)
 
-	def setup(self):
-		pass
+	def can(self, attr):
+		return hasattr(self, attr)
 
-	def scroll(self, relative):
+	def setup(self):
 		pass
 
 	def resize(self):
@@ -74,8 +91,10 @@ class UI():
 	def press(self, key, fm):
 		self.env.key_append(key)
 
-#		from ranger.helper import log
-#		log(self.env.keybuffer)
+		for widg in self.widgets:
+			if widg.focused:
+				widg.press(key, fm, self.env)
+				return
 
 		try:
 			cmd = self.commandlist.paths[self.env.keybuffer]
@@ -98,28 +117,14 @@ class UI():
 		self.win.erase()
 		for widg in self.widgets:
 			widg.feed_env(self.env)
-			widg.draw()
+			if widg.visible:
+				widg.draw()
+		for widg in self.widgets:
+			if widg.visible:
+				widg.finalize()
 		self.win.refresh()
 
 	def get_next_key(self):
 		key = self.win.getch()
 		curses.flushinp()
 		return key
-
-
-class MouseEvent():
-	import curses
-	PRESSED = [ 0,
-			curses.BUTTON1_PRESSED,
-			curses.BUTTON2_PRESSED,
-			curses.BUTTON3_PRESSED,
-			curses.BUTTON4_PRESSED ]
-
-	def __init__(self, getmouse):
-		_, self.x, self.y, _, self.bstate = getmouse
-	
-	def pressed(self, n):
-		try:
-			return (self.bstate & MouseEvent.PRESSED[n]) != 0
-		except:
-			return False
diff --git a/ranger/gui/wconsole.py b/ranger/gui/wconsole.py
new file mode 100644
index 00000000..4411e83b
--- /dev/null
+++ b/ranger/gui/wconsole.py
@@ -0,0 +1,105 @@
+from ranger.gui.widget import Widget as SuperClass
+import curses
+
+CONSOLE_MODES = tuple(':/?>!')
+
+class WConsole(SuperClass):
+	def __init__(self, win, colorscheme):
+		from ranger.command import CommandList
+		from ranger.conf.keys import initialize_console_commands
+		SuperClass.__init__(self, win, colorscheme)
+		self.mode = None
+		self.visible = False
+		self.commandlist = CommandList()
+		initialize_console_commands(self.commandlist)
+		self.last_cursor_mode = 1
+		self.clear()
+
+	def draw(self):
+		if self.mode is None:
+			return
+
+		self.win.addstr(self.y, self.x, ":" + self.line)
+
+	def finalize(self):
+		try:
+			self.win.move(self.y, self.x + self.pos + 1)
+		except:
+			pass
+
+	def open(self, mode):
+		if mode not in CONSOLE_MODES:
+			return False
+
+		self.last_cursor_mode = curses.curs_set(1)
+		self.mode = mode
+		self.focused = True
+		self.visible = True
+		return True
+
+	def close(self):
+		curses.curs_set(self.last_cursor_mode)
+		self.focused = False
+		self.visible = False
+		if hasattr(self, 'on_close'):
+			self.on_close()
+	
+	def clear(self):
+		self.pos = 0
+		self.line = ''
+	
+	def press(self, key, fm, env):
+		from curses.ascii import ctrl, ESC
+		from ranger.helper import log
+		log(key)
+
+		try:
+			cmd = self.commandlist.paths[env.keybuffer]
+		except KeyError:
+			env.key_clear()
+			return
+
+		if cmd == self.commandlist.dummy_object:
+			return
+
+		cmd.execute(self, fm)
+		env.key_clear()
+
+	def type_key(self, key):
+		if isinstance(key, int):
+			key = chr(key)
+
+		if self.pos == len(self.line):
+			self.line += key
+		else:
+			self.line = self.line[:self.pos] + key + self.line[self.pos:]
+
+		self.pos += len(key)
+
+	def move(self, relative = 0, absolute = None):
+		if absolute is not None:
+			if absolute < 0:
+				self.pos = len(self.line) + 1 + absolute
+			else:
+				self.pos = absolute
+
+		self.pos = min(max(0, self.pos + relative), len(self.line))
+
+	def delete_rest(self, direction):
+		if direction > 0:
+			self.line = self.line[:self.pos]
+		else:
+			self.line = self.line[self.pos:]
+			self.pos = 0
+	
+	def delete(self, mod):
+		pos = self.pos + mod
+
+		self.line = self.line[0:pos] + self.line[pos+1:]
+		self.move(relative = mod)
+
+	def execute(self):
+		self.line = ''
+		self.pos = 0
+		self.close()
+
diff --git a/ranger/gui/widget.py b/ranger/gui/widget.py
index f9a0651f..fa1ca585 100644
--- a/ranger/gui/widget.py
+++ b/ranger/gui/widget.py
@@ -11,7 +11,9 @@ def combine(keylist, keys):
 class Widget():
 	def __init__(self, win, colorscheme):
 		self.win = win
+		self.focused = False
 		self.colorscheme = colorscheme
+		self.visible = True
 		self.setdim(0, 0, 0, 0)
 
 	def color(self, keylist = None, *keys):
@@ -49,7 +51,7 @@ class Widget():
 		return (x >= self.x and x < self.x + self.wid) and \
 				(y >= self.y and y < self.y + self.hei)
 
-	def feed_env(self):
+	def feed_env(self, env):
 		pass
 
 	def feed(self):
@@ -57,9 +59,15 @@ class Widget():
 
 	def click(self, event, fm):
 		pass
+
+	def press(self, key, fm):
+		pass
 	
 	def draw(self):
 		pass
 
+	def finalize(self):
+		pass
+
 	def destroy(self):
 		pass
d60c3bdb9d605'>^
b0bf5321 ^
a90faae9 ^

1ead3562 ^
a90faae9 ^

b0bf5321 ^
a90faae9 ^


b0bf5321 ^

a90faae9 ^
dd30f56d ^







a90faae9 ^
fa94f4d9 ^
a90faae9 ^
dd30f56d ^
a90faae9 ^

b0bf5321 ^
a90faae9 ^
fa94f4d9 ^
a90faae9 ^
dd30f56d ^





8ee53369 ^

dd30f56d ^
9c2421ec ^






















8ee53369 ^
9c2421ec ^

















8ee53369 ^
9c2421ec ^
dd30f56d ^
a90faae9 ^


b4bccf49 ^
b0bf5321 ^
b4bccf49 ^



b0bf5321 ^
b4bccf49 ^
350ca0c6 ^
b0bf5321 ^

ea5e7fd4 ^


b4bccf49 ^

a90faae9 ^

fa94f4d9 ^
a90faae9 ^

7bf9212f ^
a90faae9 ^

b0bf5321 ^
a90faae9 ^

7bf9212f ^
a90faae9 ^


b0bf5321 ^
a90faae9 ^

ea5e7fd4 ^


a90faae9 ^
4e7186fa ^
ea5e7fd4 ^
a90faae9 ^





b0bf5321 ^
a90faae9 ^
350ca0c6 ^
a90faae9 ^
350ca0c6 ^
a90faae9 ^




b0bf5321 ^
a90faae9 ^








bbc28b80 ^
a90faae9 ^




1ead3562 ^
a90faae9 ^
1ead3562 ^
a90faae9 ^






b0bf5321 ^
a90faae9 ^
afdda1ea ^
a90faae9 ^
8beff53e ^
a90faae9 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258