2 Commits f9e1cf479b ... de1a71a5f1

Author SHA1 Message Date
  The MMGen Project de1a71a5f1 tw.view: new `column_widths_data` attribute 5 days ago
  The MMGen Project 0dba2a1fb8 tw.view: various fixes and cleanups 5 days ago

+ 1 - 1
mmgen/data/version

@@ -1 +1 @@
-16.1.dev18
+16.1.dev19

+ 0 - 1
mmgen/proto/xmr/tw/unspent.py

@@ -21,7 +21,6 @@ class MoneroTwUnspentOutputs(MoneroTwView, TwUnspentOutputs):
 	hdr_lbl = 'spendable accounts'
 	desc = 'spendable accounts'
 	include_empty = False
-	has_used = False
 
 	prompt_fs_in = [
 		'Sort options: [a]mount, [A]ge, a[d]dr, [M]mgen addr, [r]everse',

+ 3 - 5
mmgen/proto/xmr/tw/view.py

@@ -163,8 +163,8 @@ class MoneroTwView:
 			fmt_method = 'gen_display'
 			line_fmt_method = 'squeezed_format_line'
 
-	def get_column_widths(self, data, *, wide, interactive):
-		return self.compute_column_widths(
+	def get_column_widths(self, data, *, wide):
+		return self.column_widths_data(
 			widths = { # fixed cols
 				'addr_idx': MoneroIdx.max_digits,
 				'used': 4 if 'used' in self.display_type.squeezed.cols else 0,
@@ -176,9 +176,7 @@ class MoneroTwView:
 			minws = {
 				'addr': 16,
 				'comment': len('Comment')},
-			maxws_nice = self.nice_addr_w,
-			wide = wide,
-			interactive = interactive)
+			maxws_nice = self.nice_addr_w)
 
 	def gen_display(self, data, cw, fs, color, fmt_method):
 		yes, no = (red('Used'), green('New ')) if color else ('Used', 'New ')

+ 3 - 8
mmgen/tw/addresses.py

@@ -35,8 +35,6 @@ class TwAddresses(TwView):
 	showused = 1 # tristate: 0: no, 1: yes, 2: only
 	all_labels = False
 	mod_subpath = 'tw.addresses'
-	has_age = False
-	has_used = False
 
 	prompt_fs_in = [
 		'Sort options: [a]mt, [M]mgen addr, [r]everse',
@@ -188,9 +186,8 @@ class TwAddresses(TwView):
 							if d.is_used:
 								yield d
 
-	def get_column_widths(self, data, *, wide, interactive):
-
-		return self.compute_column_widths(
+	def get_column_widths(self, data, *, wide):
+		return self.column_widths_data(
 			widths = { # fixed cols
 				'num':       max(2, len(str(len(data)))+1),
 				'mmid':      max(len(d.twmmid.disp) for d in data),
@@ -206,9 +203,7 @@ class TwAddresses(TwView):
 			minws = {
 				'addr':    12 if self.showcoinaddrs else 0,
 				'comment': len('Comment')},
-			maxws_nice = {'addr': 18},
-			wide = wide,
-			interactive = interactive)
+			maxws_nice = {'addr': 18})
 
 	def squeezed_col_hdr(self, cw, fs, color):
 		return fs.format(

+ 13 - 30
mmgen/tw/txhistory.py

@@ -58,8 +58,7 @@ class TwTxHistory(TwView):
 		amts_tuple = namedtuple('amts_data', ['amt'])
 		return super().set_amt_widths([amts_tuple(d.amt_disp(self.show_total_amt)) for d in data])
 
-	def get_column_widths(self, data, *, wide, interactive):
-
+	def get_column_widths(self, data, *, wide):
 		# var cols: inputs outputs comment [txid]
 		if not hasattr(self, 'varcol_maxwidths'):
 			self.varcol_maxwidths = {
@@ -71,34 +70,18 @@ class TwTxHistory(TwView):
 						for d in data),
 				'comment': max(len(d.comment)
 						for d in data)}
-
-		maxws = self.varcol_maxwidths.copy()
-		minws = {
-			'inputs': 15,
-			'outputs': 15,
-			'comment': len('Comment')}
-		if self.show_txid:
-			maxws['txid'] = self.txid_w
-			minws['txid'] = 8
-			maxws_nice = {'txid': 20}
-		else:
-			maxws['txid'] = 0
-			minws['txid'] = 0
-			maxws_nice = {}
-
-		widths = { # fixed cols
-			'num': max(2, len(str(len(data)))+1),
-			'date': self.age_w,
-			'amt': self.amt_widths['amt'],
-			'spc': 6 + self.show_txid} # 5(6) spaces between cols + 1 leading space in fs
-
-		return self.compute_column_widths(
-			widths,
-			maxws,
-			minws,
-			maxws_nice,
-			wide        = wide,
-			interactive = interactive)
+		return self.column_widths_data(
+			widths = { # fixed cols
+				'num': max(2, len(str(len(data)))+1),
+				'date': self.age_w,
+				'amt': self.amt_widths['amt'],
+				'spc': 6 + self.show_txid}, # 5(6) spaces between cols + 1 leading space in fs
+			maxws = self.varcol_maxwidths | {'txid': self.txid_w if self.show_txid else 0},
+			minws = {
+				'inputs': 15,
+				'outputs': 15,
+				'comment': len('Comment')} | {'txid': 8 if self.show_txid else 0},
+			maxws_nice = {'txid': 20 if self.show_txid else 0})
 
 	def gen_squeezed_subheader(self, cw, color):
 		# keep these shorter than min screen width (currently prompt width, or 65 chars)

+ 4 - 11
mmgen/tw/unspent.py

@@ -28,14 +28,13 @@ from ..obj import (
 	TwComment,
 	CoinTxID,
 	NonNegativeInt)
-from ..addr import CoinAddr, MoneroIdx
+from ..addr import CoinAddr
 from ..amt import CoinAmtChk
 from .shared import TwMMGenID, TwLabel, get_tw_label
 from .view import TwView
 
 class TwUnspentOutputs(TwView):
 
-	has_age = False
 	show_mmid = True
 	hdr_lbl = 'tracked addresses'
 	desc    = 'address balances'
@@ -152,11 +151,9 @@ class TwUnspentOutputs(TwView):
 
 		return self.data
 
-	def get_column_widths(self, data, *, wide, interactive):
-
+	def get_column_widths(self, data, *, wide):
 		show_mmid = self.show_mmid or wide
-
-		return self.compute_column_widths(
+		return self.column_widths_data(
 			widths = { # fixed cols
 				'num': max(2, len(str(len(data)))+1),
 				'txid': 0,
@@ -166,8 +163,6 @@ class TwUnspentOutputs(TwView):
 				'amt2': self.amt_widths.get('amt2', 0),
 				'block': self.age_col_params['block'][0] if wide else 0,
 				'date_time': self.age_col_params['date_time'][0] if wide else 0,
-				'addr_idx': MoneroIdx.max_digits,
-				'acct_idx': MoneroIdx.max_digits,
 				'date': self.age_w,
 				'spc': self.disp_spc + (2 * show_mmid) + self.has_amt2},
 			maxws = { # expandable cols
@@ -180,9 +175,7 @@ class TwUnspentOutputs(TwView):
 			} | self.txid_min_w,
 			maxws_nice = (
 				self.nice_addr_w if show_mmid else {}
-			) | self.txid_nice_w,
-			wide = wide,
-			interactive = interactive)
+			) | self.txid_nice_w)
 
 	def squeezed_col_hdr(self, cw, fs, color):
 		return fs.format(

+ 35 - 28
mmgen/tw/view.py

@@ -80,6 +80,8 @@ class TwView(MMGenObject, metaclass=AsyncInit):
 			def do(method, data, cw, fs, color, fmt_method):
 				return [l.rstrip() for l in method(data, cw, fs, color, fmt_method)]
 
+	has_age     = False
+	has_used    = False
 	has_wallet  = True
 	has_amt2    = False
 	dates_set   = False
@@ -108,6 +110,7 @@ class TwView(MMGenObject, metaclass=AsyncInit):
 	pos = 0
 	filters = ()
 
+	column_widths_data = namedtuple('twview_column_widths', ['widths', 'maxws', 'minws', 'maxws_nice'])
 	fp = namedtuple('fs_params', ['fs_key', 'hdr_fs_repl', 'fs_repl', 'hdr_fs', 'fs'])
 	fs_params = {
 		'num':       fp('n', True, True,  ' {n:>%s}', ' {n:>%s}'),
@@ -199,7 +202,7 @@ class TwView(MMGenObject, metaclass=AsyncInit):
 		if self.has_wallet:
 			from .ctl import TwCtl
 			self.twctl = await TwCtl(cfg, proto, mode='w', no_rpc=not have_rpc)
-		self.amt_keys = {'amt':'iwidth', 'amt2':'iwidth2'} if self.has_amt2 else {'amt':'iwidth'}
+		self.amt_iwidth_keys = {'amt': 'iwidth'} | ({'amt2': 'iwidth2'} if self.has_amt2 else {})
 		if repl_data := self.prompt_fs_repl.get(self.proto.coin):
 			for repl in [repl_data] if isinstance(repl_data[0], int) else repl_data:
 				self.prompt_fs_in[repl[0]] = repl[1]
@@ -326,14 +329,16 @@ class TwView(MMGenObject, metaclass=AsyncInit):
 			else:
 				return _term_dimensions(min_cols, ts.height)
 
-	def compute_column_widths(self, widths, maxws, minws, maxws_nice, *, wide, interactive):
+	def compute_column_widths(self, in_data, *, wide, interactive):
 
 		def do_ret(freews):
 			if freews:
 				widths.update({k: minws[k] + freews.get(k, 0) for k in minws})
 			else:
 				widths.update({k: max(minws[k], maxws[k]) for k in minws})
-			widths.update({ikey: widths[key] - self.disp_prec - 1 for key, ikey in self.amt_keys.items()})
+			widths.update({
+				ikey: widths[key] - self.disp_prec - 1
+					for key, ikey in self.amt_iwidth_keys.items()})
 			return namedtuple('column_widths', widths.keys())(*widths.values())
 
 		def get_freews(cols, varws, varw, minw):
@@ -352,6 +357,7 @@ class TwView(MMGenObject, metaclass=AsyncInit):
 			else:
 				return {k:0 for k in varws}
 
+		widths, maxws, minws, maxws_nice = in_data
 		varws = {k: maxws[k] - minws[k] for k in maxws if maxws[k] > minws[k]}
 		minw = sum(widths.values()) + sum(minws.values())
 		varw = sum(varws.values())
@@ -409,7 +415,7 @@ class TwView(MMGenObject, metaclass=AsyncInit):
 		self.amt_widths = {
 			k: min(7, max(len(str(getattr(d, k).to_integral_value()))
 				for d in data)) + 1 + self.disp_prec
-					for k in self.amt_keys}
+					for k in self.amt_iwidth_keys}
 
 	async def format(
 			self,
@@ -483,7 +489,10 @@ class TwView(MMGenObject, metaclass=AsyncInit):
 
 			if self.disp_data and dt.need_column_widths:
 				self.set_amt_widths(self.disp_data)
-				cw = self.get_column_widths(self.disp_data, wide=dt.detail, interactive=interactive)
+				cw = self.compute_column_widths(
+					self.get_column_widths(self.disp_data, wide=dt.detail),
+					wide = dt.detail,
+					interactive = interactive)
 				cwh = cw._asdict()
 				fp = self.fs_params
 				rfill = ' ' * (self.term_width - self.cols) if scroll else ''
@@ -760,7 +769,7 @@ class TwView(MMGenObject, metaclass=AsyncInit):
 					break
 				await asyncio.sleep(0.5)
 
-			if parent.scroll and ret is False or ret == 'redraw':
+			if parent.scroll and (ret is False or ret == 'redraw'):
 				# error messages could leave screen in messy state, so do complete redraw:
 				msg_r(
 					CUR_HOME + ERASE_ALL +
@@ -801,17 +810,17 @@ class TwView(MMGenObject, metaclass=AsyncInit):
 
 			async def do_comment_add(comment_in):
 				from ..obj import TwComment
-				comment = await parent.twctl.set_comment(
-						addrspec     = None,
-						comment      = comment_in,
-						trusted_pair = (entry.twmmid, entry.addr),
-						silent       = parent.scroll)
-
-				if isinstance(comment, TwComment):
-					entry.comment = comment
-					edited = cur_comment and comment
-					parent.oneshot_msg = (green if comment else yellow)('Label {a} {b}{c}'.format(
-						a = 'for' if edited else 'added to' if comment else 'removed from',
+				new_comment = await parent.twctl.set_comment(
+					addrspec     = None,
+					comment      = comment_in,
+					trusted_pair = (entry.twmmid, entry.addr),
+					silent       = parent.scroll)
+
+				edited = old_comment and new_comment
+				if isinstance(new_comment, TwComment):
+					entry.comment = new_comment
+					parent.oneshot_msg = (green if new_comment else yellow)('Label {a} {b}{c}'.format(
+						a = 'for' if edited else 'added to' if new_comment else 'removed from',
 						b = desc,
 						c = ' edited' if edited else ''))
 					return 'redraw' if parent.cfg.coin == 'XMR' else True
@@ -819,13 +828,9 @@ class TwView(MMGenObject, metaclass=AsyncInit):
 					await asyncio.sleep(3)
 					parent.oneshot_msg = red('Label for {desc} could not be {action}'.format(
 						desc = desc,
-						action =
-							'edited' if cur_comment and comment else
-							'added' if comment else
-							'removed'))
+						action = 'edited' if edited else 'added' if new_comment else 'removed'))
 					return False
 
-			entry = parent.disp_data[idx-1]
 			if acct_addr_idx is None:
 				desc       = f'{parent.item_desc} #{idx}'
 				color_desc = f'{parent.item_desc} {red("#" + str(idx))}'
@@ -833,14 +838,16 @@ class TwView(MMGenObject, metaclass=AsyncInit):
 				desc       = f'address #{acct_addr_idx}'
 				color_desc = f'address {red("#" + str(acct_addr_idx))}'
 
-			cur_comment = parent.disp_data[idx-1].comment
-			msg('Current label: {}'.format(cur_comment.hl() if cur_comment else '(none)'))
+			entry = parent.disp_data[idx-1]
+			old_comment = entry.comment
+			msg('Current label: {}'.format(old_comment.hl() if old_comment else '(none)'))
 
 			from ..ui import line_input
-			res = line_input(parent.cfg, f'Enter label text for {color_desc}: ', insert_txt=cur_comment)
-
-			match res:
-				case s if s == cur_comment:
+			match res:= line_input(
+					parent.cfg,
+					f'Enter label text for {color_desc}: ',
+					insert_txt = old_comment):
+				case s if s == old_comment:
 					parent.oneshot_msg = yellow(f'Label for {desc} unchanged')
 					return None
 				case '':