summaryrefslogtreecommitdiff
path: root/internal/ascii/render.go
blob: eb41dd1619b0c143047212c140f480c4ce1c4629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Package ascii provides ASCII art rendering for timer display.
// This code is adapted from https://github.com/Bahaaio/pomo
// Copyright (c) 2025 Bahaa El Deen Mohamed
// Licensed under the MIT License
package ascii

import (
	"github.com/charmbracelet/lipgloss"
)

// RenderNumber renders a string of digits as ASCII art using the specified font.
// The number parameter should contain only digits (0-9) and colons (:).
// Returns the ASCII art representation as a single string with properly aligned characters.
func RenderNumber(number string, font Font) string {
	digits := make([]string, 0, len(number))

	for _, digit := range number {
		digits = append(digits, renderDigit(digit, font))
	}

	asciiDigits := lipgloss.JoinHorizontal(lipgloss.Top, digits...)
	return asciiDigits
}

// GetFont returns the font with the given name.
// If the font doesn't exist, it returns the default font.
func GetFont(fontName string) Font {
	if font, exists := fonts[fontName]; exists {
		return font
	}

	return fonts[DefaultFont]
}

// renderDigit converts a single digit character to its ASCII art representation.
// Supports digits 0-9 and colon (:). Returns empty string for unsupported characters.
func renderDigit(digit rune, font Font) string {
	if digit == ':' {
		return font[len(font)-1]
	}

	if digit < '0' || digit > '9' {
		return ""
	}

	return font[digit-'0']
}