diff --git a/app.go b/app.go index 05950db..a9ea596 100644 --- a/app.go +++ b/app.go @@ -3,8 +3,10 @@ package main import ( "context" "fmt" + "io/ioutil" "os" "os/exec" + "regexp" "strings" "github.com/wailsapp/wails/v2/pkg/runtime" @@ -15,12 +17,24 @@ type App struct { ctx context.Context inputList []string + + remeberedPath []string +} + +func GetRemeberPath() (string, error) { + homedir, err := os.UserHomeDir() + if err != nil { + return "", err + } + + return fmt.Sprintf("%s/.config/railgun-remeber", homedir), nil } // NewApp creates a new App application struct func NewApp() *App { return &App{ - inputList: []string{}, + inputList: []string{}, + remeberedPath: []string{}, } } @@ -28,6 +42,24 @@ func NewApp() *App { // so we can call the runtime methods func (a *App) startup(ctx context.Context) { a.ctx = ctx + + remeberPath, err := GetRemeberPath() + if err != nil { + return + } + if _, err := os.Stat(remeberPath); err != nil { + return + } + data, err := ioutil.ReadFile(remeberPath) + + base := strings.Split(string(data), "\n") + a.remeberedPath = []string{} + + for _, elm := range base { + if elm != "" { + a.remeberedPath = append(a.remeberedPath, elm) + } + } } func (a *App) Close() { @@ -81,6 +113,37 @@ func (a *App) SearchMathEql(value string) *string { return nil } +func (a *App) SearchRemeberDir(value string) []string { + if value == "" { + if len(a.remeberedPath) >= 5 { + return a.remeberedPath[0:6] + } + return a.remeberedPath + } + + reg, err := regexp.Compile(value) + if err != nil { + return []string{} + } + + if len(a.remeberedPath) == 0 { + return []string{} + } + + n_list := []string{} + + for _, elm := range a.remeberedPath { + if reg.MatchString(elm) { + n_list = append(n_list, elm) + } + if len(n_list) >= 5 { + return n_list + } + } + + return n_list +} + // Greet returns a greeting for the given name func (a *App) Search(value string) []string { if len(a.inputList) == 0 { @@ -122,3 +185,8 @@ func (a *App) Enter(value string) { fmt.Printf("%s", value) runtime.Quit(a.ctx) } + +func (a *App) EnterDir(value string) { + fmt.Printf("ghostty --working-directory=%s", value) + runtime.Quit(a.ctx) +} diff --git a/frontend/src/App.svelte b/frontend/src/App.svelte index 9731eb2..a9a2e5e 100644 --- a/frontend/src/App.svelte +++ b/frontend/src/App.svelte @@ -1,171 +1,206 @@
-
- { - if (e.key === "Escape") { - Close(); - } else if (e.key === "ArrowDown") { - selectedI = Math.min(selectedI + 1, (search ?? []).length); - } else if (e.key === "ArrowUp") { - selectedI = Math.max(selectedI - 1, 0); - } else if (e.key === "Enter") { - if (search[selectedI]) { - Enter(search[selectedI]); - } - } - }} - /> -
- {#if searchText?.match(/^#[\da-f]{3}([\da-f]{3})?$/)} -
-

Color

-
-
- {/if} - {#if mathRes} -
-

Calculations

-
- > - {mathRes} -
-
- {/if} - {#if eqRes} -
-

Equasion

-
- > - {eqRes.substring(0, eqRes.length - 2)} -
-
- {/if} - {#if dictRes} - {@const data = JSON.parse(dictRes)[0]} -
-

Dictionary ({data.word})

-
- {@html data.definition} -
-
- {/if} - {#if search} -
- {#each search as item, i} -
- {item} -
- {/each} -
- {/if} -
-
+
+ { + if (e.key === "Escape") { + Close(); + } else if (e.key === "ArrowDown") { + selectedI = Math.min( + selectedI + 1, + (search ?? []).length + (remeberSearch ?? []).length, + ); + } else if (e.key === "ArrowUp") { + selectedI = Math.max(selectedI - 1, 0); + } else if (e.key === "Enter") { + if (selectedI < remeberSearch.length) { + EnterDir(remeberSearch[selectedI]); + return; + } + if (search[selectedI - remeberSearch.length]) { + Enter(search[selectedI - remeberSearch.length]); + } + } + }} + /> +
+ {#if remeberSearch.length > 0} +
+

Remebered Paths

+ {#each remeberSearch as item, i} +
+ {item} +
+ {/each} +
+ {/if} + {#if searchText?.match(/^#[\da-f]{3}([\da-f]{3})?$/)} +
+

Color

+
+
+ {/if} + {#if mathRes} +
+

Calculations

+
+ > + {mathRes} +
+
+ {/if} + {#if eqRes} +
+

Equasion

+
+ > + {eqRes.substring(0, eqRes.length - 2)} +
+
+ {/if} + {#if dictRes} + {@const data = JSON.parse(dictRes)[0]} +
+

Dictionary ({data.word})

+
+ {@html data.definition} +
+
+ {/if} + {#if search} +
+ {#each search as item, i} +
+ {item} +
+ {/each} +
+ {/if} +
+
diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index cc7e38a..9df5648 100755 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -5,6 +5,8 @@ export function Close():Promise; export function Enter(arg1:string):Promise; +export function EnterDir(arg1:string):Promise; + export function Search(arg1:string):Promise>; export function SearchDict(arg1:string):Promise; @@ -12,3 +14,5 @@ export function SearchDict(arg1:string):Promise; export function SearchMath(arg1:string):Promise; export function SearchMathEql(arg1:string):Promise; + +export function SearchRemeberDir(arg1:string):Promise>; diff --git a/frontend/wailsjs/go/main/App.js b/frontend/wailsjs/go/main/App.js index ea15b4f..0b3ca82 100755 --- a/frontend/wailsjs/go/main/App.js +++ b/frontend/wailsjs/go/main/App.js @@ -10,6 +10,10 @@ export function Enter(arg1) { return window['go']['main']['App']['Enter'](arg1); } +export function EnterDir(arg1) { + return window['go']['main']['App']['EnterDir'](arg1); +} + export function Search(arg1) { return window['go']['main']['App']['Search'](arg1); } @@ -25,3 +29,7 @@ export function SearchMath(arg1) { export function SearchMathEql(arg1) { return window['go']['main']['App']['SearchMathEql'](arg1); } + +export function SearchRemeberDir(arg1) { + return window['go']['main']['App']['SearchRemeberDir'](arg1); +} diff --git a/main.go b/main.go index 192a284..08b190c 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,11 @@ package main import ( "embed" + "fmt" + "io/ioutil" + "log" + "os" + "strings" "github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2/pkg/options" @@ -13,6 +18,83 @@ import ( var assets embed.FS func main() { + if len(os.Args) > 1 { + if os.Args[1] == "--remeber" { + path, err := os.Getwd() + if err != nil { + log.Fatal("Could not get working directory", err) + return + } + + remeber_path, err := GetRemeberPath() + if err != nil { + log.Fatal("Could not create/open the remeber file") + return + } + + data, err := ioutil.ReadFile(remeber_path) + if err != nil { + log.Fatal("Could not read the remeber file") + return + } + if strings.Contains(fmt.Sprintf("%s\n", path), string(data)) { + log.Fatal("This path is already remebered") + return + } + + f, err := os.OpenFile(remeber_path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + log.Fatal("Could not create/open the remeber file") + return + } + defer f.Close() + + if _, err = f.WriteString(fmt.Sprintf("%s\n", path)); err != nil { + log.Fatal("Could not remeber", err) + } + + fmt.Printf("Remebered: %s\n", path) + } else if os.Args[1] == "--forget" { + path, err := os.Getwd() + if err != nil { + log.Fatal("Could not get working directory", err) + return + } + + remeber_path, err := GetRemeberPath() + if err != nil { + log.Fatal("Could not create/open the remeber file") + return + } + + data, err := ioutil.ReadFile(remeber_path) + if err != nil { + log.Fatal("Could not read the remeber file") + return + } + if !strings.Contains(fmt.Sprintf("%s\n", path), string(data)) { + fmt.Printf("Forgotten*: %s\n", path) + return + } + + base := strings.Split(string(data), "\n") + n_list := []string{} + for _, elm := range base { + if elm != path { + n_list = append(n_list, elm) + } + } + + os.WriteFile(remeber_path, []byte(strings.Join(n_list, "\n")), 0666) + + fmt.Printf("Forgotten: %s\n", path) + } else { + log.Fatal("Unkown option") + return + } + return + } + // Create an instance of the app structure app := NewApp()