Skip to content

Instantly share code, notes, and snippets.

@Ikke
Created March 4, 2012 09:01
Show Gist options
  • Select an option

  • Save Ikke/1971584 to your computer and use it in GitHub Desktop.

Select an option

Save Ikke/1971584 to your computer and use it in GitHub Desktop.
ncurses two windows side-by-side
class Buffer(object):
def __init__(self, window, lines):
self.window = window
self.lines = lines
self.buffer = [""]
def write(self, text):
lines = text.split("\n")
self.buffer[-1] += lines[0]
self.buffer.extend(lines[1:])
self.refresh()
def writeln(self, text):
self.write(text + "\n")
def input(self, text = ""):
return self._input(text, lambda: self.window.getstr().decode('utf-8'))
def input_chr(self, text = ""):
return self._input(text, lambda: chr(self.window.getch()))
def _input(self, text, get_input):
self.write(text)
input = get_input()
self.writeln(input)
return input
def refresh(self):
self.window.clear()
for nr, line in enumerate(self.buffer[-self.lines:]):
self.window.addstr(nr, 0, line)
import argparse
import os
from ledger.actions import read_csv, complete_entries, read_ledger_accounts
from ledger.buffer import Buffer
import curses
import json
parser = argparse.ArgumentParser(description="Adds entries to a ledger file")
parser.add_argument('action')
parser.add_argument('input')
actions = {
'parsecsv': read_csv
}
def run(stdscr):
curses.echo()
lines = int(os.environ['LINES'])
columns = int(os.environ['COLUMNS'])
half_width = int(columns / 2)
left_window = curses.newwin(lines, half_width)
left_buffer = Buffer(left_window, lines)
right_window = curses.newwin(lines, half_width, 0, half_width + 2)
right_buffer = Buffer(right_window, lines)
args = parser.parse_args()
input_filename = os.path.expanduser(args.input)
if args.action == 'parsecsv':
accounts = read_ledger_accounts(os.path.expanduser("~/Documents/boekhouding.dat"))
right_buffer.writeln('Test')
right_buffer.writeln(json.dumps(accounts))
right_buffer.refresh()
entries = read_csv(input_filename)
entries = complete_entries(entries, left_buffer, right_buffer)
#stdscr.refresh()
right_buffer.input_chr()
#this is the startup file
from ledger import ledger
import curses
curses.wrapper(ledger.run)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment