From 2f6ced69fc2aeed0240de2826c201c5dd40da578 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Hoelt Date: Mon, 7 Nov 2016 12:52:10 +0200 Subject: [PATCH 1/3] Display balances in Accounts view --- regdel | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/regdel b/regdel index bde8735..c2ad710 100755 --- a/regdel +++ b/regdel @@ -45,10 +45,14 @@ KEY_MAP = { curses.KEY_UP: "PREV_LINE", curses.KEY_DOWN: "NEXT_LINE", ord('q'): "QUIT", + curses.KEY_EXIT: "QUIT", + curses.KEY_F10: "QUIT", ord('x'): "COMMODITY", ord('\n'): "SELECT", ord('b'): "BALANCE", ord(' '): "NEXT_PAGE", + curses.KEY_NPAGE: "NEXT_PAGE", + curses.KEY_PPAGE: "PREV_PAGE", ord('g'): "FIRST_LINE", curses.KEY_HOME: "FIRST_LINE", ord('G'): "LAST_LINE", @@ -162,7 +166,11 @@ class View(object): class AccountsView(View): def __init__(self, app, win): super(AccountsView, self).__init__(app, win) - lines = ledger(app.path, 'accounts') + self.update() + + def update(self): + lines = ledger(self.app.path, 'balance', + options=[ '--flat', '--no-total' ]) accounts = [] for line in lines: for i in range(len(line.split(':'))): @@ -180,7 +188,14 @@ class AccountsView(View): win.addstr("Accounts") def select(self, i): - return RegView(self.app, self.full, self.lines[i]) + if len(self.lines) <= i: + return None + tokens = self.lines[i].split(" ") + tokens = [token for token in tokens if len(token) > 0] + if len(tokens) <= 1: + return None + txt = tokens[len(tokens) - 1] + return RegView(self.app, self.full, txt) class RegView(View): def __init__(self, app, win, account): @@ -305,6 +320,11 @@ class App: if req == "NEXT_PAGE": step = self.view.win.getmaxyx()[0] self.view.offset += self.view.win.getmaxyx()[0] + if req == "PREV_PAGE": + step = -self.view.win.getmaxyx()[0] + self.view.offset -= self.view.win.getmaxyx()[0] + if self.view.offset < 0: + self.view.offset = 0 if req == "FIRST_LINE": self.view.lineno = 0 self.view.offset = 0 From a9a1f7b8d0d3fcbdbddbc3ed140457864d65ee3f Mon Sep 17 00:00:00 2001 From: Jean-Christophe Hoelt Date: Mon, 7 Nov 2016 20:20:44 +0200 Subject: [PATCH 2/3] Fix display of accounts as balance --- regdel | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/regdel b/regdel index c2ad710..5a1b032 100755 --- a/regdel +++ b/regdel @@ -169,14 +169,8 @@ class AccountsView(View): self.update() def update(self): - lines = ledger(self.app.path, 'balance', + self.lines = ledger(self.app.path, 'balance', options=[ '--flat', '--no-total' ]) - accounts = [] - for line in lines: - for i in range(len(line.split(':'))): - a = ':'.join(line.split(':')[:i+1]) - if a not in accounts: accounts.append(a) - self.lines = accounts def render(self, win, i): if i >= len(self.lines): return From 0ecdc48f0e0751822dba8e3a14f5ea8f0323ac4c Mon Sep 17 00:00:00 2001 From: Jean-Christophe Hoelt Date: Mon, 7 Nov 2016 22:55:22 +0200 Subject: [PATCH 3/3] Run custom query with '/' --- README.md | 1 + regdel | 53 +++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7971249..41101af 100644 --- a/README.md +++ b/README.md @@ -22,3 +22,4 @@ so it might not work very well on other files. - b: show balance view - x: toggle commodities - r: toggle --real +- /: run custom query diff --git a/regdel b/regdel index 6160b35..27e7906 100755 --- a/regdel +++ b/regdel @@ -44,6 +44,7 @@ KEY_MAP = { ord('j'): "NEXT_LINE", curses.KEY_UP: "PREV_LINE", curses.KEY_DOWN: "NEXT_LINE", + 27: "QUIT", ord('q'): "QUIT", curses.KEY_EXIT: "QUIT", curses.KEY_F10: "QUIT", @@ -58,6 +59,7 @@ KEY_MAP = { curses.KEY_HOME: "FIRST_LINE", ord('G'): "LAST_LINE", curses.KEY_END: "LAST_LINE", + ord('/'): "SEARCH", } def clamp(x, a, b): return min(max(x, a), b) @@ -184,7 +186,7 @@ class AccountsView(View): options=[ '--flat', '--no-total' ]) def render(self, win, i): - if i >= len(self.lines): return + if i < 0 or i >= len(self.lines): return line = self.lines[i] cur = 'cursor' if i == self.lineno else None win.addstr(line, self.get_attr(cur or 'account')) @@ -202,6 +204,7 @@ class AccountsView(View): if len(tokens) <= 1: return None txt = tokens[len(tokens) - 1] + self.app.query = txt return RegView(self.app, self.full, txt) class RegView(View): @@ -225,7 +228,7 @@ class RegView(View): options=['--effective', '-S', 'date']) def render(self, win, i): - if i >= len(self.lines): return + if i < 0 or i >= len(self.lines): return line = self.lines[i] if not line: return cur = 'cursor' if i == self.lineno else None @@ -267,7 +270,7 @@ class TransactionView(View): super(TransactionView, self).__init__(app, win) self.lines = lines def render(self, win, i): - if i >= len(self.lines): return + if i < 0 or i >= len(self.lines): return line = self.lines[i] cur = 'cursor' if i == self.lineno else None win.addstr(line, self.get_attr(cur)) @@ -292,7 +295,7 @@ class BalanceView(View): real=self.real, options=[]) def render(self, win, i): - if i >= len(self.lines): return + if i < 0 or i >= len(self.lines): return line = self.lines[i] if not line: return cur = 'cursor' if i == self.lineno else None @@ -312,6 +315,9 @@ class BalanceView(View): class App: def __init__(self, path, scr): + self.mode = "NORMAL" + self.textpad = None + self.query = "" self.path = path self.scr = scr self.view = AccountsView(self, self.scr) @@ -361,16 +367,51 @@ class App: self.views.append(view) self.view = view self.view.refresh() + if req == "SEARCH": + self.mode = "COMMAND" + if self.textpad == None: + self.textpad = curses.newwin(1,self.view.full.getmaxyx()[1],1,0) + self.refreshCommand() self.view.lineno += step if req == "MAIN" or step != 0: self.view.refresh() + def refreshCommand(self): + self.textpad.clear() + self.textpad.addstr("query # " + self.query) + self.textpad.refresh() + + def processCommand(self, c): + if c == ord('\n'): + view = RegView(self, self.view.full, self.query) + self.views.append(view) + self.view = view + self.view.refresh() + self.textpad.erase() + self.mode = "NORMAL" + elif c == 127: + self.query = self.query[:-1] + self.refreshCommand() + elif c == 27: + self.query = "" + self.textpad.erase() + self.mode = "NORMAL" + self.textpad.refresh() + self.view.refresh() + elif c < 256: + self.textpad.addch(c) + self.textpad.refresh() + self.query += chr(c) + def run(self): self.process("MAIN") while True: c = self.scr.getch() - req = KEY_MAP.get(c) - if self.process(req): break + if self.mode == "NORMAL": + req = KEY_MAP.get(c) + if self.process(req): break + else: + self.processCommand(c) if len(sys.argv) != 2: print "USAGE: regdel "