From 4d25e4ece0b72d240bb2565f8abb7389e650990a Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Sat, 23 Nov 2024 21:05:11 -0500 Subject: Preview + Unit-tests for color conversions --- internal/preview/preview.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 internal/preview/preview.go (limited to 'internal/preview') diff --git a/internal/preview/preview.go b/internal/preview/preview.go new file mode 100644 index 0000000..ef4c9f0 --- /dev/null +++ b/internal/preview/preview.go @@ -0,0 +1,42 @@ +package preview + +import ( + "strings" + + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" +) + +const runeBlock = "█" + +type Model struct { + size int // height of the square in rows + hex string +} + +func (m *Model) Color(hex string) { + m.hex = hex +} + +func New(hex string) *Model { + return &Model{ + size: 5, + hex: hex, + } +} + +func (m Model) Init() tea.Cmd { + return nil +} + +func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + return m, nil +} + +func (m Model) View() string { + style := lipgloss.NewStyle().Foreground(lipgloss.Color(m.hex)) + // size is doubled since terminal cells are 2:1 (h:w) + oneRow := strings.Repeat(runeBlock, m.size*2) + block := strings.Repeat(oneRow+"\n", m.size) + return style.Render(block) +} -- cgit v1.2.3