railgun/app.go
2025-05-13 08:05:47 +01:00

125 lines
2.2 KiB
Go

package main
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
inputList []string
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{
inputList: []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
}
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
}
// 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)
}