diff options
Diffstat (limited to 'internal/app')
-rw-r--r-- | internal/app/app.go | 67 | ||||
-rw-r--r-- | internal/app/flags.go | 21 | ||||
-rw-r--r-- | internal/app/keybindings.md | 24 |
3 files changed, 112 insertions, 0 deletions
diff --git a/internal/app/app.go b/internal/app/app.go new file mode 100644 index 0000000..b17f32a --- /dev/null +++ b/internal/app/app.go @@ -0,0 +1,67 @@ +package app + +import ( + "context" + _ "embed" + "fmt" + "io" + "log/slog" + + "github.com/ChausseBenjamin/termpicker/internal/logging" + "github.com/ChausseBenjamin/termpicker/internal/switcher" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/glamour" + docs "github.com/urfave/cli-docs/v3" + "github.com/urfave/cli/v3" +) + +//go:embed keybindings.md +var KeybindingDocs string + +// Set by the build system +var version = "compiled" + +func AppAction(ctx context.Context, cmd *cli.Command) error { + logfile := logging.Setup(cmd.String("logfile")) + defer logfile.Close() + + slog.Info("Starting Termpicker") + + sw := switcher.New() + + if colorStr := cmd.String("color"); colorStr != "" { + sw.NewNotice(sw.SetColorFromText(colorStr)) + } + + p := tea.NewProgram(sw) + if _, err := p.Run(); err != nil { + return err + } + return nil +} + +func Command() *cli.Command { + cmd := &cli.Command{ + Name: "termpicker", + Usage: "A terminal-based color picker", + Action: AppAction, + Authors: []any{"Benjamin Chausse <benjamin@chausse.xyz>"}, + Version: version, + Flags: AppFlags, + EnableShellCompletion: true, + } + + cli.HelpPrinter = func(w io.Writer, _ string, _ any) { + docs.MarkdownDocTemplate = fmt.Sprintf("%s\n\n\n%s", + docs.MarkdownDocTemplate, + KeybindingDocs, + ) + + helpRaw, _ := docs.ToMarkdown(cmd) + helpCute, _ := glamour.Render(helpRaw, "dark") + + w.Write([]byte(helpCute)) + } + + return cmd +} diff --git a/internal/app/flags.go b/internal/app/flags.go new file mode 100644 index 0000000..fae403b --- /dev/null +++ b/internal/app/flags.go @@ -0,0 +1,21 @@ +package app + +import "github.com/urfave/cli/v3" + +const ( + flagLogfile = "logfile" +) + +var AppFlags []cli.Flag = []cli.Flag{ + &cli.StringFlag{ + Name: flagLogfile, + Aliases: []string{"l"}, + Usage: "Log file", + }, + &cli.StringFlag{ + Name: "color", + Aliases: []string{"c"}, + Usage: "Initial color", + Value: "", + }, +} diff --git a/internal/app/keybindings.md b/internal/app/keybindings.md new file mode 100644 index 0000000..03f8201 --- /dev/null +++ b/internal/app/keybindings.md @@ -0,0 +1,24 @@ +# KEYBINDINGS + +**Normal mode**: + +- `h`,`l`: coarse decrease/increase the current slider by 5% +- `H`,`L`: fine decrease/increase the current slider by 1 +- `j`,`k`: select the slider below/above +- `<Tab>`,`<S-Tab>`: move to the next/previous tab +- `f`,`b`: copy the color as an ANSI escape code for the foreground/background +- `x`,`r`,`s`,`c`: copy the color as a hex/rgb/hsl/cmyk +- `?`: expand/shrink the help menu +- `i`,`<cmd>`: enter Insert mode +- `q`/`<C-c>`: quit the application + +**Insert mode**: + +Manually type a color. Pressing <Esc> will cancel/leave insert mode. +Anything in the following formats will be used as a color input when +pressing enter: + +- Hex values: `#rrggbb` +- RGB values: `rgb( r, g, b)` +- CMYK values: `cmyk(c, m, y, k)` +- HSL values: `hsl(h, s, l)` |