read file to match input as you write and show it below Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jomaweb
Posts: 11
Joined: 20 Dec 2020, 06:01

read file to match input as you write and show it below

20 Dec 2020, 06:11

I have a very simple working script that reads a file and shows any line with a matching text from user input.
I want such a script to use as "cheatsheet hotkeys helper" when working, so my file has this structure (just an example):

vscode open file Ctrl + O
vscode save file Ctrl + S
Spyder compile program Ctrl + Alt + C
Chrome vimium open bookmark O


My script:

Code: Select all

^k::
InputBox, SearchString, Search in C:\Users\jomaweb\hotkeys.txt, Entering the String to search
Lines =
Loop, Read, C:\Users\jomaweb\hotkeys.txt
	If InStr(A_LoopReadLine, SearchString)
		Lines .= SubStr(A_LoopReadLine, 1+InStr(A_LoopReadLine,"/",,0)) "`r`n"
MsgBox, % Lines
Working ok so far but i would like a simple improvement I can not figure how to achieve: open the file and show the matching lines in real time as I write:
So when I write "vs" only vscode lines will be shown and if I continue writing "vscode op" just the first line will be shown (no need to press enter and show results in same window not a new msgbox)

is that possible?
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: read file to match input as you write and show it below

20 Dec 2020, 07:22

Yes. If you get the input through Gui instead of InputBox, an edit field can run your routine whenever the field changes.
User avatar
Smile_
Posts: 859
Joined: 03 May 2020, 00:51

Re: read file to match input as you write and show it below

20 Dec 2020, 07:46

Code: Select all

^k::
    If WinExist("Search in C:\Users\jomaweb\hotkeys.txt")
    {
        Gui, Show,, Search in C:\Users\jomaweb\hotkeys.txt
        Return
    }
    Gui, Add, Text,, Entering the String to search
    Gui, Add, Edit, w350 h20 vText gSearch_Display
    Gui, Add, Edit, w350 h200 vDisplay +HScroll
    Gui, Add, Button, xm+305 ym+250 w45, Exit
    Gui, Show,, Search in C:\Users\jomaweb\hotkeys.txt
Return

ButtonExit:
GuiClose:
ExitApp

Search_Display:
    Gui, Submit, NoHide
    Lines := ""
    Loop, Read, C:\Users\jomaweb\hotkeys.txt
    {
        If InStr(A_LoopReadLine, Text)
            Lines .= SubStr(A_LoopReadLine, 1+InStr(A_LoopReadLine,"/",,0)) "`r`n"
    }
    GuiControl,, Display, % Lines
    If (Text = "")
        GuiControl,, Display, % ""
Return
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: read file to match input as you write and show it below

20 Dec 2020, 09:10

A bit easier:

Code: Select all

Gui, 1: New,, Search in C:\Users\jomaweb\hotkeys.txt
Gui, Add, Text,, Enter the string to search
Gui, Add, Edit, w300 vQuery gShowResult
Gui, Add, Edit, wp h200
FileRead, contents, C:\Users\jomaweb\hotkeys.txt
Return

^k:: Gui, Show

ShowResult:
   GuiControlGet, Query
   p := "\V*\Q" . Query . "\E\V*\R?"
   text := Query = "" ? "" : RegExReplace(contents, "si).*?(" . p . ")|.*", "$1")
   GuiControl,, Edit2, % text
   Return
jomaweb
Posts: 11
Joined: 20 Dec 2020, 06:01

Re: read file to match input as you write and show it below

21 Dec 2020, 13:20

Hi @teadrinker this is really simpler.

is there any way of searching words not together?

IMagine this file:

Code: Select all

vim move jump to next  keybinds...
vim file save document  keybinds...
Vscode .....

If I search "vim jump" nothing is shown.

I will study both scripts to be a little more proficient in Ahk scripts. Thanks a lot
Last edited by jomaweb on 21 Dec 2020, 13:29, edited 3 times in total.
jomaweb
Posts: 11
Joined: 20 Dec 2020, 06:01

Re: read file to match input as you write and show it below

21 Dec 2020, 13:21

thanks @mikeyww

I will study your script to learn (newbie here)

Regards
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: read file to match input as you write and show it below

21 Dec 2020, 13:23

I didn't exactly provide one! :) It looks like you still have some good options here.
jomaweb
Posts: 11
Joined: 20 Dec 2020, 06:01

Re: read file to match input as you write and show it below

21 Dec 2020, 13:27

@Smile_
Great!

is there any way of searching words not together?

IMagine this file:

Code: Select all

vim move jump to next
vim file save document
Vscode .....

If I search "vim jump" nothing is shown.
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: read file to match input as you write and show it below  Topic is solved

21 Dec 2020, 13:52

jomaweb wrote: is there any way of searching words not together?
Try this one:

Code: Select all

Gui, 1: New,, Search in C:\Users\jomaweb\hotkeys.txt
Gui, Add, Text,, Enter the string to search
Gui, Add, Edit, w300 vQuery gShowResult
Gui, Add, Edit, wp h200
FileRead, contents, C:\Users\jomaweb\hotkeys.txt
Return

^k:: Gui, Show

ShowResult:
   GuiControlGet, Query
   p := ""
   for k, v in StrSplit(Query, " ")
      (v != "" && p .= "\V*\Q" . v . "\E")
   p .= "\V*\R?"
   text := Query = "" ? "" : RegExReplace(contents, "si).*?(" . p . ")|.*", "$1")
   GuiControl,, Edit2, % text
   Return
jomaweb
Posts: 11
Joined: 20 Dec 2020, 06:01

Re: read file to match input as you write and show it below

21 Dec 2020, 13:59

@teadrinker that is!
thanks a lot from a newbie

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Google [Bot], macromint, peter_ahk and 334 guests