1. Mike Bayer
  2. sliderepl

Commits

Mike Bayer  committed 540687c

- remove some commands we don't use
- better help screen
- hook to allow SQL /logging highlighting

  • Participants
  • Parent commits 5df937a
  • Branches default

Comments (0)

Files changed (2)

File sliderepl/core.py

View file
  • Ignore whitespace
 
 class Deck(object):
     expose = ('next', 'goto', 'show', 'info', 'prev',
-              'rerun', 'presentation', 'timer')
+              'rerun', 'presentation')
 
     _exec_on_return = False
 
         self.init_slide = None
         self.color = options.get('color', None)
         self._set_presentation(options.get('presentation', False))
-        self._set_show_timer(options.get('timer', False))
         self.pending_exec = False
         self._letter_commands = {}
         self._expose_map = dict(("!%s" % name, getattr(self, name))
         self._expose_map['?'] = self.commands
 
     @property
+    def highlight_stdout(self):
+        return sys.stdout
+
+    @property
     def ps1(self):
         return getattr(sys, 'ps1', '>>> ')
 
     def start(self):
         pass
 
-    def _set_show_timer(self, mode):
-        self._show_timer = mode
-        print("%% show timer is now %s" % (self._show_timer and "ON" or "OFF"))
-
     def _set_presentation(self, mode):
         self._presentation = mode
         print("%% presentation mode is now %s" %
         """Toggle presentation mode"""
         self._set_presentation(not self._presentation)
 
-    def timer(self):
-        """Show current time in slide header"""
-
-        self._set_show_timer(not self._show_timer)
-
     def rerun(self):
         """Re-run the current slide."""
 
 
     def _do_slide(self, num, run=True, echo=True):
         slide = self.slides[num - 1]
-        if self._show_timer:
-            timer = " " + datetime.now().strftime("%H:%M:%S")
-        else:
-            timer = ""
         if echo:
             if self._presentation:
                 if not slide.no_clear:
                     os.system(clearcmd)
-                    print(slide._banner(timer))
+                    print(slide._banner())
                 else:
                     print("")
-                    print(slide._banner(timer))
+                    print(slide._banner())
             else:
-                print(slide._banner(timer))
+                print(slide._banner())
 
         slide.run(run=run, echo=echo)
         if run != 'force' and getattr(slide, 'no_exec', False):
 
     @slide_actor
     def show(self, slide_number):
-        """show NUM, display a slide without executing it."""
+        """show slide <number>, display a slide without executing it."""
 
         self._do_slide(slide_number, False)
 
     @slide_actor
     def goto(self, slide_number):
-        """goto NUM, skip forward to another slide or go
-        back to a previous slide."""
+        """goto slide <number>"""
 
         self.pending_exec = False
         if slide_number <= self.current:
     def commands(self):
         """Display this help message."""
         for cmd in ['?'] + [
-               "!%s" % exp for exp in self.expose
-            ]:
-            print("% " + cmd + \
-                (cmd in self._letter_commands and " / " +
-                                self._letter_commands[cmd] or ""))
-            print("%\t" + self._expose_map[cmd].__doc__)
+                    "!%s" % exp for exp in self.expose
+                ]:
+
+            line_start = "% " + cmd + \
+                        (cmd in self._letter_commands and " / " +
+                        self._letter_commands[cmd] or "")
+
+            space = " " * (25 - len(line_start))
+            print(line_start + space + self._expose_map[cmd].__doc__)
 
     del slide_actor
 
             self.file = file
             self.index = index
 
-
-
-        def _banner(self, timer):
+        def _banner(self):
             # not doing the full banners for now
 
             banner = ""
 
             box_size = 63
 
-            if False:
-                header = "%s / %d" % (self.file, self.index)
-                if timer:
-                    header += " / " + timer
-
-                if header:
-                    box_size = max(box_size, len(header))
-            else:
-                if not self.title and not self.intro:
-                    return ""
+            if not self.title and not self.intro:
+                return ""
 
             title = None
 
                         else:
                             to_show = l
 
-                        to_show = to_show #.rstrip()
+                        to_show = to_show
 
                         if not run and \
                                 last_block and \
                         shown.append(to_show)
 
                     Deck._add_history(''.join(display).rstrip())
-                    shown = ''.join(shown)   #+ "\n" #.rstrip() + "\n"
+                    shown = ''.join(shown)
 
                     if last_block:
                         shown = shown.rstrip() + "\n"
                     sys.stdout.write(self.deck._highlight_text(shown))
 
-                    #if len(display) > 1:
-                    #    if not re.match(r'#[\s\n]', display[0]) or \
-                    #       (i + 1 < len(self.codeblocks) and
-                    #            not re.match(r'#[\s\n]',
-                    #                            self.codeblocks[i + 1][0][0])):
-                    #        print("")
-
                 if run:
                     try:
                         exec_(co, environ)

File sliderepl/hairy.py

View file
  • Ignore whitespace
 
 from pygments.token import Comment
 from pygments.formatters.terminal import TERMINAL_COLORS
+import sys
+
 
 scheme = TERMINAL_COLORS.copy()
 scheme[Comment] = ('teal', 'turquoise')
 _pycon_lexer = get_lexer_by_name('pycon')
 
 
+class HighlightOutput(object):
+    def __init__(self, deck, lexer):
+        self.deck = deck
+        self.lexer = lexer
+
+    def write(self, text):
+        sys.stdout.write(self.deck._highlight_text(text, self.lexer))
+
+    def __getattr__(self, key):
+        return getattr(sys.stdout, key)
 
 class Deck(core.Deck):
     expose = core.Deck.expose + ("highlight",)
         core.Deck.__init__(self, path, **options)
         self._highlight = True
 
+    def highlight_stdout(self, lexer):
+        lexer = get_lexer_by_name(lexer)
+        return HighlightOutput(self, lexer)
+
     def highlight(self):
         """Toggle code highlighting."""
         self._highlight = not self._highlight
         print("%% Code highlighting is now %s" %
                 (self._highlight and "ON" or "OFF"))
 
-    def _highlight_text(self, text):
+    def _highlight_text(self, text, lexer=_pycon_lexer):
         bg = self.color == 'dark' and 'dark' or 'light'
-        if self.color in ('auto', 'light', 'dark'):
+        if self._highlight and \
+                self.color in ('auto', 'light', 'dark'):
             whitespace = re.match(r'(.*?)(\s+)$', text, re.S)
             if whitespace:
                 text = whitespace.group(1)
                 whitespace = whitespace.group(2)
-            content = highlight(
-                text, _pycon_lexer,
-                    TerminalFormatter(bg=bg, colorscheme=scheme))
+            if text.strip():
+                content = highlight(
+                    text, lexer,
+                        TerminalFormatter(bg=bg, colorscheme=scheme)).rstrip()
+            else:
+                content = text
             if whitespace:
-                content = content.rstrip() + whitespace
+                content = content + whitespace
         else:
             content = text
         return content