As of 2016-02-26, there will be no more posts for this blog. s/blog/pba/
Showing posts with label FIGlet. Show all posts

Everyone knows FIGlet or at least have seen once or twice the output of FIGlet even whether they know what generates those or not. But not everyone knows that there is a port1 of FIGlet, I was one of them until I came across a program called termdown, which is a countdown timer and depends on the port of FIGlet, pyfiglet:

                        _|_|  _|            _|              _|
_|_|_|    _|    _|    _|            _|_|_|  _|    _|_|    _|_|_|_|
_|    _|  _|    _|  _|_|_|_|  _|  _|    _|  _|  _|_|_|_|    _|
_|    _|  _|    _|    _|      _|  _|    _|  _|  _|          _|
_|_|_|      _|_|_|    _|      _|    _|_|_|  _|    _|_|_|      _|_|
_|              _|                      _|
_|          _|_|                    _|_|

Lets compare the CLIs, FIGlet v2.2.2 (2005-08-05) and pyfiglet v0.7 (2014-06-08):

$ figlet -h
figlet: invalid option -- 'h'
Usage: figlet [ -cklnoprstvxDELNRSWX ] [ -d fontdirectory ]
              [ -f fontfile ] [ -m smushmode ] [ -w outputwidth ]
              [ -C controlfile ] [ -I infocode ] [ message ]

$ pyfiglet -h
Usage: pyfiglet [options] [text..]

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -f FONT, --font=FONT  font to render with (default: standard)
  -D DIRECTION, --direction=DIRECTION
                        set direction text will be formatted in (default:
                        auto)
  -j SIDE, --justify=SIDE
                        set justification, defaults to print direction
  -w COLS, --width=COLS
                        set terminal width for wrapping/justification
                        (default: 80)
  -r, --reverse         shows mirror image of output text
  -F, --flip            flips rendered output text over
  -l, --list_fonts      show installed fonts list
  -i, --info_font       show font's information, use with -f FONT

Well, I cant say its fully ported in terms of options, but I would say they are close, the CLI isnt the point here, but the programmical interface is, that you can just get a FIGlet-style output right in your Python script. I always shake my head whenever I see someone coding something like the following:

os.system('clear')
os.system('tput cup 0 0')

Why do I dislike code above? Just think about should you do in that way, you would understand why usually think this isnt a good idea.

With pyfiglet, you dont need to pipe in FIGlet if you really want those big text with fancy fonts. You just import the library and generate like, from README:

>>> from pyfiglet import Figlet
>>> f = Figlet(font='slant')
>>> print f.renderText('text to render')
   __            __     __                               __
  / /____  _  __/ /_   / /_____     ________  ____  ____/ /__  _____
 / __/ _ \| |/_/ __/  / __/ __ \   / ___/ _ \/ __ \/ __  / _ \/ ___/
/ /_/  __/>  </ /_   / /_/ /_/ /  / /  /  __/ / / / /_/ /  __/ /
\__/\___/_/|_|\__/   \__/\____/  /_/   \___/_/ /_/\__,_/\___/_/

It supports many fonts, maybe even all of FIGlet fonts, run pyfiglet -l to get the list.

pyfiglet is made by Christopher Jones, Stefano Rivera, and Peter Waller, under the GPLv2. It first appeared in 2007, now the version 0.7 (2014-06-08), works for both Python 2 and 3.

[1]There is a project called TOIlet, although it can loads FIGlets fonts, I dont know if its considered as a port or not.

Just finished this hours-project. I happened to read this thread and know the FIGlet and TOIlet, hours later, I am showing you MPDisp. I used FIGlet not TOIlet because TOIlet doesn't support center aligning. Here is a screenshot:

MPDisp

You can read and download the code. The only dependency should be FIGlet (do I really need to say you need MPD?). It doesn't rely on (nc)mpc(pp), it contacts MPD directly. All it does is show the song information and give you a little bit of control, see the key list:

  • p - Pause/Resume
  • Enter - Play
  • s - Stop
  • n - Next song
  • p - Previous song
  • r - Repeat mode on/off
  • S - Single mode on/off
  • R - Random mode on/off
  • q - Quit
  • Q - Quit with MPD
I was thinking to use TOIlet, it has a feature called filter, which can render with colors. But it can't align text center. I might try to see what I can do to next.

The only problem I have seen is if you have too small window, text will be messed up, and I am not intended to solve it right now.