Skip to content

Commit 545ca24

Browse files
author
Sebastian Ramacher
committed
Extend urwid's Statusbar.
1 parent b58f201 commit 545ca24

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

bpython/urwid.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,52 @@ class Statusbar(object):
167167
The "widget" attribute is an urwid widget.
168168
"""
169169

170-
def __init__(self, config, s=None):
170+
def __init__(self, config, s=None, main_loop=None):
171171
self.config = config
172+
self.timer = None
173+
self.main_loop = main_loop
172174
self.s = s or ''
173175

174-
# XXX wrap in AttrMap for wrapping?
175176
self.widget = urwid.Text(('main', self.s))
177+
# use wrap mode 'clip' to just cut off at the end of line
178+
self.widget.set_wrap_mode('clip')
179+
180+
def _check(self, callback, userdata=None):
181+
"""This is the method is called from the timer to reset the status bar."""
182+
self.timer = None
183+
self.settext(self.s)
184+
185+
def message(self, s, n=3):
186+
"""Display a message for a short n seconds on the statusbar and return
187+
it to its original state."""
188+
189+
self.settext(s)
190+
self.timer = self.main_loop.set_alarm_in(n, self._check)
191+
192+
def prompt(self, s=''):
193+
"""Prompt the user for some input (with the optional prompt 's') and
194+
return the input text, then restore the statusbar to its original
195+
value."""
196+
197+
# TODO
198+
return ''
199+
200+
def settext(self, s, permanent=False):
201+
"""Set the text on the status bar to a new value. If permanent is True,
202+
the new value will be permanent."""
203+
204+
if self.timer is not None:
205+
self.main_loop.remove_alarm(self.timer)
206+
self.timer = None
207+
208+
self.widget.set_text(('main', s))
209+
if permanent:
210+
self.s = s
211+
212+
def clear(self):
213+
"""Clear the status bar."""
214+
self.settext('')
215+
176216

177217
def decoding_input_filter(keys, raw):
178218
"""Input filter for urwid which decodes each key with the locale's
@@ -476,6 +516,7 @@ def __init__(self, event_loop, palette, interpreter, config):
476516
self.frame, palette,
477517
event_loop=event_loop, unhandled_input=self.handle_input,
478518
input_filter=input_filter, handle_mouse=False)
519+
self.statusbar.main_loop = self.main_loop
479520

480521
self.edits = []
481522
self.edit = None

0 commit comments

Comments
 (0)