Skip to content

Instantly share code, notes, and snippets.

@webgtx
Created June 4, 2024 10:23
Show Gist options
  • Save webgtx/e87f225c66ed2f0f6d836247c872cce7 to your computer and use it in GitHub Desktop.
Save webgtx/e87f225c66ed2f0f6d836247c872cce7 to your computer and use it in GitHub Desktop.
import curses
from urllib.request import urlopen
import json
from time import sleep
fetch_todo = lambda: json.loads(urlopen("https://jsonplaceholder.typicode.com/todos").read())
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
# Check for color support
if curses.has_colors():
curses.start_color()
# Initialize the color pairs for future use
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)
# Top panel
stdscr.addstr("TODO:", curses.A_REVERSE)
stdscr.chgat(-1, curses.A_REVERSE)
# Bottom panel
stdscr.addstr(curses.LINES-1, 0, "Simple Terminal User Interface application for your To Do tasks")
# Change R to green
stdscr.chgat(curses.LINES-1, 7, 1, curses.A_BOLD | curses.color_pair(2))
# Set up the root window
root_window = curses.newwin(curses.LINES-2, curses.COLS, 1, 0)
# Set up the text window (subwin)
text_window = root_window.subwin(curses.LINES - 6, curses.COLS - 4, 3, 2)
# Draw a border around root window
root_window.box()
# Update the internal window data structures
stdscr.noutrefresh()
root_window.noutrefresh()
# Redraw the screen
curses.doupdate()
# Create the event loop
while 1:
c = root_window.getch()
if c == ord('r') or c == ord('R'):
text_window.clear()
text_window.addstr("Fetching data...", curses.color_pair(3))
text_window.refresh()
text_window.clear()
data = fetch_todo()
# Program will crash if you exceed available lines in the terminal
for idx in range(5):
template = ("\tTask ID: {}\n"
"\t\t- [{}] {}\n").format(data[idx]['id'], data[idx]['completed'], data[idx]['title'])
text_window.addstr(template, curses.color_pair(1))
elif c == ord('q') or c == ord('Q'):
break
# Refresh the windows from the bottom up
stdscr.noutrefresh()
root_window.noutrefresh
text_window.noutrefresh()
curses.doupdate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment