summaryrefslogtreecommitdiff
path: root/internal/switcher/misc.go
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2024-11-25 21:44:11 -0500
committerGitHub <noreply@github.com>2024-11-25 21:44:11 -0500
commitde7c6e9ab8bb4ab3af4557800cf92dd5ebddd2f8 (patch)
treee889022472615bf6ab06df37469eb3bffba8f4a3 /internal/switcher/misc.go
parent44456ebb15be6f2d9e981fd4093c48fc736d219a (diff)
feat: Interactive input (#16)
* Refactor copy keybinds into single help entry * Manually input color values at runtime * Showcase input instead of clipboard in demo * more vhs updates
Diffstat (limited to 'internal/switcher/misc.go')
-rw-r--r--internal/switcher/misc.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/internal/switcher/misc.go b/internal/switcher/misc.go
new file mode 100644
index 0000000..e87f219
--- /dev/null
+++ b/internal/switcher/misc.go
@@ -0,0 +1,54 @@
+package switcher
+
+import (
+ "log/slog"
+
+ "github.com/ChausseBenjamin/termpicker/internal/colors"
+ "github.com/ChausseBenjamin/termpicker/internal/parse"
+ "github.com/ChausseBenjamin/termpicker/internal/util"
+)
+
+const (
+ okCpMsg = "Copied %s to clipboard as %s"
+)
+
+func (m Model) copyColor(format string) string {
+ pc := m.pickers[m.active].GetColor().ToPrecise()
+ switch format {
+ case cpHex:
+ return util.Copy(colors.Hex(m.pickers[m.active].GetColor()))
+ case cpRGB:
+ rgb := colors.RGB{}.FromPrecise(pc).(colors.RGB)
+ return util.Copy(rgb.String())
+ case cpHSL:
+ hsl := colors.HSL{}.FromPrecise(pc).(colors.HSL)
+ return util.Copy(hsl.String())
+ case cpCMYK:
+ cmyk := colors.CMYK{}.FromPrecise(pc).(colors.CMYK)
+ return util.Copy(cmyk.String())
+ default:
+ return "Copy format not supported"
+ }
+}
+
+func (m *Model) SetColorFromText(colorStr string) string {
+ color, err := parse.Color(colorStr)
+ if err != nil {
+ slog.Error("Failed to parse color", util.ErrKey, err)
+ return err.Error()
+ } else {
+ pc := color.ToPrecise()
+ switch color.(type) {
+ case colors.RGB:
+ m.UpdatePicker(IndexRgb, pc)
+ m.SetActive(IndexRgb)
+ case colors.CMYK:
+ m.UpdatePicker(IndexCmyk, pc)
+ m.SetActive(IndexCmyk)
+ case colors.HSL:
+ m.UpdatePicker(IndexHsl, pc)
+ m.SetActive(IndexHsl)
+ }
+ return "Color set to " + colorStr
+ }
+}