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.
StaSH - is it possible to get color output from a script to the StaSH console?
-
-
Stash includes
pyte
which is a terminal emulator, and includes ANSI escape codes.I thought you could use normal print commands from within stash with the appropriate escape codes --though possibly not all of them are supported. But, I could be wrong. stash also has some other "shortcut" methods -- see for example
ls.py
within stash/bin which uses color.telnet.py
also supports color, I believe by instantiating. pyte.Screen directly, though I am not entirely sure how it gets attached to the stash pyte stream. -
Thanks everyone. I'll take a look at both.
-
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 -
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!
-
If it is not in globals() then is it in locals()?
-
@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!
-
_stash is available right after running launch_stash, (in the 2.7 console, not 3.0)
It gets cleared when globals are cleared, so when running a script from the play button it will not be available without extra work.. I thought we used to protect that variable, but i guess not. But, You can find it again (in the 2.7 interpreter) as follows:_stash=[o for o in gc.get_objects() if 'StaSh' in str(type(o))][0]
For scripts run from within stash, the variable is there automatically, which is why you see ls just use it.
myfolder/myscript.py: print(_stash)
....
cd myfolder myscript
if you run you script using the
python
commandpython myscript arg1
there seems to be a bug in bin/python.py. It populates _stash for non-argument scripts, or scripts run as modules, but does not if the script has arguments, or using -c. runpy takes init_globals, so we should be populating that.
normal commands like you'd find in bin, or I think in the current path, are run through exec_py_file inside shruntime.py, which populates the namespace (globals/locals).
Finally, stash provides the following ways to format strings (in stash.py)
def text_color(self, s, color_name='default', **kwargs): return self.text_style(s, {'color': color_name}, **kwargs) def text_bgcolor(self, s, color_name='default', **kwargs): return self.text_style(s, {'bgcolor': color_name}, **kwargs) def text_bold(self, s, **kwargs): return self.text_style(s, {'traits': ['bold']}, **kwargs) def text_italic(self, s, **kwargs): return self.text_style(s, {'traits': ['italic']}, **kwargs) def text_bold_italic(self, s, **kwargs): return self.text_style(s, {'traits': ['bold', 'italic']}, **kwargs) def text_underline(self, s, **kwargs): return self.text_style(s, {'traits': ['underline']}, **kwargs) def text_strikethrough(self, s, **kwargs): return self.text_style(s, {'traits': ['strikethrough']}, **kwargs)
Combinations can also be made with text_style, and most ansi strings from other scripts should just work.
-
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 -
@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 -
How are you running the command from within stash?
i.e
[~/Documents] myscript
or
[~/Documents] python myscript
?
-
gc
is the garbage collector module. import gc. That method would be if you have an external script (that you run from the play menu) and want to interact with a running stash instance. But if you want to run from within stash, _stash is already there for you.[~/Documents]$ version StaSh v0.6.18 Pythonista 3.1.1 (311004) iOS 8.4.1 (32-bit iPad3,3) root: ~/Documents/site-packages/stash stash.py: 2017-07-17 07:49:40 SELFUPDATE_BRANCH: master BIN_PATH: ~/Documents/bin ~/Documents/stash_extensions/bin ~/Documents/site-packages/stash/bin [~/Documents]$ cat myscript.py print "_stash variable is" print _stash [~/Documents]$ myscript _stash variable is <stash.stash.StaSh object at 0x726a4cc> [~/Documents]$ myscript arg _stash variable is <stash.stash.StaSh object at 0x726a4cc> [~/Documents]$ myscript.py _stash variable is <stash.stash.StaSh object at 0x726a4cc> [~/Documents]$ python myscript.py _stash variable is Error: name '_stash' is not defined [~/Documents]$ python myscript.py arg _stash variable is Error: name '_stash' is not defined [~/Documents]$
-
@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!
-
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 -
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 -
I'd say the difference is mainly a "bug" in bin/python.py, in that it does not populate the namespace. You can compare the code by looking at system/shruntime.py, and bin/python.py.
-
Thanks!
-
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 -
You might look at how latte does colored text in StaSH. Perhaps you need to turn the color off before applying a new color.
-
I think you might be confusing pythonista docs with stash docs?
Anyway, in stash, stash.text_color calls stash.style_text, which looks up colors via
graphics
, which is an imported alias of shcommon.Graphics. If you open up shcommon.py, you will see the list of colors30: "black", 31: "red", 32: "green", 33: "brown", 34: "blue", 35: "magenta", 36: "cyan", 37: "white", 39: "default", # white. 50: "gray", 51: "yellow", 52: "smoke",
These are the standard ANSI color code definitions -- stash supports a reasonable approximation of an ANSI terminal (via pyte).
The stash terminal actually has another text_color, which takes a tuple... but you would need to go a little lower level. For basic work, stick with these.