• MikeBeard

    I really like this idea.

    I’ve got this set up and put both the sharededlibs.py file and the sharedlibs_list.txt file in the generic site-packages directory. I’m able to run the sharedlibs.py script directly and get it to list the “packages” I have put into the file.

    When I try to to actually use it now, I get an error:

    stash: <class 'ModuleNotFoundError'>: No module named 'my_cmd'
    

    I have a directory called “my_cmd” (and it is the path I use in the txt file above) with a __init__.py and BaseCmd.py file in it.

    So, am I missing something? Probably am, but not seeing it! :-D Any help would be appreciated!

    Thanks!
    Mike

    posted in Pythonista read more
  • MikeBeard

    @Darren

    You're welcome! Glad to help.

    posted in Pythonista read more
  • MikeBeard

    @Darren

    Was finally able to test on my script on bash, so a small addition (actually just replace the getTerminalColorClass() function with the following:

    def getTerminalColorClass():
        if '_stash' in globals() or '_stash' in locals():
            if _stash != None:
                return StashTerminalColors()
        
        return TerminalColors()
    

    How I was checking before was never going to work! ;-)

    posted in Pythonista read more
  • MikeBeard

    @Darren

    Hi Darren!

    I made the classes (shown below) to handle the color. I had the TerminalColors class already written, so didn't want to change the interface to handle both cases, so I created the getTerminalColorClass() function to key off of the existence (or not) of the _stash variable that can give access to the text_xxx functions.

    There is certainly some refactoring that could be done -- just haven't gotten to it yet. If I used the ANSI codes, as suggested, then it's likely all I would need to change are the actual definitions of the colors. I'd probably use some sort of enum that is defined differently like the getTerminalColorClass() function.

    def getTerminalColorClass():
        if _stash != None:
            return StashTerminalColors()
        
        return TerminalColors()
        
    
    class TerminalColors(object):
        """
        here is a good url for the different colors and things:
            http://blog.taylormcgann.com/tag/prompt-color/
        """
        HEADER = '\033[95m'
        BLUE = '\033[94m'
        DARK_BLUE = '\033[34m'
        GREEN = '\033[92m'
        DARK_GREEN = '\033[32m'
        YELLOW = '\033[93m'
        DARK_YELLOW = '\033[33m'
        RED = '\033[91m'
        DARK_RED = '\033[31m'
        BOLD = '\033[1m'
        UNDERLINE = '\033[4m'
        CYAN = '\033[96m'
        DARK_CYAN = '\033[36m'
        WHITE = '\033[97m'
        PURPLE = '\033[95m'
        DARK_PURPLE = '\033[35m'
        END = '\033[0m'
    
        def __init__(self):
            pass
    
        def simple_print(self, color, str_msg):
            print( "%s%s" % (self.get_color_string(color, str_msg), self.END))
    
        def complex_print(self, color, str_format, data):
            str_msg = str_format % data
            self.simple_print( color, str_msg )
            
        def multi_color_print(self, color_str_tuple_list):
            str_msg = ""
            for clr, str in color_str_tuple_list:
                str_msg += self.get_color_string(clr, str)
            self.println(str_msg)
            
        def get_color_string(self, color, str_msg):
            return "%s%s" % (color, str_msg)
            
        def println(self, str_msg):
            print( "%s%s" % (str_msg, self.END) )
    
    
    class StashTerminalColors (object):
        
        # from shcommon.py in stash code
        
        HEADER = 'UNDERLINE'
        BLACK = 'black'
        RED = 'red'
        GREEN = 'green'
        BROWN = 'brown'
        BLUE = 'blue'
        MAGENTA = 'magenta'
        PURPLE = 'magenta'
        CYAN = 'cyan'
        WHITE = 'white'
        GRAY = 'gray'
        YELLOW = 'yellow'
        SMOKE = 'smoke'
        DEFAULT = 'white'
        STRIKE = 'STRIKE'
        BOLD = 'BOLD'
        UNDERLINE = 'UNDERLINE'
        BOLD_ITALIC = 'BOLD_ITALIC'
        ITALIC = 'ITALIC'
        END = 'END'
            
        def __init__(self):
            self._stash = _stash
            
        def simple_print(self, color, str_msg): 
            print("%s" % self.get_color_string(color, str_msg))
            
        def get_color_string(self, color, str_msg):
            if color == self.BOLD:
                prn_str = _stash.text_bold(str_msg)
            elif color == self.UNDERLINE:
                prn_str = _stash.text_underline(str_msg)
            elif color == self.BOLD_ITALIC:
                prn_str = _stash.text_bold_italic(str_msg)
            elif color == self.ITALIC:
                prn_str = _stash.text_italic(str_msg)
            elif color == self.STRIKE:
                prn_str = _stash.text_strikethrough(str_msg)
            else:
                prn_str = _stash.text_color(str_msg, color)
                
            return prn_str
            
        def multi_color_print(self, color_str_tuple_list):
            str_msg = ""
            for clr, str in color_str_tuple_list:
                str_msg += self.get_color_string(clr, str)
            print(str_msg)
            
    

    The class is pretty simple to use (written explicitly for my use case, so ... YMMV). I'll give examples of usage for both below.

    Here are some examples of using simple_print():

    tc = getTerminalColorClass()
    
    tc.simple_print(tc.BLUE, "this should be BLUE")
    print("hopefully, this is NOT blue")
    tc.simple_print(tc.DEFAULT, "this is the DEFAULT color (white)")
    tc.simple_print(tc.BROWN, "this is BROWN text")
    tc.simple_print(tc.BOLD, "this should be BOLD")
    tc.simple_print(tc.UNDERLINE, "this should be UNDERLINE")
    tc.simple_print(tc.STRIKE, "this should be STRIKETHROUGH")
    tc.simple_print(tc.ITALIC, "this should be ITALIC")
    tc.simple_print(tc.BOLD_ITALIC, "this should be BOLD ITALIC")
       
    
    # will combine multiple effects
    msg = '\n  ID  Status   Date(-t)           Owner(-u)   Description (-d)\n'
    tc.simple_print(self.tc.BOLD + self.tc.UNDERLINE, msg)
    

    Here is some example for multi_color_print():

    def print_status_footer():
        tc = getTerminalColorClass()
    
        # statusLine = "Status: [+]add [@]block [-]reject [*]accept [#]workon [.]finish"
        prn_list = [
            (tc.WHITE,      "Status: "),
            (get_color_for_status('+'), "[+]add "),
            (get_color_for_status('@'), "[@]block "),
            (get_color_for_status('-'), "[-]reject "),
            (get_color_for_status('*'), "[*]accept "),
            (get_color_for_status('#'), "[#]workon "),
            (get_color_for_status('.'), "[.]finish ")
        ]
        tc.multi_color_print(prn_list)
        tc.simple_print(tc.WHITE, "        ST=status  PR=priority")
    

    The commented out line shows what it was before I added color to it. The get_color_for_status() just returns one of the colors defined in the terminal color class.

    I hope that this helps!
    Mike

    posted in Pythonista read more
  • MikeBeard

    @AtomBombed

    I forgot and didn't mention -- I went the other way! I had this working on Mac terminal with colors and wanted it to work in StaSh. Now, it selects things dynamically and should work on either without any changes!

    Thanks again!
    Mike

    posted in Pythonista read more
  • MikeBeard

    @AtomBombed

    Thanks AtomBombed! That is very similar to what I was using with standard sh/bash escape codes.

    I might update my code to use this, but it's working well right now, so ...

    It's good to have the escape codes in one place though!

    Thanks again!
    Mike

    posted in Pythonista read more
  • MikeBeard

    @JonB

    Thanks JonB! I certainly got those from the Pythonista docs. Was under the impression that was how StaSh was doing it. I didn't go down the code chain as far as I should have.

    Thanks for the color references. That will work for now. I really didn't relish working through the couple hundred different color names I found on the web for CSS colors. You saved me some work! Thanks!

    Good to know that there is another way to set text with a tuple. I might give that a look. With what you gave me though, probably won't need to do anything with that for awhile though!

    Thanks again for the help!
    Mike

    posted in Pythonista read more
  • MikeBeard

    @ccc

    Thanks ccc! I'll give that a look. I was using the terminal escape codes for a bash script, but they don't work in StaSh. A different way sounds fun!

    Thanks again!

    posted in Pythonista read more
  • MikeBeard

    So, another question on this -- it seems that there is something not quite right.

    Looking at the documentation, it states:

    When setting this attribute, you can pass a string (CSS color name or hex, e.g. 'red' or '#ff0000'), a tuple (e.g. (1.0, 0.0, 0.0, 0.5) for half-transparent red), or a number (e.g. 0.5 for 50% gray).
    

    See the code below:

    print "text_color test"
        print _stash.text_color("red text - using color 'red'", 'red')
        print _stash.text_color("red text - using #ff0000", "#ff0000")
        print _stash.text_color("red text - using (1.0, 0.0, 0.0, 1.0)")
        print _stash.text_color("grey text - using 0.75")
        print "\n\n"
    
        ''' output is:
            
            text_color test
            red text - using color 'red' -- (this is actually red in output!)
            red text - using #ff0000
            red text - using (1.0, 0.0, 0.0, 1.0)
            grey text - using 0.75
        '''
    

    As noted with a comment, the only text that actually prints out red is when I'm using the 'red' color to text_color. Also, the last line (just a number -- 0.75) doesn't do anything except output standard white text.

    Is this expected? Should I be able to pass in the other ways to print red of a shade?

    Interestingly, the editor will correctly interpret the color values correctly by bringing up a 'popup' of the correct color. So, needless to say, I'm a little confused.

    As an aside, does anyone know what color values are actually valid?

    I have looked at the code, but haven't been able to find where the list of colors are (if there is such a thing). I've tried a bunch but only found these to actually give me colored output:

    • red
    • yellow
    • green
    • blue
    • black
    • grey
    • brown
    • cyan
    • magenta

    Thanks for any help or direction on this!
    Mike

    posted in Pythonista read more
  • MikeBeard

    @JonB

    That seems to work now. At least I can get the _stash variable and it's defined.

    Now, on to actually getting it to do what I'd like!

    Thanks for the help!
    Mike

    posted in Pythonista read more
  • MikeBeard

    @JonB

    I hadn't looked as closely to your other reply before sending my first reply. Sigh. You answered my questions there. Sorry for the noise.

    Thank you for showing the difference between the two calls. Good to know.

    I'm still curious as to the why? If you don't mind explaining!

    Thanks so much!
    Mike

    posted in Pythonista read more
  • MikeBeard

    @JonB
    Hi JonB! I'm using the 'python script'. It never occurred to me that I could just run the script, though it probably should.

    I'll try that in a bit when I get a chance later this morning.

    What would the difference be between the two?

    Thanks!

    posted in Pythonista read more
  • MikeBeard

    @JonB
    Maybe I'm missing something here. I tried to just use _stash, and I am told that it is undefined. When I try to get _stash from the go.get_objects(), I'm told that it can't find the global variable gc.

    Am I supposed to be importing something to get access to these?

    To be clear, this is the code that I'm running -- in a very simple script where I do the following:

            print "globals variable is"
            print globals()
            print "locals are"
            print locals()
            print "_stash variable is"
            #print _stash
            
            _stash=[o for o in gc.get_objects() if 'StaSh' in str(type(o))][0]
            print "get from gc -- _stash is\n", _stash
            
            _stash = globals()['_stash']
    

    I'm running this from the StaSH console.

    Anyway, any insights would be gladly accepted.

    Thanks,
    Mike

    posted in Pythonista read more
  • MikeBeard

    @JonB

    Thanks JonB. I'll have to give it a look later, but that would explain some things. I do pass in arguments, so ...

    Also, thanks for the explanations. I'll play around with it and might have some questions later! 😃

    Thanks again!
    Mike

    posted in Pythonista read more
  • MikeBeard

    @ccc
    I printed out both globals() and locals() and do not see it in either, so maybe I'm doing something wrong/unexpected?

    Also, when I run the script IN StaSH versus in Pythonista, I get slightly different results (globals seems to have a LOT more when run in StaSH), but locals is pretty slim in both.

    Thanks for the help!

    posted in Pythonista read more
  • MikeBeard

    @ccc

    This seems to be for the Pythonista console window, which is good knowledge to have, but it doesn't seem that it applies to the 'console' windows that StaSH is using (which is a iOS window control of some sort?).

    Thanks for the good tip though. It will be useful for some other things I was thinking about!

    posted in Pythonista read more
  • MikeBeard

    So, I tried to go the route that it looks like ls and telnet use in StaSH by using the following:

    _stash = globals()['_stash']

    Then, once you have that, then you can use __stash.text_color() and the like.

    The only issue is that when I try this using a script that is run IN StaSH, I get: Error: '_stash'.

    To me, this means that this variable is NOT in the globals dictionary.

    So, is there something else that a script running in StaSH (but not part of StaSH itself) needs to do? Does anyone have an idea?

    Thanks so much! I'll keep looking and trying as well, but any help would be appreciated.

    Thanks again,
    Mike

    posted in Pythonista read more
  • MikeBeard

    Thanks everyone. I'll take a look at both.

    posted in Pythonista read more
  • MikeBeard

    I'm trying to see if there is a way to get colorized output from a script I'm running.

    In regular Python, I get color, as I'm using escape codes to the terminal. In StaSH, I obviously don't get that (and thankfully, I don't see the escape codes either, so that's good).

    Is there a way to do this programmatically from Python, if I know I'm working with StaSH?

    Any help or pointers would be appreciated.

    Thanks,
    Mike

    posted in Pythonista read more
Internal error.

Oops! Looks like something went wrong!