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)?
@cvp Hi, sorry I'm feel very unskilled, no objc problem, I must use Python3, no Python2...
Sorry I ask you :when you touch built-in key Clear in console do you have the clear of full console content and the color red of last highligthed text?
@Matteo I thought about this problem of python 2 or 3 overnight and I wanted to check this morning because I "remove SUIButton_PY3 objects" ... Great that you found it
clear of full console content and the color red of last highligthed text?
You're right, that's a problem. My script does not know when you tap the clear button thus it is not able to also clear the couloured buttons. I'll try to find something but not promised
temporarily, you would have to clear manually the console line then tap the 🔍 button, colorful buttons will be removed
Edit: if you have cleared the console, tap only the 🔍 button, even without clearing the search text, all highlighted words will disappear
@cvp You are right, to clear all I can use your Search button with empty text, perfect.
Many thanks for your help!
@Matteo And, if you tap the standard clear button, you can, after, tap the search button even without clearing the searched text. As the console is cleared, there is no occurrence to find, thus all highlighted will be removed
@Matteo small update if you want a case insensitive search
for m in re.finditer('(?i)'+txt, t):
Hi @cvp , nice, yes no need to clear the search text field! And thanks also for insensitive search!
Sorry if I ask you something else, answer only if it's something that interests you too: is it possible to have a second version of your script that can search text with python Regex? I think a second script for only Regex would be simpler than adding a setting flag (that user could change by executing a script associated to another custom user key) that allows user to switch from regex to simple text and viceversa with your original script.
For example, if I have the console output with a lot of text data and I want to select all text (numbers) after word
value:, I'd like to use the regex pattern
(?<=value: )(.*)in order to highlight all after word
value:(to the new row/line).
Do you think it is possible? I ask you this because, although it is something that would help me a lot, I can't understand how to enable Regex searching by taking inspiration from your update about insensitive search.
@Matteo these lines can find both texts or regex patterns:
for m in re.finditer(txt, t): st,en =m.span() p1 = tv.positionFromPosition_offset_(tv.beginningOfDocument(), st) p2 = tv.positionFromPosition_offset_(tv.beginningOfDocument(), en)
Please, note that if you type a text, it is case sensitive (back)
And you don't need two different search buttons
Note also that the regex you gave as example needs a space after value:
I can't understand how it is possible to have regex mode by modifying the code from
st,end=m.span() ... p2 = tv.positionFromPosition_offset_(tv.beginningOfDocument(), st+len(txt))
st,en =m.span() ... p2 = tv.positionFromPosition_offset_(tv.beginningOfDocument(), en)
Magic of programming and of those who know how to use this magic ...
Thank you very much!
@Matteo no real magic. 1st version was ok but only for a text. 2nd version really uses start and end of search
@Matteo if you want to use different colors to search different words, you can use the regex
|and modify the script as
if '|' not in txt: l.background_color = (1,0,0,0.2) else: # search multiple strings wrds = txt.split('|') idx = wrds.index(t[st:en]) cols = [(1,0,0,0.2), (0,1,0,0.2), (0,0,1,0.2), (1,1,0,0.2), (1,0,1,0.2), (0,1,1,0.2)] col = cols[idx % len(cols)] l.background_color = col
Obviously, you can put the cols initialization outside the loop but, here, it is only to show only one group of modified lines
@cvp Thank you, very nice and useful!
As a curiosity, maybe I didn't understand before: do you know if the highlighted text with your script can be also copied in clipboard or it is impossible due to limits of OMTextView?
If it is impossible to copy the highlighted text in Pythonista clipboard, do you know if a script exists that can write (in background, that is during Pythonista usage), in an external text file, the full output of the console, and the external file updates itself after each output print in console?
Thank you for your interest :-)
do you know if the highlighted text with your script can be also copied in clipboard
Sure, it can. Do you want I modify the script to copy all highlighted texts into the clipboard?
If yes, how to separate all highlighted texts?
clp = '' for m in re.finditer(txt, t): . . . tv.addSubview_(l) clp += t[st:en] + '\n' clipboard.set(clp)
Perform your search, then paste the clipboard to check its content
Here, each highlighted text will be a new line, as example
Hi @cvp, exactly, you read me in thought!
The choice to copy the highlighted text so that each word is on a different line is perfect for me, in this way after some calculation I can screenshot iphone screen with some highlighted text (also with different colors, thank you) and to send image to someone (it is easier to explain something with some text highlighted), with also the full string matched by your script as a text file, for further analysis (with spreadsheets or other tools).
Thank you !
, you read me in thought!
Nothing so magic, I only have hacked your front camera 😂
@cvp :-) , but my phone is so old and with storage memory so clogged that the hack works only randomly...;-)
@Matteo not sure that comes from the iPhone age, but perhaps due to mine 😀
I also got some problems, try the imports also at def module, because the script could be no more active but the def of the search button yes
from objc_util import * import ui @on_main_thread def test(sender): import clipboard import console from objc_util import ObjCClass import re import ui