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.


    Can I use termcolor in Pythonista?

    Pythonista
    3
    8
    2395
    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.
    • bigbridge
      bigbridge last edited by

      Hi
      I use Stash and install termcolor library to use pip.

      but it doesn't work. my script is

      from termcolor import colored
      
      print(colored('red text', 'red'))
      

      Output result is

      red text
      

      Can't use termcolor library on Pythonista?

      Thanks in advance

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

        @bigbridge Is it not Python 2?

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

          It's Python3

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

            @bigbridge see here

            import console
            console.set_color(1,0,0)
            print('red text') 
            
            1 Reply Last reply Reply Quote 0
            • JonB
              JonB last edited by

              If you are talking about stash, it does support standard ansi colors.

              https://forum.omz-software.com/topic/4175/stash-is-it-possible-to-get-color-output-from-a-script-to-the-stash-console/28?page=2

              Looking at termcolor, it checks for ANSI_COLORS_DISABLED environ variable. You can edit your .stashrc to set this by default, or else could use os.setenv in your script. Stash also has its own functions for this also.

              bigbridge 1 Reply Last reply Reply Quote 1
              • cvp
                cvp last edited by cvp

                If you really want to mix colors in the same console line, try this quick and dirty code

                from   objc_util import *
                import ui
                
                UIColor = ObjCClass('UIColor')
                NSMutableAttributedString = ObjCClass('NSMutableAttributedString')
                UIFont = ObjCClass('UIFont')
                
                @on_main_thread
                def colored(txt):
                	from objc_util import ObjCClass
                	win = ObjCClass('UIApplication').sharedApplication().keyWindow()
                	main_view = win.rootViewController().view() 
                	ret = ''
                	font = UIFont.fontWithName_size_('Menlo', 15)
                	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:
                					if isinstance(ele,tuple):
                						t = ele[0]
                						c = ele[1]
                					else:
                						t = ele
                						c = 'black'
                					color = UIColor.colorWithRed_green_blue_alpha_(*ui.parse_color(c))
                					attr_str = NSMutableAttributedString.alloc().initWithString_(t)				
                					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([('red text', 'red'), ' normal ', ('blue text', 'blue')])
                	print()
                

                bigbridge 1 Reply Last reply Reply Quote 1
                • bigbridge
                  bigbridge @JonB last edited by

                  @JonB said:

                  Looking at termcolor, it checks for ANSI_COLORS_DISABLED environ variable. You can edit your .stashrc to set this by default, or else could use os.setenv in your script. Stash also has its own functions for this also.

                  Thank you for your reply.

                  I'll check it.

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

                    @cvp
                    I really appreciate your kindness reply.

                    I can output 3 colors on console and I study your script.

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