[Tutor] Simple curses application
Julius Hamilton
juliushamilton100 at gmail.com
Mon Oct 25 07:40:39 EDT 2021
Hey,
Here’s my attempt at some pseudocode, with a few questions about it:
import curses
import json
# load previous application data
data = open(“dr.dat”).json()
# ?
# Data is a list of dictionaries. Each text name is a key. The value is
three more dictionaries. The first is the text. The second is {index: } -
the location in the text where the reader left off. The second is
{comments: “”} - a text file.
So, data = [{“TheBible”: [{text: “text of bible”}, {index: 0}, {comments:
“”}]}]
You can launch the app with or without a command line argument - a filename
- which imports a new text into the data. The index starts at 0 for new
texts.
$ dr newtext
textname = $1
if textname != “”:
data.add({textname: [{text: open(textname).read()}, {index: 0},
{comments: “”}]}].
else:
# show a menu of imported texts with curses. The user can navigate up and
down with Vim keys: j and k, and choose one with “enter”. What’s a good way
to do that with curses? Or a different library?
scr = curses.initscr()
# …
Assuming they select one:
text = data[selection[text]]
index = data[selection[index]]
comments = data[selection[comments]]
lines = text.splitlines()
scr.addstr(line[index])
curses.refresh()
# How do I listen for user key presses “l” and “h”, left and right after
Vim? l puts the index up one and refreshes the screen, h the index down
one. c allows a the user to type into a text box on screen, beneath the
line. When they press enter, it’s appended as a new line to the “comments”
file. q rewrites the data to the data file and closes the curses viewer.
Could anyone please correct my code and fill in these blank details? I’d
really appreciate it.
Thanks very much,
Julius
More information about the Tutor
mailing list