Commits

J.A. Roberts Tunney committed 82dc5d1

random fixes

Comments (0)

Files changed (7)

fabulous/color.py

     width = utils.term.width
     print >>strm, bold(title.center(width)).as_utf8
     print >>strm, bold((bar * width)[:width]).as_utf8
+
+
+def main(args):
+    """I provide a command-line interface for this module
+    """
+    section("Fabulous 4-Bit Colors")
+
+    print ("style(...): " +
+           bold("bold") +" "+
+           underline("underline") +" "+
+           flip("flip") +
+           " (YMMV: " + italic("italic") +" "+
+           underline2("underline2") +" "+
+           strike("strike") +" "+
+           blink("blink") + ")\n").as_utf8
+
+    print ("color(...)           " +
+           black("black") +" "+
+           red("red") +" "+
+           green("green") +" "+
+           yellow("yellow") +" "+
+           blue("blue") +" "+
+           magenta("magenta") +" "+
+           cyan("cyan") +" "+
+           white("white")).as_utf8
+
+    print ("bold(color(...))     " +
+           bold(black("black") +" "+
+                red("red") +" "+
+                green("green") +" "+
+                yellow("yellow") +" "+
+                blue("blue") +" "+
+                magenta("magenta") +" "+
+                cyan("cyan") +" "+
+                white("white"))).as_utf8
+
+    print plain(
+        'highlight_color(...) ',
+        highlight_black('black'), ' ', highlight_red('red'), ' ',
+        highlight_green('green'), ' ', highlight_yellow('yellow'), ' ',
+        highlight_blue('blue'), ' ', highlight_magenta('magenta'), ' ',
+        highlight_cyan('cyan'), ' ', highlight_white('white')).as_utf8
+
+    print ("bold(color_bg(...))  " +
+           bold(black_bg("black") +" "+
+                red_bg("red") +" "+
+                green_bg("green") +" "+
+                yellow_bg("yellow") +" "+
+                blue_bg("blue") +" "+
+                magenta_bg("magenta") +" "+
+                cyan_bg("cyan") +" "+
+                white_bg("white"))).as_utf8
+
+    section("Fabulous 8-Bit Colors")
+
+    for code in ["bold(fg256('red', ' lorem ipsum '))",
+                 "bold(bg256('#ff0000', ' lorem ipsum '))",
+                 "highlight256((255, 0, 0), ' lorem ipsum ')",
+                 "highlight256('#09a', ' lorem ipsum ')",
+                 "highlight256('green', ' lorem ipsum ')",
+                 "highlight256('magenta', ' lorem ipsum ')",
+                 "highlight256('indigo', ' lorem ipsum ')",
+                 "highlight256('orange', ' lorem ipsum ')",
+                 "highlight256('orangered', ' lorem ipsum ')"]:
+        print "%-42s %s" % (code, eval(code))
+    print ''
+
+    # grayscales
+    line = " "
+    for xc in range(232, 256):
+        line += bg256(xc, '  ')
+    print line
+    line = " "
+    for xc in range(232, 256)[::-1]:
+        line += bg256(xc, '  ')
+    print line
+    print ''
+
+    cube_color = lambda x,y,z: 16 + x + y*6 + z*6*6
+    for y in range(6):
+        line = " "
+        for z in range(6):
+            for x in range(6):
+                line += bg256(cube_color(x, y, z), '  ')
+            line += " "
+        print line.as_utf8
+

fabulous/debug.py

 
 
 class DebugImage(image.Image):
-    """Visualize Optimization Techniques Used By :class:`Image`
+    """Visualize optimization techniques used by :class:`Image`
     """
 
     def reduce(self, colors):
     print text.Text("Fabulous")
     wait()
 
-    print bold(imp + 'print text.Text("Fabulous", shadow=True, scew=5)\n')
-    print text.Text("Fabulous", shadow=True, scew=5)
+    print bold(imp + 'print text.Text("Fabulous", shadow=True, skew=5)\n')
+    print text.Text("Fabulous", shadow=True, skew=5)
     wait()
 
 

fabulous/gotham.py

 def lorem_gotham():
     """Cheesy Gothic Poetry Generator
 
+    Uses Python generators to yield eternal angst.
+
     When you need to generate random verbiage to test your code or
-    design, let's face it... Lorem Ipsum and "the quick brown fox" are
-    old and boring!
+    typographic design, let's face it... Lorem Ipsum and "the quick
+    brown fox" are old and boring!
 
-    What we need is something with flavor, the kind of thing a
+    What you need is something with *flavor*, the kind of thing a
     depressed teenager with a lot of black makeup would write.
     """
     w = lambda l: l[random.randrange(len(l))]

fabulous/image.py

     background color.
 
     I also put a lot of work into optimizing the output line-by-line
-    so it needs as few ANSI escape sequences as possible.  You can use
-    :class:`DebugImage` to visualize these optimizations.
+    so it needs as few ANSI escape sequences as possible.  If your
+    terminal is kinda slow, you're gonna want to buy me a drink ;) You
+    can use :class:`DebugImage` to visualize these optimizations.
+
+    The generated output will only include spaces with different
+    background colors.  In the future routines will be provided to
+    overlay text on top of these images.
+
     """
 
     pad = ' '
         utils.pil_check()
         from PIL import Image as PillsPillsPills
         self.img = PillsPillsPills.open(path)
+        # when reading pixels, gifs will return colors corresponding
+        # to a palette if we don't do this :\
+        self.img = self.img.convert("RGBA")
         self.resize(width)
 
-    def __str__(self):
-        return "\n".join(self)
+    def __iter__(self):
+        """I allow Image to behave as an iterable
 
-    def __iter__(self):
+        By using me with a for loop, you can use each line as they're
+        created.  When printing a large image, this helps you not have
+        to wait for the whole thing to be converted.
+
+        :return: Yields lines of text (without line end character)
+        """
         # strip out blank lines
         for line in self.reduce(self.convert()):
             if line.strip():
                 yield line
         yield ""
 
+    def __str__(self):
+        """I return the entire image as one big string
+
+        Unlike the iteration approach, you have to wait for the entire
+        image to be converted.
+
+        :return: String containing all lines joined together.
+        """
+        return "\n".join(self)
+
     @property
     def size(self):
+        """Returns size of image
+        """
         return self.img.size
 
     def resize(self, width=None):
+        """Resizes image to fit inside terminal
+
+        Called by the constructor automatically.
+        """
         (iw, ih) = self.size
         if width is None:
             width = min(iw, utils.term.width)
         self.img = self.img.resize((width, height))
 
     def reduce(self, colors):
+        """Converts color codes into optimized text
+
+        This optimizer works by merging adjacent colors so we don't
+        have to repeat the same escape codes for each pixel.  There is
+        no loss of information.
+
+        :param colors: Iterable yielding an xterm color code for each
+                       pixel, None to indicate a transparent pixel, or
+                       ``'EOL'`` to indicate th end of a line.
+
+        :return: Yields lines of optimized text.
+
+        """
         need_reset = False
         line = []
         for color, items in itertools.groupby(colors):
                     color, self.pad * len(list(items))))
 
     def convert(self):
+        """Yields xterm color codes for each pixel in image
+        """
         (width, height) = self.img.size
+        bgcolor = utils.term.bgcolor
         pix = self.img.load()
         for y in xrange(height):
             for x in xrange(width):
                     yield xterm256.rgb_to_xterm(*rgba[:3])
                 else:
                     color = gf.Color.NewFromRgb(*[c / 255.0 for c in rgba])
-                    rgba = gf.Color.AlphaBlend(color, utils.term.bgcolor).rgb
+                    rgba = gf.Color.AlphaBlend(color, bgcolor).rgb
                     yield xterm256.rgb_to_xterm(
                         *[int(c * 255.0) for c in rgba])
             yield "EOL"
 def main(args):
     """I provide a command-line interface for this module
     """
-    for imgpath in args[1:]:
+    for imgpath in args:
         for line in Image(imgpath):
             print line
 
 
 if __name__ == '__main__':
-    main(sys.argv)
+    main(sys.argv[1:])
     """I provide a command-line interface for this module
     """
     import optparse
-    parser = optparse.OptionParser(args)
+    parser = optparse.OptionParser()
     parser.add_option(
         "-S", "--skew", dest="skew", type="int", default=None,
         help=("Apply skew effect (measured in pixels) to make it look "
     parser.add_option(
         "-s", "--shadow", dest="shadow", action="store_true", default=False,
         help=("Size of font in points.  Default: %default"))
-    (options, args) = parser.parse_args()
+    (options, args) = parser.parse_args(args=args)
 
     if options.term_color:
         utils.term.bgcolor = options.term_color
 
-    text = " ".join(args)
-    fab_text = Text(text, skew=options.skew, color=options.color,
-                    font=options.font, fsize=options.fsize,
-                    shadow=options.shadow)
-    for line in fab_text:
-        print line
+    for line in " ".join(args).split("\n"):
+        fab_text = Text(line, skew=options.skew, color=options.color,
+                        font=options.font, fsize=options.fsize,
+                        shadow=options.shadow)
+        for chunk in fab_text:
+            print chunk
+
 
 if __name__ == '__main__':
-    main(sys.argv)
+    main(sys.argv[1:])
     packages             = find_packages(),
     setup_requires       = ["setuptools_hg"],
     zip_safe             = False,
+    include_package_data = True,
+    # include_data         = True,
     # http://diveintopython3.org/packaging.html
     # http://wiki.python.org/moin/CheeseShopTutorial
     # http://pypi.python.org/pypi?:action=list_classifiers