Add `-e' option to add ANSI escape code (underline/bold) - wr - Translate a term via WordReference.com
 (HTM) hg clone https://bitbucket.org/iamleot/wr
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) changeset 248ac1bb951d9f36479205ac551ad1b01d4f43b6
 (DIR) parent 89a8d712bafdcb642ebf85ff0ea6313c98ebc2b7
 (HTM) Author: Rocky Hotas <rockyhotas@firemail.cc>
       Date:   Thu, 28 Nov 2019 12:14:29 
       
       Add `-e' option to add ANSI escape code (underline/bold)
       
       Diffstat:
        wr.py |  27 +++++++++++++++++++++------
        1 files changed, 21 insertions(+), 6 deletions(-)
       ---
       diff -r 89a8d712bafd -r 248ac1bb951d wr.py
       --- a/wr.py     Wed Nov 27 22:42:55 2019 +0100
       +++ b/wr.py     Thu Nov 28 12:14:29 2019 +0100
       @@ -190,17 +190,17 @@
            return ts
        
        
       -def print_term(term: Term):
       +def print_term(term: Term, ansi_escape: bool = False):
            """Pretty print a Term"""
       -    print('{term} {term_type}'.format(term=term.term,
       -                                      term_type=term.term_type))
       +    print('{term} {term_type}'.format(term=bold(term.term, ansi_escape),
       +                                      term_type=underline(term.term_type, ansi_escape)))
            print('{description}'.format(description=term.term_description))
            for translation, translation_context, translation_type in zip(term.translations, term.translation_contexts, term.translation_types):
                if translation_context:
                    print('{context}'.format(context=translation_context))
                print(textwrap.fill('{translation} {translation_type}'.format(
                                    translation=translation,
       -                            translation_type=translation_type),
       +                            translation_type=underline(translation_type, ansi_escape)),
                      initial_indent=' ' * 8,
                      subsequent_indent=' ' * 12,
                      width=80,
       @@ -208,6 +208,20 @@
                      break_on_hyphens=False))
        
        
       +def underline(text: str, ansi_escape: bool = False) -> str:
       +    if ansi_escape:
       +        return '\033[4m{text}\033[0m'.format(text=text)
       +    else:
       +        return text
       +
       +
       +def bold(text: str, ansi_escape: bool = False) -> str:
       +    if ansi_escape:
       +        return '\033[1m{text}\033[0m'.format(text=text)
       +    else:
       +        return text
       +
       +
        if __name__ == '__main__':
            import argparse
        
       @@ -215,6 +229,7 @@
            parser.add_argument("dictionary", type=str, help="specify the dictionaries to be used: e.g., enit")
            parser.add_argument("term", type=str, help="specify the term to be translated")
            parser.add_argument("-a", action="store_true", help="show all the translation categories", default=False)
       +    parser.add_argument("-e", action="store_true", help="format text via ANSI escape characters", default=False)
            args = parser.parse_args()
        
            mcats = ('Principal Translations', 'Additional Translations')
       @@ -229,6 +244,6 @@
                    pcat = t.term_category
                    if i > 0:
                        print()
       -            print(pcat)
       +            print(bold(pcat, args.e))
                    print()
       -        print_term(t)
       +        print_term(t, args.e)