diff options
author | Benjamin Chausse <benjamin@chausse.xyz> | 2024-11-24 01:10:15 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-24 01:10:15 -0500 |
commit | d92c9c601e1ef8c183562c94141db493de50921f (patch) | |
tree | 6caffa28f14f92be4711c91887c83db74f4460af /internal | |
parent | f66e718496fdc5c9a9bac1c281ba8e9f3f825791 (diff) |
Remove useless code and implement clipboard functionality (#1)
* Remove dead code
* Implement clipboard
* Improve Stringer color interfaces
Diffstat (limited to 'internal')
-rw-r--r-- | internal/colors/cmyk.go | 2 | ||||
-rw-r--r-- | internal/colors/hsl.go | 2 | ||||
-rw-r--r-- | internal/switcher/keys.go | 20 | ||||
-rw-r--r-- | internal/switcher/switcher.go | 16 | ||||
-rw-r--r-- | internal/util/clipboard.go | 18 | ||||
-rw-r--r-- | internal/util/util.go | 9 |
6 files changed, 53 insertions, 14 deletions
diff --git a/internal/colors/cmyk.go b/internal/colors/cmyk.go index b5ef223..30bc290 100644 --- a/internal/colors/cmyk.go +++ b/internal/colors/cmyk.go @@ -13,7 +13,7 @@ type CMYK struct { } func (c CMYK) String() string { - return fmt.Sprintf("cmyk(%d, %d, %d, %d)", c.C, c.M, c.Y, c.K) + return fmt.Sprintf("cmyk(%d%%, %d%%, %d%%, %d%%)", c.C, c.M, c.Y, c.K) } func (c CMYK) ToPrecise() PreciseColor { diff --git a/internal/colors/hsl.go b/internal/colors/hsl.go index 2a31f81..51a1785 100644 --- a/internal/colors/hsl.go +++ b/internal/colors/hsl.go @@ -12,7 +12,7 @@ type HSL struct { } func (h HSL) String() string { - return fmt.Sprintf("hsl(%d, %d, %d)", h.H, h.S, h.L) + return fmt.Sprintf("hsl(%d, %d%%, %d%%)", h.H, h.S, h.L) } func (h HSL) ToPrecise() PreciseColor { diff --git a/internal/switcher/keys.go b/internal/switcher/keys.go index 94eb6fb..e019ba1 100644 --- a/internal/switcher/keys.go +++ b/internal/switcher/keys.go @@ -3,7 +3,7 @@ package switcher import "github.com/charmbracelet/bubbles/key" type keybinds struct { - next, prev, quit key.Binding + next, prev, cpHex, cpRgb, cpHsl, cpCmyk, quit key.Binding } func newKeybinds() keybinds { @@ -16,6 +16,22 @@ func newKeybinds() keybinds { key.WithKeys("shift+tab"), key.WithHelp("shift+tab", "previous picker"), ), + cpHex: key.NewBinding( + key.WithKeys("x"), + key.WithHelp("x", "yank/copy hex value"), + ), + cpRgb: key.NewBinding( + key.WithKeys("r"), + key.WithHelp("r", "yank/copy RGB value"), + ), + cpHsl: key.NewBinding( + key.WithKeys("s"), + key.WithHelp("s", "yank/copy HSL value"), + ), + cpCmyk: key.NewBinding( + key.WithKeys("c"), + key.WithHelp("c", "yank/copy CMYK value"), + ), quit: key.NewBinding( key.WithKeys("q", "ctrl+c"), key.WithHelp("q", "quit"), @@ -25,7 +41,7 @@ func newKeybinds() keybinds { func Keys() []key.Binding { k := newKeybinds() - return []key.Binding{k.next, k.prev} + return []key.Binding{k.next, k.prev, k.cpHex, k.cpRgb, k.cpHsl, k.cpCmyk, k.quit} } func (m Model) AllKeys() []key.Binding { diff --git a/internal/switcher/switcher.go b/internal/switcher/switcher.go index 0680996..7ff7164 100644 --- a/internal/switcher/switcher.go +++ b/internal/switcher/switcher.go @@ -9,6 +9,7 @@ import ( "github.com/charmbracelet/bubbletea-app-template/internal/picker" "github.com/charmbracelet/bubbletea-app-template/internal/preview" "github.com/charmbracelet/bubbletea-app-template/internal/quit" + "github.com/charmbracelet/bubbletea-app-template/internal/util" ) type Model struct { @@ -74,9 +75,22 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cs := m.pickers[m.active].GetColor() m.Prev() m.pickers[m.active].SetColor(cs) + case key.Matches(msg, keys.cpHex): + util.Copy(colors.Hex(m.pickers[m.active].GetColor())) + case key.Matches(msg, keys.cpRgb): + pc := m.pickers[m.active].GetColor().ToPrecise() + rgb := colors.RGB{}.FromPrecise(pc).(colors.RGB) + util.Copy(rgb.String()) + case key.Matches(msg, keys.cpHsl): + pc := m.pickers[m.active].GetColor().ToPrecise() + hsl := colors.HSL{}.FromPrecise(pc).(colors.HSL) + util.Copy(hsl.String()) + case key.Matches(msg, keys.cpCmyk): + pc := m.pickers[m.active].GetColor().ToPrecise() + cmyk := colors.CMYK{}.FromPrecise(pc).(colors.CMYK) + util.Copy(cmyk.String()) case key.Matches(msg, keys.quit): return quit.Model{}, tea.Quit - // return m, tea.Quit default: // Update the picker newActive, cmd := m.pickers[m.active].Update(msg) diff --git a/internal/util/clipboard.go b/internal/util/clipboard.go new file mode 100644 index 0000000..4aaf828 --- /dev/null +++ b/internal/util/clipboard.go @@ -0,0 +1,18 @@ +package util + +import ( + "log/slog" + + "golang.design/x/clipboard" +) + +// Copies any object that has the Stringer interface to the clipboard +func Copy(str string) { + // Initialize the clipboard + if err := clipboard.Init(); err != nil { + slog.Error("failed to initialize clipboard", "error", err) + return + } + + clipboard.Write(clipboard.FmtText, []byte(str)) +} diff --git a/internal/util/util.go b/internal/util/util.go index 9fea40c..e5b7273 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -1,12 +1,3 @@ package util const ErrKey = "error_message" - -func HexMap() [16]byte { - return [16]byte{ - '0', '1', '2', '3', - '4', '5', '6', '7', - '8', '9', 'a', 'b', - 'c', 'd', 'e', 'f', - } -} |