From 749dff6dd169ad347c9775aeaa43c332126794ea Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sat, 3 Dec 2022 17:40:44 +0000 Subject: [PATCH] Hilite and subclasses: minor cleanups; truncate_str(): cleanup --- mmgen/amt.py | 8 +++++++- mmgen/obj.py | 8 ++++---- mmgen/objmethods.py | 16 ++++++++-------- mmgen/proto/btc/tx/info.py | 2 +- mmgen/tw/bal.py | 2 +- mmgen/tw/unspent.py | 2 +- 6 files changed, 22 insertions(+), 16 deletions(-) diff --git a/mmgen/amt.py b/mmgen/amt.py index 80c89f90..d6548443 100755 --- a/mmgen/amt.py +++ b/mmgen/amt.py @@ -73,9 +73,15 @@ class CoinAmt(Decimal,Hilite,InitErrors): # abstract class def fmtc(cls): cls.method_not_implemented() - def fmt(self,color=False,iwidth=1,prec=None): # iwidth: width of the integer part + def fmt( + self, + color = False, + iwidth = 1, # width of the integer part + prec = None ): + s = self.__str__() prec = prec or self.max_prec + if '.' in s: a,b = s.split('.',1) return self.colorize( diff --git a/mmgen/obj.py b/mmgen/obj.py index 6323958c..ecd73e2a 100755 --- a/mmgen/obj.py +++ b/mmgen/obj.py @@ -273,12 +273,12 @@ class Int(int,Hilite,InitErrors): return cls.init_fail(e,n) @classmethod - def fmtc(cls,n,*args,**kwargs): - return super().fmtc(str(n),*args,**kwargs) + def fmtc(cls,s,**kwargs): + return super().fmtc(s.__str__(),**kwargs) @classmethod - def colorize(cls,n,*args,**kwargs): - return super().colorize(str(n),*args,**kwargs) + def colorize(cls,s,**kwargs): + return super().colorize(s.__str__(),**kwargs) class NonNegativeInt(Int): min_val = 0 diff --git a/mmgen/objmethods.py b/mmgen/objmethods.py index 0e1f8425..ef458a26 100755 --- a/mmgen/objmethods.py +++ b/mmgen/objmethods.py @@ -33,14 +33,14 @@ else: def truncate_str(s,width): # width = screen width wide_count = 0 - for i in range(len(s)): - wide_count += unicodedata.east_asian_width(s[i]) in ('F','W') - if wide_count + i >= width: - return s[:i] + ('',' ')[ - unicodedata.east_asian_width(s[i]) in ('F','W') - and wide_count + i == width] - else: # pad the string to width if necessary - return s + ' '*(width-len(s)-wide_count) + for n,ch in enumerate(s,1): + wide_count += unicodedata.east_asian_width(ch) in ('F','W') + if n + wide_count > width: + return s[:n-1] + ('',' ')[ + unicodedata.east_asian_width(ch) in ('F','W') + and n + wide_count == width + 1] + else: + raise ValueError('string requires no truncating') class Hilite: diff --git a/mmgen/proto/btc/tx/info.py b/mmgen/proto/btc/tx/info.py index 93b86c99..4aa0592d 100755 --- a/mmgen/proto/btc/tx/info.py +++ b/mmgen/proto/btc/tx/info.py @@ -81,7 +81,7 @@ class TxInfo(TxInfo): append_chars=('',' (chg)')[bool(not is_input and e.is_chg and terse)], append_color='green') else: - mmid_fmt = MMGenID.fmtc(nonmm_str,width=max_mmwid,color=True) + mmid_fmt = MMGenID.fmtc( nonmm_str, width=max_mmwid, color=True ) if terse: yield '{:3} {} {} {} {}\n'.format( n+1, diff --git a/mmgen/tw/bal.py b/mmgen/tw/bal.py index 3e4d7949..b16c981f 100755 --- a/mmgen/tw/bal.py +++ b/mmgen/tw/bal.py @@ -97,7 +97,7 @@ class TwGetBalance(MMGenObject,metaclass=AsyncInit): for label in sorted(self.data.keys()): yield '{lbl} {cols}'.format( lbl = yellow((label + ' ' + self.proto.coin).ljust(col1_w)) if label == 'TOTAL' - else MMGenID.hlc((label+':').ljust(col1_w),color=color), + else MMGenID.hlc( (label+':').ljust(col1_w), color=color ), cols = ' '.join(make_col(label,col) for col in self.conf_cols) ) diff --git a/mmgen/tw/unspent.py b/mmgen/tw/unspent.py index c639f4b3..961b4360 100755 --- a/mmgen/tw/unspent.py +++ b/mmgen/tw/unspent.py @@ -186,7 +186,7 @@ class TwUnspentOutputs(TwView): t = (CoinTxID.fmtc('|' + '.'*(cw.txid-1),color=color) if d.skip == 'txid' else d.txid.truncate( width=cw.txid, color=color )) if cw.txid else None, v = ' ' + d.vout.fmt( width=cw.vout-1, color=color ) if cw.vout else None, - a = type(d.addr).fmtc( '|' + '.'*(cw.addr-1), width=cw.addr, color=color ) if d.skip == 'addr' + a = d.addr.fmtc( '|' + '.'*(cw.addr-1), width=cw.addr, color=color ) if d.skip == 'addr' else d.addr.fmt( width=cw.addr, color=color ), m = (MMGenID.fmtc( '.'*cw.mmid, color=color ) if d.skip == 'addr' else d.twmmid.fmt( width=cw.mmid, color=color )) if cw.mmid else None,