Skip to content

Instantly share code, notes, and snippets.

@jacmoe
Forked from vcidst/quotesOnCurses.py
Created June 20, 2017 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacmoe/df63b9d7d4b6f041e22ab39532b9c7a2 to your computer and use it in GitHub Desktop.
Save jacmoe/df63b9d7d4b6f041e22ab39532b9c7a2 to your computer and use it in GitHub Desktop.
Structure of a curses application by [Sean Zicari](https://www.youtube.com/watch?v=eN1eZtjLEnU)
# This gist is transcribed from a talk by Sean Zicari
# Talk: Use Curses, don't swear https://www.youtube.com/watch?v=eN1eZtjLEnU
#
# I haven't written the function which he used to fetch quotes from the web
# This only presents the barebones structure of his application
#
# If debugging a curses application messes up your terminal, type `tset` to reset terminal
import curses
stdscr = curses.initscr()
# initialize the screen
curses.noecho()
curses.cbreak()
curses.curs_set(0)
# check and begin color support
if curses.has_colors():
curses.start_color()
# optionally enable the keypad
# stdscr.keypad(1)
# initialize color combinations
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_BLUE, curses.COLOR_BLACK)
# if your terminal supports colors, any number from 0-255 can be used in place of colors
# begin program
stdscr.addstr("RANDOM QUOTES", curses.A_REVERSE)
stdscr.chgat(-1, curses.A_REVERSE)
# curses.LINES and curses.COLS contain height and width of screen
# adds string at coordinate (curses.LINES-1, 0)
stdscr.addstr(curses.LINES-1, 0, "Press Q to watch me not obey")
# Change the R to green
stdscr.chgat(curses.LINES-1, 7, 1, curses.A_BOLD | curses.color_pair(2))
# Set up the window to hold the random quotes
quote_window = curses.newwin(curses.LINES-2, curses.COLS, 1,0)
# curses.LINES-2 and curses.COLS are the height and width of new window (rectangle)
# (1,0) is where it starts from
# create a sub-window for visibility
quote_text_window = quote_window.subwin(curses.LINES-6, curses.COLS-4, 3, 2)
# without coordiates, addstr() adds string to whereever pointer is (which is beginning, in this case)
quote_text_window.addstr("Press R to watch yourself burn")
# draw a border around main window
quote_window.box()
# update the internal window data structure
stdscr.noutrefresh()
quote_window.noutrefresh()
# redraw the screen
curses.doupdate()
# create an event loop
while True:
c = quote_window.getch()
if c == ord('r') or c == ord('R'):
quote_text_window.clear()
quote_text_window.addstr("I'm just a terminal screen, I can't really do anything...", curses.color_pair(3))
quote_text_window.refresh()
quote_text_window.clear()
quote_text_window.addstr("There's so much happening all the time")
elif c == ord('q') or c == ord('Q'):
break
# refresh the windows from the bottom up
stdscr.noutrefresh()
quote_window.noutrefresh()
quote_text_window.noutrefresh()
curses.doupdate()
# Restore the terminal settings
curses.nocbreak()
#stdscr.keypad(0)
curses.echo()
curses.curs_set(1)
# restore terminal
curses.endwin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment