package main import ( "context" "fmt" "io/ioutil" "os" "os/exec" "regexp" "strings" "github.com/wailsapp/wails/v2/pkg/runtime" ) // App struct 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{}, remeberedPath: []string{}, } } // startup is called when the app starts. The context is saved // 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() { runtime.Quit(a.ctx) } func (a *App) SearchMath(value string) *string { if strings.ContainsAny(value, "0123456789") { out, err := exec.Command("numbat", "-e", value).Output() if err == nil { res := string(out[:]) return &res } } return nil } func (a *App) SearchDict(value string) *string { if strings.Contains(value, " ") { return nil } out, err := exec.Command("sdcv", "-jne", value).Output() if err == nil { res := string(out[:]) return &res } return nil } func (a *App) SearchMathEql(value string) *string { if !strings.Contains(value, "=") { return nil } cmd := exec.Command("maxima", "--q", "--very-quiet") stdin, err := cmd.StdinPipe() if err != nil { return nil } stdin.Write(fmt.Appendf([]byte{}, "solve([ %s ]);\n", value)) stdin.Close() out, err := cmd.Output() if err == nil { res := string(out[:]) if strings.Contains(res, "solve:") { return nil } return &res } 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 { // Perform search paths := strings.Split(os.Getenv("PATH"), ":") for _, elm := range paths { entries, err := os.ReadDir(elm) if err != nil { continue } for _, e := range entries { stat, err := os.Stat(fmt.Sprintf("%s/%s", elm, e.Name())) if err != nil { continue } // For now do not recurse if stat.IsDir() { continue } a.inputList = append(a.inputList, e.Name()) } } } n_list := []string{} for _, elm := range a.inputList { if strings.HasPrefix(elm, value) { n_list = append(n_list, elm) } } return n_list } 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) } func (a *App) EnterEditor(value string) { fmt.Printf("czed %s", value) runtime.Quit(a.ctx) }