From 26327868009a8914605d0131cdeaa9de9b79c12c Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Thu, 1 Dec 2022 12:32:31 +0000 Subject: [PATCH] tw.view: new `squeezed_col_hdr`, `detail_col_hdr` methods --- mmgen/tw/addresses.py | 30 ++++++++++--------- mmgen/tw/txhistory.py | 56 ++++++++++++++++++++++-------------- mmgen/tw/unspent.py | 34 +++++++++++----------- mmgen/tw/view.py | 18 +++++++----- test/test_py_d/ts_regtest.py | 16 +++++++++++ 5 files changed, 95 insertions(+), 59 deletions(-) diff --git a/mmgen/tw/addresses.py b/mmgen/tw/addresses.py index 7035c8f5..390cd4c2 100755 --- a/mmgen/tw/addresses.py +++ b/mmgen/tw/addresses.py @@ -155,9 +155,8 @@ class TwAddresses(TwView): else: return '' - def gen_squeezed_display(self,data,cw,hdr_fs,fs,color): - - yield hdr_fs.format( + def squeezed_col_hdr(self,cw,fs,color): + return fs.format( n = '', m = 'MMGenID', u = 'Used', @@ -166,6 +165,19 @@ class TwAddresses(TwView): A = 'Balance', d = self.age_hdr ) + def detail_col_hdr(self,cw,fs,color): + return fs.format( + n = '', + m = 'MMGenID', + u = 'Used', + a = 'Address', + c = 'Comment', + A = 'Balance', + b = 'Block', + D = 'Date/Time' ) + + def gen_squeezed_display(self,data,cw,fs,color): + yes,no = (red('Yes '),green('No ')) if color else ('Yes ','No ') id_save = data[0].al_id @@ -183,17 +195,7 @@ class TwAddresses(TwView): d = self.age_disp( d, self.age_fmt ) ) - def gen_detail_display(self,data,cw,hdr_fs,fs,color): - - yield hdr_fs.format( - n = '', - m = 'MMGenID', - u = 'Used', - a = 'Address', - c = 'Comment', - A = 'Balance', - b = 'Block', - D = 'Date/Time' ) + def gen_detail_display(self,data,cw,fs,color): yes,no = (red('Yes '),green('No ')) if color else ('Yes ','No ') id_save = data[0].al_id diff --git a/mmgen/tw/txhistory.py b/mmgen/tw/txhistory.py index da745006..3e9061fa 100755 --- a/mmgen/tw/txhistory.py +++ b/mmgen/tw/txhistory.py @@ -25,9 +25,11 @@ class TwTxHistory(TwView): class squeezed(TwView.display_type.squeezed): cols = ('num','txid','date','inputs','amt','outputs','comment') + hdr_fmt_method = 'squeezed_hdr' class detail(TwView.display_type.detail): need_column_widths = False + hdr_fmt_method = 'detail_hdr' item_separator = '\n\n' has_wallet = False @@ -89,25 +91,39 @@ class TwTxHistory(TwView): return self.compute_column_widths(widths,maxws,minws,maxws_nice,wide=wide) - def gen_squeezed_display(self,data,cw,hdr_fs,fs,color): + def squeezed_hdr(self,cw,fs,color): - if self.sinceblock: - yield f'Displaying transactions since block {self.sinceblock.hl(color=color)}' - yield 'Only wallet-related outputs are shown' - yield 'Comment is from first wallet address in outputs or inputs' - if (cw.inputs < self.varcol_maxwidths['inputs'] or - cw.outputs < self.varcol_maxwidths['outputs'] ): - yield 'Due to screen width limitations, not all addresses could be displayed' - yield '' + def gen(): + if self.sinceblock: + yield f'Displaying transactions since block {self.sinceblock.hl(color=color)}' + yield 'Only wallet-related outputs are shown' + yield 'Comment is from first wallet address in outputs or inputs' + if (cw.inputs < self.varcol_maxwidths['inputs'] or + cw.outputs < self.varcol_maxwidths['outputs'] ): + yield 'Due to screen width limitations, not all addresses could be displayed' + yield '' - yield hdr_fs.format( - n = '', - t = 'TxID', - d = self.age_hdr, - i = 'Inputs', - A = 'Amt({})'.format('TX' if self.show_total_amt else 'Wallet'), - o = 'Outputs', - c = 'Comment' ) + yield fs.format( + n = '', + t = 'TxID', + d = self.age_hdr, + i = 'Inputs', + A = 'Amt({})'.format('TX' if self.show_total_amt else 'Wallet'), + o = 'Outputs', + c = 'Comment' ) + + return '\n'.join(gen()) + + def detail_hdr(self,cw,fs,color): + + def gen(): + if self.sinceblock: + yield f'Displaying transactions since block {self.sinceblock.hl(color=color)}' + yield 'Only wallet-related outputs are shown' + + return '\n'.join(gen()) + '\n\n' + + def gen_squeezed_display(self,data,cw,fs,color): for n,d in enumerate(data,1): yield fs.format( @@ -119,11 +135,7 @@ class TwTxHistory(TwView): o = d.vouts_disp( 'outputs', width=cw.outputs, color=color ), c = d.comment.fmt( width=cw.comment, color=color, nullrepl='-' ) ) - def gen_detail_display(self,data,cw,hdr_fs,fs,color): - - if self.sinceblock: - yield f'Displaying transactions since block {self.sinceblock.hl(color=color)}' - yield 'Only wallet-related outputs are shown' + def gen_detail_display(self,data,cw,fs,color): fs = fmt(""" {n} diff --git a/mmgen/tw/unspent.py b/mmgen/tw/unspent.py index d8d45260..44f418d2 100755 --- a/mmgen/tw/unspent.py +++ b/mmgen/tw/unspent.py @@ -153,9 +153,8 @@ class TwUnspentOutputs(TwView): wide = wide, ) - def gen_squeezed_display(self,data,cw,hdr_fs,fs,color): - - yield hdr_fs.format( + def squeezed_col_hdr(self,cw,fs,color): + return fs.format( n = '', t = 'TxID', v = 'Vout', @@ -166,6 +165,21 @@ class TwUnspentOutputs(TwView): B = 'Amt({})'.format(self.proto.coin), d = self.age_hdr ) + def detail_col_hdr(self,cw,fs,color): + return fs.format( + n = '', + t = 'TxID', + v = 'Vout', + a = 'Address', + m = 'MMGenID', + A = 'Amt({})'.format(self.proto.dcoin), + B = 'Amt({})'.format(self.proto.coin), + b = 'Block', + D = 'Date/Time', + c = 'Comment' ) + + def gen_squeezed_display(self,data,cw,fs,color): + for n,d in enumerate(data): yield fs.format( n = str(n+1) + ')', @@ -182,19 +196,7 @@ class TwUnspentOutputs(TwView): d = self.age_disp(d,self.age_fmt), ) - def gen_detail_display(self,data,cw,hdr_fs,fs,color): - - yield hdr_fs.format( - n = '', - t = 'TxID', - v = 'Vout', - a = 'Address', - m = 'MMGenID', - A = 'Amt({})'.format(self.proto.dcoin), - B = 'Amt({})'.format(self.proto.coin), - b = 'Block', - D = 'Date/Time', - c = 'Comment' ) + def gen_detail_display(self,data,cw,fs,color): for n,d in enumerate(data): yield fs.format( diff --git a/mmgen/tw/view.py b/mmgen/tw/view.py index a84bb3ea..e11222fa 100755 --- a/mmgen/tw/view.py +++ b/mmgen/tw/view.py @@ -39,6 +39,7 @@ class TwView(MMGenObject,metaclass=AsyncInit): class squeezed: detail = False fmt_method = 'gen_squeezed_display' + hdr_fmt_method = 'squeezed_col_hdr' need_column_widths = True item_separator = '\n' print_header = '[screen print truncated to width {}]\n' @@ -46,6 +47,7 @@ class TwView(MMGenObject,metaclass=AsyncInit): class detail: detail = True fmt_method = 'gen_detail_display' + hdr_fmt_method = 'detail_col_hdr' need_column_widths = True item_separator = '\n' print_header = '' @@ -54,8 +56,8 @@ class TwView(MMGenObject,metaclass=AsyncInit): class print: color = False - def do(method,data,cw,hdr_fs,fs,color): - return [l.rstrip() for l in method(data,cw,hdr_fs,fs,color)] + def do(method,data,cw,fs,color): + return [l.rstrip() for l in method(data,cw,fs,color)] has_wallet = True has_amt2 = False @@ -343,7 +345,7 @@ class TwView(MMGenObject,metaclass=AsyncInit): cwh = cw._asdict() fp = self.fs_params hdr_fs = ''.join(fp[name].hdr_fs % ((),cwh[name])[fp[name].hdr_fs_repl] - for name in dt.cols if cwh[name]) + for name in dt.cols if cwh[name]) + '\n' fs = ''.join(fp[name].fs % ((),cwh[name])[fp[name].fs_repl] for name in dt.cols if cwh[name]) else: @@ -355,14 +357,15 @@ class TwView(MMGenObject,metaclass=AsyncInit): def get_body(method): if line_processing: - return lp_cls.do(method,data,cw,hdr_fs,fs,color) + return lp_cls.do(method,data,cw,fs,color) else: - return method(data,cw,hdr_fs,fs,color) + return method(data,cw,fs,color) - self._display_data[display_type] = '{a}{b}\n{c}\n'.format( + self._display_data[display_type] = '{a}{b}\n{c}{d}\n'.format( a = self.header(color), b = self.subheader(color), - c = ( + c = getattr(self,dt.hdr_fmt_method)(cw,hdr_fs,color) if data else '', + d = ( dt.item_separator.join(get_body(getattr(self,dt.fmt_method))) if data else (nocolor,yellow)[color]('[no data for requested parameters]')) ) @@ -402,6 +405,7 @@ class TwView(MMGenObject,metaclass=AsyncInit): continue action = self.key_mappings[reply] + if hasattr(self.action,action): await self.action().run(self,action) elif action.startswith('s_'): # put here to allow overriding by action method diff --git a/test/test_py_d/ts_regtest.py b/test/test_py_d/ts_regtest.py index 82f98b15..b015262e 100755 --- a/test/test_py_d/ts_regtest.py +++ b/test/test_py_d/ts_regtest.py @@ -336,6 +336,8 @@ class TestSuiteRegtest(TestSuiteBase,TestSuiteShared): ), 'view': ( 'viewing addresses and unspent outputs', + ('alice_listaddresses_empty', 'listaddresses (no data)'), + ('alice_listaddresses_menu', 'listaddresses (menu items)'), ('alice_listaddresses1', 'listaddresses'), ('alice_listaddresses_days', 'listaddresses (age_fmt=days)'), ('alice_listaddresses_date', 'listaddresses (age_fmt=date)'), @@ -1251,6 +1253,20 @@ class TestSuiteRegtest(TestSuiteBase,TestSuiteShared): t.expect(r'\[q\]uit view, .*?:.','q',regex=True) return t + def _alice_listaddresses_interactive(self,expect=(),expect_menu=()): + t = self.spawn('mmgen-tool',['--alice','listaddresses','interactive=1']) + for s in expect_menu: + t.expect('abel:\b',s) + for p,s in expect: + t.expect(p,s) + return t + + def alice_listaddresses_empty(self): + return self._alice_listaddresses_interactive(expect_menu='uuEq') + + def alice_listaddresses_menu(self): + return self._alice_listaddresses_interactive(expect_menu='aAMrDDDDLeq') + def alice_listaddresses(self,args,expect): t = self.spawn('mmgen-tool',['--alice','listaddresses','showempty=1'] + args) expect_str = r'\D{}\D.*\b{}'.format(*expect)