omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    ANSI escape sequence codes

    Pythonista
    3
    11
    713
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Phlurim
      Phlurim last edited by

      The ANSI escape sequence codes don’t seem to work for printing bold text in Pythonista 3 on my iPad Pro. Many forum contributors suggest code like the following, which I copy & paste. And they all say it’s as simple as this. It does seem simple, which is exactly what I’m looking for. But it doesn’t seem to work for me.

          *print("This is bold text - looks like:",'\033[1m' + 'Python' + '\033[0m')*
      

      My Pythonista 3 console output:

          *This is bold text - looks like: [1mPython[0m*
      

      And the word ‘Python’ is not shown as bolded.

      Am I forgetting something? Is there a particular module that I’m suppose to import first? Is ANSI recognized by Pythonista? Is there a unicode alternative to ANSI?

      Please advise. Thank you.

      cvp 2 Replies Last reply Reply Quote 0
      • cvp
        cvp @Phlurim last edited by

        @Phlurim printing on the console in Pythonista does not support ANSI commands

        1 Reply Last reply Reply Quote 0
        • cvp
          cvp @Phlurim last edited by

          @Phlurim if really needed and important, you could use this little function I had written for somebody else who wanted colored printing in the console.
          I just have modified it to support colored and bold in the console.

          from   objc_util import *
          import ui
          
          UIColor = ObjCClass('UIColor')
          NSMutableAttributedString = ObjCClass('NSMutableAttributedString')
          UIFont = ObjCClass('UIFont')
          
          @on_main_thread
          def colored_bold(txt):
          	from objc_util import ObjCClass
          	win = ObjCClass('UIApplication').sharedApplication().keyWindow()
          	main_view = win.rootViewController().view() 
          	ret = ''
          	font = UIFont.fontWithName_size_('Menlo', 15)
          	traits = 0 | 1 << 1
          	font_bold = UIFont.fontWithName_size_traits_('Menlo', 15,traits)
          
          	def analyze(v):
          		for tv in v.subviews():
          			if 'OMTextView' in str(tv._get_objc_classname()):
          				su = tv.superview()
          				if 'OMTextEditorView' in str(su._get_objc_classname()):	
          					continue
          				# tv = console is a OMTextView baseClass = UIScrollView
          				#print(dir(tv))
          				if isinstance(txt,list):
          					txt_list = txt
          				else:
          					txt_list = [txt]
          				for ele in txt_list:
          					bold = False
          					if isinstance(ele,tuple):
          						t = ele[0]
          						c = ele[1]
          						if 'bold' in ele:
          							bold = True
          					else:
          						t = ele
          						c = 'black'
          					color = UIColor.colorWithRed_green_blue_alpha_(*ui.parse_color(c))
          					attr_str = NSMutableAttributedString.alloc().initWithString_(t)		
          					if bold:
          						attributes = {ns('NSColor'):color, ns('NSFont'):font_bold}
          					else:		
          						attributes = {ns('NSColor'):color, ns('NSFont'):font}
          					attr_str.setAttributes_range_(attributes, NSRange(0, len(t)))
          					tv.appendAttributedText_(attr_str)
          			ret = analyze(tv)
          			if ret:
          				return ret
          	ret = analyze(main_view)
          
          if __name__ == '__main__':
          	colored_bold([('red text', 'red'), ' normal ', ('blue text', 'blue','bold')])
          	print()
          

          1 Reply Last reply Reply Quote 1
          • Phlurim
            Phlurim last edited by

            Many thanks. Your code works great on my iPad.

            1 Reply Last reply Reply Quote 0
            • Phlurim
              Phlurim last edited by

              I’m a newbie, and am having lots of fun playing with your slick code. Thx again for sharing it.

              cvp 1 Reply Last reply Reply Quote 0
              • cvp
                cvp @Phlurim last edited by cvp

                @Phlurim For info, your print line works in Pyto app

                1 Reply Last reply Reply Quote 0
                • ccc
                  ccc last edited by

                  Pyto bundles colorama...

                  • https://github.com/tartley/colorama#colored-output
                  1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp last edited by

                    @Phlurim thus, you have some ways in Pyto (see @ccc last post ) but it is not easy to choose between Pythonista and Pyto, only in function of this functionality. That's the problem for a big number of Pythonista's users (including me): leave Pythonista for Pyto or not.

                    1 Reply Last reply Reply Quote 1
                    • Phlurim
                      Phlurim last edited by

                      Thanks. Before I installed Pythonista on my iPad Pro, I had already installed Pyto
                      last winter on my Mac desktop (Monterey Intel). Your code works great via Pythonista, but I'll give the ANSI escape sequence code a try on Pyto.

                      I find that Pythonista, and it’s tablet environment, are more newbie-friendly than Pyto, for my particular style of self-taught learning. Am having fun playing with Python at my own leisurely pace (my ‘hobbies’!) It’s also good therapy for propping up my aging memory (I'm a 77½ yr old retired Canadian electrical engineer with a ‘not-dead-yet’ insatiable desire for learning new things.)

                      One final question: I’ve seen the occasional on-line comment that Python 3 and Pythonista 3 are now obsolete, and that programmers should abandon them, and upgrade ASAP. Is this true?

                      Thx

                      cvp 1 Reply Last reply Reply Quote 0
                      • ccc
                        ccc last edited by ccc

                        Python is not obsolete... https://pythonbytes.fm/episodes/show/298/unstoppable-python

                        • https://www.infoworld.com/article/3669232/python-popularity-still-soaring.html
                        • https://www.infoworld.com/article/3636789/python-tops-tiobe-language-index.html
                        • https://pypl.github.io/PYPL.html
                        1 Reply Last reply Reply Quote 0
                        • cvp
                          cvp @Phlurim last edited by

                          @Phlurim said

                          I'm a 77½ yr old retired Canadian electrical engineer with a ‘not-dead-yet’ insatiable desire for learning new things.

                          I'm a 72½ yr old retired Belgian electrical engineer with a ‘not-dead-yet’ insatiable desire for learning new things. 😉

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post
                          Powered by NodeBB Forums | Contributors