It is currently April 19th, 2024, 8:00 am

Help creating a search box that searches a .txt file

Get help with creating, editing & fixing problems with skins
User avatar
MazinLabib10
Posts: 14
Joined: January 21st, 2023, 3:47 pm

Help creating a search box that searches a .txt file

Post by MazinLabib10 »

Hi everyone! :welcome:

A few months ago, I began working on my first fully OC skin that shows the previous and next game for any football/soccer team. Unfortunately, I had to step away from it as university work piled on. Now that I'm finally on vacation, I'm back at work on this project, but I'm stuck trying to come up with a solution to this problem and I didn't find much help online.

Basically what I'm looking to do is to create a search box in my settings skin that will allow the user to select which team they want. I have a .txt file that contains the names of 10,000 teams along with the URL to their pages on the website I'm using. What I visualize is a search box where as you type in a few characters, you are shown a few suggested teams below. When you click on the team you want, the corresponding URL is assigned to a variable for use elsewhere.

My current thought process is to try to combine the InputText and WebParser plugins to do this. The InputText would take the characters from the user and assign it to a variable. The WebParser would look for, let's say, 5 instances of the value of this variable in the .txt file and return them as suggestions. When any of these are clicked, the corresponding URL is captured. However, immediately I've identified two limitations with this method:
  1. In order for the suggestions to show up, the user would have to first submit the characters they have already typed. If they want to add more characters, they would have to submit it once more for new suggestions.
  2. The suggestions would just be the first 5 instances of the entered characters and not the most relevant 5 instances.
As of this writing, I've not managed to think of any other solutions or ways to overcome these limitations. I'm hoping you guys will have something to help me out! :sos:
User avatar
Yincognito
Rainmeter Sage
Posts: 7125
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Help creating a search box that searches a .txt file

Post by Yincognito »

MazinLabib10 wrote: May 22nd, 2023, 3:13 pmHowever, immediately I've identified two limitations with this method:
  1. In order for the suggestions to show up, the user would have to first submit the characters they have already typed. If they want to add more characters, they would have to submit it once more for new suggestions.
  2. The suggestions would just be the first 5 instances of the entered characters and not the most relevant 5 instances.
As of this writing, I've not managed to think of any other solutions or ways to overcome these limitations. I'm hoping you guys will have something to help me out! :sos:
Limitations exist to be overcome. Both your points can be tackled a bit differently, to get closer to achieving what you want. Some ideas:

1. Submitting partial strings:
a) you could simulate an "input box" (or other variants in that discussion) using the HotKey plugin, but it will be a bit overkill and I'm not entirely sure you could integrate it with the InputText one, unless you separate them, try to put one on top of another, etc.
b) you could use the FocusDismiss or OnDismissAction options of an InputText to try and "force" the content to stay the same when dismissing and look for those partial $UserInput$ instances in the text file (if $UserInput$ does retain already typed characters before submitting, that is), assuming that you make the appropriate "buttons" (e.g. Search and Submit) to replicate the two actions, instead of relying on the Enter key for submitting at each step of the editing

2. Searching by relevance: this will depend on how you define relevance. If it's about how many occurrences of that instance exist in the text file, this can probably be done by some regex kung-fu to count such instances. If it's about something else, it will depend what that something is.

Bottom line, you'll never know until you try. Just make some very simple / short / basic sample skin that captures the essence of the process (10 or 15 names are enough for the sample .txt, for example) and see how various choices behave. Personally, I never tried the suggestion thingy, but if I'm not mistaken I've seen something similar applied to online searches from a skin (e.g. weather location, if I recall correctly). Also, in case you'd want to examine some other (probably incomplete) attempts on the football skin topic, you can take a look here.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
MazinLabib10
Posts: 14
Joined: January 21st, 2023, 3:47 pm

Re: Help creating a search box that searches a .txt file

Post by MazinLabib10 »

Yincognito wrote: May 22nd, 2023, 6:25 pma) you could simulate an "input box" (or other variants in that discussion) using the HotKey plugin, but it will be a bit overkill and I'm not entirely sure you could integrate it with the InputText one, unless you separate them, try to put one on top of another, etc.
As I'm still not too experienced in Rainmeter coding, I think this one might be a bit too complex for me.
Yincognito wrote: May 22nd, 2023, 6:25 pmb) you could use the FocusDismiss or OnDismissAction options of an InputText to try and "force" the content to stay the same when dismissing and look for those partial $UserInput$ instances in the text file (if $UserInput$ does retain already typed characters before submitting, that is), assuming that you make the appropriate "buttons" (e.g. Search and Submit) to replicate the two actions, instead of relying on the Enter key for submitting at each step of the editing
I feel this has potential as something I could do. However, how would I have it so that the string is submitted only when the user clicks the "Submit" button and not anywhere else?
Yincognito wrote: May 22nd, 2023, 6:25 pm2. Searching by relevance: this will depend on how you define relevance. If it's about how many occurrences of that instance exist in the text file, this can probably be done by some regex kung-fu to count such instances. If it's about something else, it will depend what that something is.
Now that you put it like that, I've realized I haven't really put much thought into what exactly I consider relevance here. Will have to give it a good think through. Maybe I might just stick with the first few instances after all!

Anyways, I'm gonna look through those links you've referenced and see what I can come up with. Will get back to you once I'm done (which could take quite a while!). Thanks for the response!
User avatar
Yincognito
Rainmeter Sage
Posts: 7125
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Help creating a search box that searches a .txt file

Post by Yincognito »

MazinLabib10 wrote: May 22nd, 2023, 7:05 pm I feel this has potential as something I could do. However, how would I have it so that the string is submitted only when the user clicks the "Submit" button and not anywhere else?
Something like LeftMouseUpAction=[!CommandMeasure YourInputTextMeasure "ExecuteBatch 1-2"] added to your "Submit button", assuming that Command1 and Command2 from your InputText measure handle the submit part.
MazinLabib10 wrote: May 22nd, 2023, 7:05 pm As I'm still not too experienced in Rainmeter coding, I think this one might be a bit too complex for me.

Now that you put it like that, I've realized I haven't really put much thought into what exactly I consider relevance here. Will have to give it a good think through. Maybe I might just stick with the first few instances after all!

Anyways, I'm gonna look through those links you've referenced and see what I can come up with. Will get back to you once I'm done (which could take quite a while!). Thanks for the response!
Yeah, no worries - take your time and think it through. Having a clear idea of what you want is the first step in achieving it. I might come up with a sample (if all things check out) at a later time, though I can't guarentee it. ;-)
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
MazinLabib10
Posts: 14
Joined: January 21st, 2023, 3:47 pm

Re: Help creating a search box that searches a .txt file

Post by MazinLabib10 »

Yincognito wrote: May 22nd, 2023, 7:25 pm Something like LeftMouseUpAction=[!CommandMeasure YourInputTextMeasure "ExecuteBatch 1-2"] added to your "Submit button", assuming that Command1 and Command2 from your InputText measure handle the submit part.

Yeah, no worries - take your time and think it through. Having a clear idea of what you want is the first step in achieving it. I might come up with a sample (if all things check out) at a later time, though I can't guarentee it. ;-)
Just ran into a new problem. Thought about creating a new post for it but just wanted to see first if you had some idea about it.
I'm currently setting up the search functionality and have managed to get WebParser to look for the inputted string in the text file. However, now I'm having an issue with the matching. To understand what I'm looking for, here's the first few lines of the text file:

Code: Select all


01 Hoechst / https://www.besoccer.com/team/matches/sg-01-hoechst
07 Vestur / https://www.besoccer.com/team/matches/07-vestur
07 Vestur II / https://www.besoccer.com/team/matches/07-vestur-ii
07 Vestur III / https://www.besoccer.com/team/matches/07-vestur-iii
1. FC Phönix Lübeck / https://www.besoccer.com/team/matches/fc-phonix-lubeck
1. FC Saarbrücken / https://www.besoccer.com/team/matches/saarbrucken
1. Maj Ruma / https://www.besoccer.com/team/matches/1-maj-ruma
And here's the WebParser code I've come up with as of this writing, where "Team" is a variable that contains the inputted string,

Code: Select all

[MeasureSuggestions]
Measure=WebParser
URL=file:///#@#Clubs.txt
RegExp=(?siU)(#Team#.*) /
DynamicVariables=1
This works if the user inputs the first few characters of the name. However, what I need next is for it to be able to get matches even if characters within the name are entered. For example, entering "FC Saar" should give "1. FC Saarbrücken". I tried RegExp=(?siU)\r(.*#Team#.*) / but that begins from the first line onwards since the first "\r" appears there. I need it somehow to start from the last "\r" that appears before the first instance of "#Team#". Any ideas?
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5391
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Help creating a search box that searches a .txt file

Post by eclectic-tech »

Here's a completely different suggested method; use FindStr in a cmd window to search for partial strings.

An example code using your last posted team file saved as "team2.ini" in the same folder as this code.

Code: Select all

; ========= Variables ==========
[Variables]
Team=.*

; ========= Skin Settings ==========
[Rainmeter]
Update=1000
AccurateText=1
DynamicWindowSize=1
Group=#RootConfig#

OnRefreshAction=[!CommandMeasure MeasureRunCommand "Run"]

; ========= Measures ==========
[MeasureRunCommand]
Measure=Plugin
Plugin=RunCommand
; Program=%ComSpec% \U \C
Parameter=""findstr /i "#Team#" teams2.ini""
State=Hide
OutputType=ANSI
; Save the results in a file if you want to parse it in another measure.
; OutputFile=#CurrentPath#Found.txt
; FinishAction=
; StartInFolder=
TimeOut=-1
; Documentation: https://docs.rainmeter.net/manual-beta/plugins/runcommand/

[MeasureInputText]
Measure=Plugin
Plugin=InputText
InputLimit=0
InputNumber=0
X=0
Y=0
W=180
H=15
Command1=[!WriteKeyValue Variables Team "$UserInput$"]
Command2=[!Refresh]
DefaultValue=#Team#
FocusDismiss=1


; ========= Meters ==========
[MeterInputTextWrite]
Meter=String
SolidColor=0,0,0,1
FontSize=10
FontColor=255,255,255
AntiAlias=1
Text="Enter Team Name"
LeftMouseUpAction=[!CommandMeasure "MeasureInputText" "ExecuteBatch 1-2"]

[MeterString]
Meter=String
MeasureName=MeasureRunCommand
Y=20R
FontColor=255,255,255
SolidColor=0,0,0,1
Text="Search Results#CRLF#%1"
Inlinepattern=Search Results
Inlinesetting=weight | 600
Screenshot of initial list. Click the top line and enter a partial name and press "Enter" to see the search results.
teamlist.png
You do not have the required permissions to view the files attached to this post.
User avatar
Yincognito
Rainmeter Sage
Posts: 7125
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Help creating a search box that searches a .txt file

Post by Yincognito »

MazinLabib10 wrote: May 25th, 2023, 6:17 pm Just ran into a new problem. Thought about creating a new post for it but just wanted to see first if you had some idea about it.
I'm currently setting up the search functionality and have managed to get WebParser to look for the inputted string in the text file. However, now I'm having an issue with the matching. To understand what I'm looking for, here's the first few lines of the text file:

Code: Select all


01 Hoechst / https://www.besoccer.com/team/matches/sg-01-hoechst
07 Vestur / https://www.besoccer.com/team/matches/07-vestur
07 Vestur II / https://www.besoccer.com/team/matches/07-vestur-ii
07 Vestur III / https://www.besoccer.com/team/matches/07-vestur-iii
1. FC Phönix Lübeck / https://www.besoccer.com/team/matches/fc-phonix-lubeck
1. FC Saarbrücken / https://www.besoccer.com/team/matches/saarbrucken
1. Maj Ruma / https://www.besoccer.com/team/matches/1-maj-ruma
And here's the WebParser code I've come up with as of this writing, where "Team" is a variable that contains the inputted string,

Code: Select all

[MeasureSuggestions]
Measure=WebParser
URL=file:///#@#Clubs.txt
RegExp=(?siU)(#Team#.*) /
DynamicVariables=1
This works if the user inputs the first few characters of the name. However, what I need next is for it to be able to get matches even if characters within the name are entered. For example, entering "FC Saar" should give "1. FC Saarbrücken". I tried RegExp=(?siU)\r(.*#Team#.*) / but that begins from the first line onwards since the first "\r" appears there. I need it somehow to start from the last "\r" that appears before the first instance of "#Team#". Any ideas?
Sure. You have two options:
Team - Variant 1.jpg
Team - Variant 2.jpg
Basically, you can try skipping the ?s dot-all (i.e. matching newlines too) flag and use the \R to match both the \r\n and the single \n as newline markers (one in Windows, the other on Macs), taking care to escape the / by preceding with \ in the first variant, or, alternatively, you can keep using the flag and make sure you use unambiguous \N patterns for non linebreak characters instead of the typical . which with the flag matches newlines as well.

Take note of the site used for testing, the places where each thing goes, and the setting of the PCRE regex flavor in the top right corner of the page. It's a great place to test your regexes, assuming you accurately reproduce the conditions you have in the skin, and it's a great place to learn how to write regex patterns, by looking at the very helpful and concise sidebar at the left, once you clicked the RegEx Reference menu in it.
You do not have the required permissions to view the files attached to this post.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
Yincognito
Rainmeter Sage
Posts: 7125
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Help creating a search box that searches a .txt file

Post by Yincognito »

A small note on the otherwise excellent (albeit slightly slower, at least that's my impression) approach by eclectic-tech: you might want to set the encoding to UTF-8 in Notepad++ for the teams2.ini file that holds the data in his example, and accordingly set the OutputType=UTF8 in the RunCommand measure, in order to get all kinds of exotic characters to display and behave correctly.

EDIT: For some reason, forgot to add eclectic-tech's name to the above - corrected. :oops:
EDIT2: The more I look at it, the more I prefer his variant - it's hard to compete with cmd & findstr already providing everything you need in this case, ready to be formatted, put into another meter or split accordingly afterwards. I also love the outside the box approaches, it's something that didn't occur to me earlier, despite its simplicity. Certainly a variant to consider, especially if you don't need or look for near "realtime" search capabilities and do it on a less frequent basis (like pressing Enter or pushing a button). Thanks, eclectic-tech! :rosegift:
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5391
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Help creating a search box that searches a .txt file

Post by eclectic-tech »

I've been tinkering for too long... DOS methods are simple and useful in some instances :Whistle

Yes, the files encoding should be UTF-8 or ANSI to avoid seeing hieroglyphics in the results, thanks for catching that point. :welcome:
User avatar
Yincognito
Rainmeter Sage
Posts: 7125
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Help creating a search box that searches a .txt file

Post by Yincognito »

eclectic-tech wrote: May 25th, 2023, 11:00 pm I've been tinkering for too long... DOS methods are simple and useful in some instances :Whistle
So true! :thumbup:
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth