Find text in console
Hello, do you know a way (I searched in the forum but not found answer) to find text (also with regex) in console output field, select it, copy it in clipboard after the execution of a script or a command?
Simple example: I write in the console command line
print("hello dear")and press run: Pythonista executes the command and prints
hello dearin the console output window. Well, I'd like, when I'm in console environment and as post-processing, to find only word
dear(with direct searching of word
dearor with regex, with pattern that selects all after word
hello), select and copy it, or simply select it with a colored highlighting (in the same way user can do it in editor environment of Pythonista, where Pythonista highlights the searched text).
So, can Pythonista allow user to find text in console output? If not, do you know some existing obj-c script written by someone to create a wrench button to search and highlight text in console (maybe with pop-up in order to insert the word to be searched)?
@Matteo I think I'm wrong, very sorry. I never got full console history, at least it seems.
I'll search for you, but not free immediately
Hi @cvp , thank you so much for your time but don't worry, don't search for me, only if it is also your interest, no problem, really :-).
My interest is if a code already exists that can search text in console, when there is a lot text in it and user doesn't want to search some specific words by scolling down the screen.
I will try to search in official Apple doc about some obj-c for interaction with OMTextView. Maybe I will find something here (found now).
Thank you for your always lively kindness in answering in this forum!
@Matteo I think that OM... objects are from OMz, thus not in Apple doc
@cvp Hi, do you mean OMxxxxx is something related to omz library, not Apple?
However, if it is programmatically impossible to interact with console output via objc, do you know something for
pythonista_startup.pythat can automatically save in a file (named for example
console_output.txt) the output of any command executed in console or any script executed from editor environment, in a consecutive mode, in the same way the output in console appears? This file should contain the exact text user sees in console output/history, and this file should update itself after each execution where there is at least one print statement (that is when the console output changes its content by adding new text output).
Do you know if something already exists?
@Matteo Please could you try this code as a Pythonista tool.
First, you run the tool,
then you run a script with a console output,
then, in console mode, you type the text fo search and tap the 🔍 icon, and you will watch the miracle 😀
Sure that the code is not bug free, but it is good to start, if interested
The OMTextView does not allow to set text attributes as an UITextView but you can draw on it
from objc_util import * import clipboard import ui @on_main_thread def test(sender): import console import re import ui txt = str(sender.console.text()) if txt[-1] == '\n': txt = txt[:-1] win = ObjCClass('UIApplication').sharedApplication().keyWindow() main_view = win.rootViewController().view() ret = '' 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 for sv in tv.subviews(): if 'SUIButton_PY3' in str(sv._get_objc_classname()): sv.removeFromSuperview() if txt == '': return t = str(tv.text()) #print('search',txt,'in',t) for m in re.finditer(txt, t): st,end=m.span() p1 = tv.positionFromPosition_offset_(tv.beginningOfDocument(), st) p2 = tv.positionFromPosition_offset_(tv.beginningOfDocument(), st+len(txt)) rge = tv.textRangeFromPosition_toPosition_(p1,p2) rect = tv.firstRectForRange_(rge) # CGRect x,y = rect.origin.x,rect.origin.y w,h = rect.size.width,rect.size.height #print(x,y,w,h) l = ui.Button() l.frame = (x,y,w,h) l.background_color = (1,0,0,0.2) l.corner_radius = 4 l.border_width = 1 tv.addSubview_(l) ret = analyze(tv) if ret: return ret ret = analyze(main_view) @on_main_thread def FindTextInConsole(): global console_tv win = ObjCClass('UIApplication').sharedApplication().keyWindow() main_view = win.rootViewController().view() ret = '' next_is_console = False def analyze(v,indent): global next_is_console ret = None for sv in v.subviews(): #print(indent,sv._get_objc_classname()) if 'UILabel' in str(sv._get_objc_classname()): #print(indent,sv.text()) if str(sv.text()) == '>': next_is_console = sv else: next_is_console = False elif 'OMTextView' in str(sv._get_objc_classname()): if next_is_console: su = next_is_console.superview() for ssv in su.subviews(): if 'SUIButton_PY3'in str(ssv._get_objc_classname()): # rerun of this script, remove previous button ssv.removeFromSuperview() b = ui.Button(name='clipboard') b.tint_color ='red' b.image = ui.Image.named('iob:ios7_search_32') b.background_color = 'white' h = su.frame().size.height b.frame = (2,2,h-4,h-4) b.action = test b.console = sv #print(dir(sv)) retain_global(b) su.addSubview(ObjCInstance(b)) ret = analyze(sv,indent+' ') if ret: return ret ret = analyze(main_view,'') return ret if __name__ == '__main__': r = FindTextInConsole()
@Matteo Sorry, I did not see your last post, just before my script.
If you check this one, you will see the line
t = str(tv.text())
it contains the full console...
do you mean OMxxxxx is something related to omz library, not Apple?
Code edited, crashed if text to search empty (to clean console).
I said there were bugs, isn't it? 😀
if txt == '': return
@cvp Hi, I haven't tried it yet your code but it is impressive! Thank you :-)
I sometimes feel that there is a possibility to do something with programming, but every time I see that one of you programmers really manages to do it, I am fascinated.
I want to try your code by saving it in the StatusBarMenu (by JonB) as an action to run with the little buttons of it. So my dream is to associate your script to a user key of StatusBarMenu in order to enable/disable the search text function.
So do you know if it is possible to disable your search function (with a click) if user wants to return to use the console to run python commands instead of to search text?
Thank you very much
@Matteo It is not needed. Even if you activate the search, the normal use of the console command still works by pressing enter. The search is only done if you tap the 🔍 icon.
@mikael Hi, I still use Pythonista version 3.1 (I have an old iphone with ios 10.3.3) and it is a great software, I find it very exciting to think that user can even customize the interface of this application as you wish, if knows how to do it ;-)
Pythonista is Great!
Pythonista is Great!
Full agree. I hope you have seen my previous post because we wrote at the same time
@cvp Yes! you are right, sorry, two buttons on console line! You created a second button at left of input line, Thank you again!
two buttons on console line
Do you see two buttons on the console line?
@cvp no no, I badly explained myself sorry, I meant I see your button search at left, at right I see the built-in recall command icons and the execute command is the return key of keyboard, yes sorry. I'm excited...
Hi @cvp, thanks again for your code, it helps me a lot! I tried to change it in order to clear highlighting of the text before each touch of the "Find" button, because if I search different words, the console becomes too colored and I can't understand where the last word is selected.
If you want and have time, can you show me how to change your code in order to remove all highlighting before each touch of your "Find" user key?
@Matteo that is strange, because when you tap the find button, I remove all colored buttons before to create new ones, thus no overlap of multiple searches.
Did you change something?
I just retry and there is no problem...
Could you post your code if not exactly mine