Hilite and subclasses: minor cleanups; truncate_str(): cleanup

This commit is contained in:
The MMGen Project 2022-12-03 17:40:44 +00:00
commit 749dff6dd1
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
6 changed files with 22 additions and 16 deletions

View file

@ -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(

View file

@ -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

View file

@ -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:

View file

@ -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,

View file

@ -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)
)

View file

@ -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,