internal/formatter.go (view raw)
1package bible
2
3import (
4 "fmt"
5 "strings"
6)
7
8// Formatter handles formatting Bible text for display
9type Formatter struct {
10}
11
12// NewFormatter creates a new formatter
13func NewFormatter() *Formatter {
14 return &Formatter{}
15}
16
17// FormatHeader formats the header for a bookmark with ANSI colors
18func (f *Formatter) FormatHeader(bookmark *Bookmark) string {
19 // ANSI color codes
20 const (
21 reset = "\033[0m"
22 green = "\033[32m"
23 yellow = "\033[33m"
24 )
25
26 return fmt.Sprintf("%s%s %d%s\n%s w. %d-%d%s",
27 green,
28 bookmark.Book.String(),
29 bookmark.Chapter,
30 reset,
31 yellow,
32 bookmark.FirstVerse,
33 bookmark.SecondVerse,
34 reset)
35}
36
37// FormatSnippet formats the verse range text
38func (f *Formatter) FormatSnippet(verses []*Verse) string {
39 if len(verses) == 0 {
40 return "Error: No text matched"
41 }
42
43 var sb strings.Builder
44 for i, verse := range verses {
45 if i > 0 {
46 sb.WriteString(" ")
47 }
48 // Remove any leading pilcrow (¶) and trim
49 text := strings.TrimSpace(verse.Text)
50 if strings.HasPrefix(text, "¶ ") {
51 text = text[2:]
52 } else if strings.HasPrefix(text, "¶") {
53 text = text[1:]
54 }
55 sb.WriteString(text)
56 }
57
58 return sb.String()
59}
60
61// ExtractAndFormat extracts verses for a bookmark and formats them
62func (f *Formatter) ExtractAndFormat(bible *Bible, bookmark *Bookmark) string {
63 verses := bible.GetVerseRange(int(bookmark.Book), bookmark.Chapter,
64 bookmark.FirstVerse, bookmark.SecondVerse)
65 return f.FormatSnippet(verses)
66}
67