Added some more commands and mades some quality of life improvements
All checks were successful
Build / build (push) Successful in 4m27s
All checks were successful
Build / build (push) Successful in 4m27s
This commit is contained in:
parent
3a23118036
commit
3cb5795ca5
17 changed files with 432 additions and 75 deletions
97
app.go
97
app.go
|
|
@ -2,29 +2,56 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
"lmika.dev/pkg/modash/moslice"
|
||||
"ucl.lmika.dev/ucl"
|
||||
"ucl.lmika.dev/ucl/builtins"
|
||||
)
|
||||
|
||||
// App struct
|
||||
type App struct {
|
||||
uclInst *ucl.Inst
|
||||
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
// NewApp creates a new App application struct
|
||||
func NewApp() *App {
|
||||
return &App{}
|
||||
uclInst := ucl.New(
|
||||
ucl.WithModule(builtins.CSV(nil)),
|
||||
ucl.WithModule(builtins.Fns()),
|
||||
ucl.WithModule(builtins.FS(nil)),
|
||||
ucl.WithModule(builtins.Itrs()),
|
||||
ucl.WithModule(builtins.Lists()),
|
||||
ucl.WithModule(builtins.Log(nil)),
|
||||
ucl.WithModule(builtins.OS()),
|
||||
ucl.WithModule(builtins.Strs()),
|
||||
ucl.WithModule(builtins.Time()),
|
||||
)
|
||||
return &App{
|
||||
uclInst: uclInst,
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
a.ctx = context.WithValue(ctx, uclInstKey, a.uclInst)
|
||||
}
|
||||
|
||||
func (a *App) ListProcessors() (resp []ListProcessorsResponse) {
|
||||
for k, v := range TextFilters {
|
||||
resp = append(resp, ListProcessorsResponse{Name: k, Label: v.Label})
|
||||
}
|
||||
sort.Slice(resp, func(i, j int) bool { return resp[i].Label < resp[j].Label })
|
||||
return resp
|
||||
}
|
||||
|
||||
// Greet returns a greeting for the given name
|
||||
func (a *App) ProcessText(req ProcessTextRequest) {
|
||||
filter, ok := TextFilters[req.Action]
|
||||
if !ok {
|
||||
|
|
@ -32,23 +59,53 @@ func (a *App) ProcessText(req ProcessTextRequest) {
|
|||
return
|
||||
}
|
||||
|
||||
resp, err := moslice.MapWithError(req.Input, func(span TextSpan) (TextSpan, error) {
|
||||
outStr, err := filter(span.Text)
|
||||
if err != nil {
|
||||
return TextSpan{}, err
|
||||
switch {
|
||||
case filter.Analyze != nil:
|
||||
inBfr := strings.Builder{}
|
||||
for _, span := range req.Input {
|
||||
if inBfr.Len() > 0 {
|
||||
inBfr.WriteString("\n")
|
||||
}
|
||||
inBfr.WriteString(span.Text)
|
||||
}
|
||||
return TextSpan{
|
||||
Text: outStr,
|
||||
Pos: span.Pos,
|
||||
Len: span.Len,
|
||||
}, nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error running filter: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
runtime.EventsEmit(a.ctx, "process-text-response", ProcessTextResponse{
|
||||
Output: resp,
|
||||
})
|
||||
msg, err := filter.Analyze(a.ctx, inBfr.String())
|
||||
if err != nil {
|
||||
runtime.EventsEmit(a.ctx, "set-statusbar-message", SetStatusbarMessage{
|
||||
Message: fmt.Sprintf("Error running analysis: %v", err.Error()),
|
||||
Error: true,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
runtime.EventsEmit(a.ctx, "set-statusbar-message", SetStatusbarMessage{
|
||||
Message: msg,
|
||||
})
|
||||
case filter.Filter != nil:
|
||||
resp, err := moslice.MapWithError(req.Input, func(span TextSpan) (TextSpan, error) {
|
||||
outRes, err := filter.Filter(a.ctx, span.Text)
|
||||
if err != nil {
|
||||
return TextSpan{}, err
|
||||
}
|
||||
|
||||
return TextSpan{
|
||||
Text: outRes.Output,
|
||||
Pos: span.Pos,
|
||||
Len: span.Len,
|
||||
Append: outRes.Append,
|
||||
}, nil
|
||||
})
|
||||
if err != nil {
|
||||
runtime.EventsEmit(a.ctx, "set-statusbar-message", SetStatusbarMessage{
|
||||
Message: fmt.Sprintf("Error running filter: %v", err.Error()),
|
||||
Error: true,
|
||||
})
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
runtime.EventsEmit(a.ctx, "process-text-response", ProcessTextResponse{
|
||||
Output: resp,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue