diff options
author | Benjamin Chausse <benjamin@chausse.xyz> | 2024-11-23 21:05:11 -0500 |
---|---|---|
committer | Benjamin Chausse <benjamin@chausse.xyz> | 2024-11-23 21:05:11 -0500 |
commit | 4d25e4ece0b72d240bb2565f8abb7389e650990a (patch) | |
tree | 55af982b45d9ed576871c6f3ccf5f800cddc9b56 /internal/preview/preview.go | |
parent | b42ab480dd4c4eec83d79bba9400232ddb79f6b1 (diff) |
Preview + Unit-tests for color conversions
Diffstat (limited to 'internal/preview/preview.go')
-rw-r--r-- | internal/preview/preview.go | 42 |
1 files changed, 42 insertions, 0 deletions
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) +} |