Permalink
Browse files

"Restart" binding (\)

This allows you to change your config, hit a key and immediately see changes. A
step up from 'q''up''enter', but more useful as a precursor for some hook
action.

Signed-off-by: Jack Miller <jack@codezen.org>
  • Loading branch information...
1 parent 3b22bd5 commit 7c987cfa9f954a95c87f872133b87dc18f99a800 @themoken committed Jan 27, 2010
Showing with 62 additions and 38 deletions.
  1. +5 −2 bin/canto
  2. +1 −0 canto/cfg/keys.py
  3. +1 −0 canto/const.py
  4. +3 −0 canto/gui.py
  5. +52 −36 canto/main.py
View
@@ -2,5 +2,8 @@
import canto.main
-if __name__ == "__main__" :
- canto.main.Main()
+if __name__ == "__main__" :
+ c = canto.main.Main()
+ while c.restart:
+ c = canto.main.Main(c.cfg.stdscr)
+ sys.exit(0)
View
@@ -54,6 +54,7 @@ def register(c):
"KEY_LEFT" : "just_unread",
"KEY_NPAGE" : "next_tag",
"KEY_PPAGE" : "prev_tag",
+ "\\" : "restart",
"[" : "prev_filter",
"]" : "next_filter",
"{" : "prev_tag_filter",
View
@@ -26,6 +26,7 @@
REFILTER = 8
RETAG = 9
TFILTER = 10
+RESTART = 11
STORY_SAVED = 0
STORY_UPDATED = 1
View
@@ -706,6 +706,9 @@ def all_unread(self):
for t in self.tags :
t.all_unread()
+ def restart(self):
+ return RESTART
+
def quit(self):
if self.cfg.end_hook:
self.cfg.end_hook(self)
View
@@ -52,7 +52,7 @@ def upgrade_help():
print "http://codezen.org/canto/config/#upgrading-from-06x"
class Main():
- def __init__(self):
+ def __init__(self, stdscr = None):
signal.signal(signal.SIGUSR2, self.debug_out)
# Let locale figure itself out
@@ -87,6 +87,16 @@ def __init__(self):
self.cfg.log("Time: %s" % time.asctime())
self.cfg.log("Config parsed successfully.")
+ # If we were passed an existing curses screen (i.e. restart)
+ # pass it through to the config.
+
+ self.cfg.stdscr = stdscr
+ if self.cfg.stdscr:
+ self.restarting = True
+ else:
+ self.restarting = False
+ self.restart = False
+
# Default arguments.
flags = 0
feed_ct = None
@@ -267,24 +277,25 @@ def __init__(self):
# the client, so we fire up ncurses and add the screen
# information to our Cfg().
- self.cfg.stdscr = curses.initscr()
- self.cfg.stdscr.nodelay(1)
-
- # curs_set can return ERR, we shouldn't care
- try:
- curses.curs_set(0)
- except:
- pass
-
- # if any of these mess up though, the rest of the
- # the operation is suspect, so die.
- try:
- curses.noecho()
- curses.start_color()
- curses.use_default_colors()
- except:
- self.cfg.log("Unable to init curses, bailing")
- self.done()
+ if not self.restarting:
+ self.cfg.stdscr = curses.initscr()
+ self.cfg.stdscr.nodelay(1)
+
+ # curs_set can return ERR, we shouldn't care
+ try:
+ curses.curs_set(0)
+ except:
+ pass
+
+ # if any of these mess up though, the rest of the
+ # the operation is suspect, so die.
+ try:
+ curses.noecho()
+ curses.start_color()
+ curses.use_default_colors()
+ except:
+ self.cfg.log("Unable to init curses, bailing")
+ self.done()
self.sigusr = 0
self.resize = 0
@@ -301,7 +312,7 @@ def __init__(self):
self.cfg.log("Unable to init curses color pairs!")
self.cfg.log("Curses initialized.")
-
+
# Instantiate the base Gui class
self.gui = Gui(self.cfg, self.cfg.tags.cur())
@@ -322,8 +333,9 @@ def __init__(self):
# shitting all over the terminal.
try:
- # Initial draw of the screen
- self.refresh()
+ # Initial draw of the screen, if not restarting
+ self.refresh(self.restarting)
+ self.restarting = False
# Main program loop, terminated when all handlers have
# deregistered / exited.
@@ -440,6 +452,9 @@ def __init__(self):
self.update(1, ufds)
elif r == REDRAW_ALL:
self.gui.draw_elements()
+ elif r == RESTART:
+ self.restart = True
+ self.gui = None
elif r == EXIT:
self.gui = None
break
@@ -461,12 +476,13 @@ def done(self, a=None, b=None):
self.cfg.msg = None
# Kill curses
- try:
- curses.endwin()
- except:
- pass
+ if not self.restart:
+ try:
+ curses.endwin()
+ except:
+ pass
- self.cfg.log("Curses done.")
+ self.cfg.log("Curses done.")
# If there was an exception, nicely print it out.
if self.estring:
@@ -493,7 +509,6 @@ def done(self, a=None, b=None):
os.dup2(fd, sys.stderr.fileno())
self.ph.kill_process()
- sys.exit(0)
# For the most part, it's smart to avoid doing anything but set a flag in an
# signal handler. CHLD is an exception because the only case in which we do
@@ -607,16 +622,17 @@ def update(self, refilter = 0, iter = None, action=PROC_BOTH):
# Refresh should only be called when it's possible that the screen has
# changed shape.
- def refresh(self):
+ def refresh(self, restart = False):
# Get new self.cfg.{height, width}
- try:
- curses.endwin()
- except:
- pass
+ if not restart:
+ try:
+ curses.endwin()
+ except:
+ pass
- self.cfg.stdscr.touchwin()
- self.cfg.stdscr.refresh()
- self.cfg.stdscr.keypad(1)
+ self.cfg.stdscr.touchwin()
+ self.cfg.stdscr.refresh()
+ self.cfg.stdscr.keypad(1)
self.cfg.height, self.cfg.width = self.cfg.stdscr.getmaxyx()

0 comments on commit 7c987cf

Please sign in to comment.