Change the display style of the text on the STACKLINE.
hongy...@gmail.com
hongyi.zhao at gmail.com
Fri Sep 10 19:48:55 EDT 2021
On Saturday, September 11, 2021 at 7:43:44 AM UTC+8, hongy... at gmail.com wrote:
> On Friday, September 10, 2021 at 2:12:31 PM UTC+8, Roland Mueller wrote:
> > pe 10. syysk. 2021 klo 8.53 hongy... at gmail.com (hongy... at gmail.com)
> > kirjoitti:
> > > On Thursday, September 9, 2021 at 8:57:37 PM UTC+8, Roland Mueller wrote:
> > > > Hello
> > > >
> > > > to 9. syysk. 2021 klo 6.53 hongy... at gmail.com (hongy... at gmail.com)
> > > > kirjoitti:
> > > > > I'm using the following code in my forked project [1]:
> > > > >
> > > > > percol.view.STACKLINE = 'Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s
> > > Dir:M-d
> > > > > Dircmd:M-b'
> > > > >
> > > > > I would like to change the display style of the text mentioned above,
> > > for
> > > > > example, to underline some characters in it, as shown below:
> > > > >
> > > > > _D_ir:M-d
> > > > >
> > > > >
> > > > You can use e.g. str.replace() or re.sub()
> > > >
> > > > >>> percol.view.STACKLINE = percol.view.STACKLINE.replace('D', '_D_')
> > > > Result: 'Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s _D_ir:M-d
> > > _D_ircmd:M-b'
> > > >
> > > > >>> import re
> > > >
> > > > Replace D with _D_
> > > > >>> percol.view.STACKLINE = re.sub(r'([D])',
> > > > r'_\1_', percol.view.STACKLINE)
> > > > Result: 'Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s _D_ir:M-d
> > > _D_ircmd:M-b'
> > > >
> > > > Replace D and M with _D_, _M_
> > > > >>> percol.view.STACKLINE = re.sub(r'([DM])', r'_\1_',
> > > > percol.view.STACKLINE)
> > > > 'Fold:F1,F2,F3 Push:C-p Pop:_M_-p Script:_M_-s _D_ir:_M_-d
> > > _D_ircmd:_M_-b'
> > > >
> > > > Regards,
> > > > Roland
> > >
> > > I tried with the following, but failed to achieve the expected effect:
> > >
> > > class Term:
> > > HEADER = '\033[95m'
> > > OKBLUE = '\033[94m'
> > > OKGREEN = '\033[92m'
> > > WARNING = '\033[93m'
> > > FAIL = '\033[91m'
> > > ENDC = '\033[0m'
> > > LIGHTCYAN = '\033[1;36m'
> > > LIGHTGRAY = '\033[0;37m'
> > > YELLOW = '\033[0;33m'
> > > BOLD = '\033[1m'
> > > UNDERLINE = '\033[4m'
> > >
> > > [...]
> > >
> > > percol.view.STACKLINE = percol.view.STACKLINE.replace('D', Term.UNDERLINE
> > > + 'D' + Term.ENDC)
> > >
> > > The result will look like this:
> > >
> > > Fold:F1,F2,F3 Push:C-p Pop:M-p Script:M-s ?[4mD?[0mir:M-d
> > > ?[4mD?[0mircmd:M-b
> > >
> > > I cannot repeat that. Are you sure that the '?' shown in your output are
> > not due to your terminal settings that influence how strings printed by
> > Python or inside used terminal are shown?
> >
> > Python 3.9.6 (default, Jul 16 2021, 00:00:00)
> > [GCC 11.1.1 20210531 (Red Hat 11.1.1-3)] on linux
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> UL = '\033[4m'
> > >>> UL
> > '\x1b[4m'
> > >>> ENDC = '\033[0m'
> > >>> ENDC
> > '\x1b[0m'
> >
> > >>> s = UL + 'D' + ENDC
> > >>> s
> > '\x1b[4mD\x1b[0m'
> >
> > >>> s = 'ABCDE'
> > >>> s = s.replace('D', UL + 'D' + ENDC)
> > >>> s
> > 'ABC\x1b[4mD\x1b[0mE'
> >
> > When I call print(s) it even shows ABCD and D is underscored.
> If I test the code snippet above with ipython/ptpython/python, I got the same result as you described. But the problem I reported here is triggered by running the command line wrapper of the project by `Ctrl-r`, which is worked with curses library. And I also noticed the following file [1] used by the project, which may be pertinent to the problem discussed here.
> But till now I still can't think of a solution.
>
> [1] https://github.com/hongyi-zhao/ariadne/blob/master/percol/ansi.py
And refer to the following file used in the project:
https://github.com/hongyi-zhao/ariadne/blob/master/percol/display.py
Regards,
HY
More information about the Python-list
mailing list