forked from dnnrly/httpref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
100 lines (86 loc) · 2.83 KB
/
view.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package httpref
import (
"fmt"
"os"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
)
var (
nameStyle, summaryStyle lipgloss.Style
descriptionStyle *glamour.TermRenderer
areStylesPopulated bool
)
type RenderStyle int64
// Summarize creates a block of text that summarizes this reference
func (r Reference) Summarize(style lipgloss.Style) string {
name := nameStyle.Inherit(style).Render(r.Name)
summary := summaryStyle.Inherit(style).Render(r.Summary)
return lipgloss.JoinVertical(lipgloss.Bottom, name, summary)
}
// Describe creates a full, formated description of a reference
func (r Reference) Describe(style lipgloss.Style) string {
name := nameStyle.Inherit(style).Render(r.Name)
summary := summaryStyle.Inherit(style).PaddingLeft(2).Render(r.Summary)
descriptionStyle, err := updateTermRendered(style)
if err != nil {
// hoping this doesn't happen as most commands here suceed without issue
panic(err)
}
description, err := descriptionStyle.Render(r.Description)
return lipgloss.JoinVertical(lipgloss.Bottom, name, summary, description)
}
func init() {
renderStyles()
}
func renderStyles() {
resultStyle := lipgloss.NewStyle()
descriptionForeColorDarkTheme := "120"
descriptionForeColorLightTheme := "202"
// styled using adaptive color to set one of the two colors based on the current theme
nameStyle = resultStyle.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "202", Dark: "86"}).
Bold(true).
Underline(true)
margin := uint(1)
glamour.DarkStyleConfig.Document.Margin = &margin
glamour.LightStyleConfig.Document.Margin = &margin
glamour.DarkStyleConfig.Text.Color = &descriptionForeColorDarkTheme
glamour.LightStyleConfig.Text.Color = &descriptionForeColorLightTheme
// styled using adaptive color to set one of the two colors based on the current theme
summaryStyle = resultStyle.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "20", Dark: "178"})
r, err := updateTermRendered(resultStyle)
if err != nil {
// hoping this doesn't happen as most commands here suceed without issue
panic(err)
}
descriptionStyle = r
}
func updateTermRendered(style lipgloss.Style) (*glamour.TermRenderer, error) {
width := style.GetWidth()
if width == 0 {
width = 80
}
r, err := glamour.NewTermRenderer(
// detect background color and pick either the default dark or light theme
glamour.WithAutoStyle(),
// wrap output at specific width (default is 80)
glamour.WithWordWrap(width),
)
return r, err
}
func PrintResultsWithStyle(results References, rootStyle lipgloss.Style) {
//render := getRendererByStyle(style)
switch len(results) {
case 0:
res := "Filter not found any results\n"
fmt.Fprintf(os.Stderr, res)
os.Exit(1)
case 1:
fmt.Printf("%s\n", results[0].Describe(rootStyle))
default:
for _, r := range results {
fmt.Printf("%s\n", r.Summarize(rootStyle))
}
}
}