summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2024-11-24 02:59:00 -0500
committerBenjamin Chausse <benjamin@chausse.xyz>2024-11-24 02:59:00 -0500
commitac024fff3ce70b435a39de83f7aa3339668fdc6b (patch)
tree2e68d5d6c3517770fc8bae1da3e70f53a241e7e3
parent56ceae9524f40ac89cd68796d1f8e6cd0560c80a (diff)
Instant slider animation
-rw-r--r--.gitignore1
-rw-r--r--cmd/termpicker/flags.go13
-rw-r--r--cmd/termpicker/main.go10
-rw-r--r--internal/slider/slider.go18
-rw-r--r--internal/switcher/switcher.go4
5 files changed, 42 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index 8d7bab4..f5d3659 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
+*.json
coverage.txt
dist/
diff --git a/cmd/termpicker/flags.go b/cmd/termpicker/flags.go
index f3cfd53..660c053 100644
--- a/cmd/termpicker/flags.go
+++ b/cmd/termpicker/flags.go
@@ -2,4 +2,15 @@ package main
import "github.com/urfave/cli/v2"
-var AppFlags []cli.Flag = []cli.Flag{}
+const (
+ flagLogfile = "logfile"
+)
+
+var AppFlags []cli.Flag = []cli.Flag{
+ &cli.StringFlag{
+ Name: flagLogfile,
+ Aliases: []string{"l"},
+ Usage: "Log file",
+ Value: "/dev/null", // Don't log by default
+ },
+}
diff --git a/cmd/termpicker/main.go b/cmd/termpicker/main.go
index 0ebbadb..ef9f87f 100644
--- a/cmd/termpicker/main.go
+++ b/cmd/termpicker/main.go
@@ -14,6 +14,16 @@ import (
)
func AppAction(ctx *cli.Context) error {
+ logFile, err := os.Create(ctx.String(flagLogfile))
+ if err != nil {
+ slog.Error("Failed to create log file", util.ErrKey, err.Error())
+ os.Exit(1)
+ }
+ defer logFile.Close()
+
+ handler := slog.NewJSONHandler(logFile, nil)
+ slog.SetDefault(slog.New(handler))
+
slog.Info("Starting Termpicker")
// RGB {{{
r := slider.New('R', 255, progress.WithGradient("#660000", "#ff0000"))
diff --git a/internal/slider/slider.go b/internal/slider/slider.go
index 884d141..bfbc069 100644
--- a/internal/slider/slider.go
+++ b/internal/slider/slider.go
@@ -35,11 +35,20 @@ func New(label byte, maxVal int, opts ...progress.Option) Model {
func (m Model) Title() string { return fmt.Sprintf("%c", m.label) }
func (m Model) Init() tea.Cmd {
- return m.progress.Init()
+ // Triggering a frame message Update here will force the progress bar to
+ // render immediately. This is necessary because progress bars only render
+ // when their state changes. Without this, there is a disrepancy between
+ // the initial state of the progress bar and the initial state of the slider.
+
+ // There's no sugar-coating it: This is a hack. But it works...
+ _, cmd := m.Update(progress.FrameMsg{})
+ return cmd
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
+ cmds := []tea.Cmd{}
keys := newKeybinds()
+
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
@@ -54,12 +63,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
return m, m.progress.SetPercent(m.Pcnt())
case progress.FrameMsg:
+ if m.progress.Percent() != m.Pcnt() {
+ cmds = append(cmds, m.progress.SetPercent(m.Pcnt()))
+ }
progressModel, cmd := m.progress.Update(msg)
m.progress = progressModel.(progress.Model)
- return m, cmd
+ cmds = append(cmds, cmd)
default:
- return m, nil
}
+ return m, tea.Batch(cmds...)
}
func (m Model) ViewValue(current int) string {
diff --git a/internal/switcher/switcher.go b/internal/switcher/switcher.go
index eee92df..b534e54 100644
--- a/internal/switcher/switcher.go
+++ b/internal/switcher/switcher.go
@@ -2,6 +2,7 @@ package switcher
import (
"fmt"
+ "log/slog"
"github.com/ChausseBenjamin/termpicker/internal/colors"
"github.com/ChausseBenjamin/termpicker/internal/picker"
@@ -64,6 +65,7 @@ func (m Model) View() string {
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
keys := newKeybinds()
cmds := []tea.Cmd{}
+ slog.Info("Received tea.Msg", "tea_msg", msg, "type", fmt.Sprintf("%T", msg))
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
@@ -102,6 +104,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}
+ default:
+ // fmt.Printf("\nmsg: %T\n", msg)
}
for i, p := range m.pickers {
newActive, cmd := p.Update(msg)