generated from dnnrly/goclitem
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
view.go
155 lines (131 loc) · 4.47 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package httpref
import (
"fmt"
"os"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
)
var (
nameStyle, summaryStyle lipgloss.Style
descriptionStyle *glamour.TermRenderer
descriptionBorderStyle lipgloss.Style
)
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)
statusBar := renderStatusBar(r.Name, r.Summary)
return lipgloss.JoinVertical(lipgloss.Bottom, name, summary, statusBar)
}
// Describe creates a full, formatted description of a reference
func (r Reference) Describe(style lipgloss.Style) (string, error) {
defer func() {
if rec := recover(); rec != nil {
fmt.Fprintf(os.Stderr, "An unexpected error occurred: %v\nPlease raise an issue on GitHub: https://github.com/Erfanm83/httpref/issues/new\n", rec)
}
}()
statusBar := renderStatusBar(r.Name, r.Summary)
descriptionStyle, err := updateTermRendered(style)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to update term renderer: %v\n", err)
return "", err
}
description, err := descriptionStyle.Render(r.Description)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to render description: %v\n", err)
return "", err
}
descriptionWithBorder := descriptionBorderStyle.Render(description)
return lipgloss.JoinVertical(lipgloss.Bottom, statusBar, descriptionWithBorder), nil
}
func init() {
err := renderStyles()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize render styles: %v\nPlease raise an issue on GitHub: https://github.com/dnnrly/httpref/issues/new \n", err)
os.Exit(1)
}
}
func renderStyles() error {
defer func() {
if rec := recover(); rec != nil {
fmt.Fprintf(os.Stderr, "An unexpected error occurred during renderStyles: %v\nPlease raise an issue on GitHub: https://github.com/dnnrly/httpref/issues/new \n", rec)
}
}()
resultStyle := lipgloss.NewStyle()
descriptionForeColorDarkTheme := "120"
descriptionForeColorLightTheme := "202"
descriptionBorderStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.AdaptiveColor{Light: "#04B575", Dark: "5"}).
Padding(0, 1, 0, 1)
margin := uint(1)
glamour.DarkStyleConfig.Document.Margin = &margin
glamour.LightStyleConfig.Document.Margin = &margin
glamour.DarkStyleConfig.Text.Color = &descriptionForeColorDarkTheme
glamour.LightStyleConfig.Text.Color = &descriptionForeColorLightTheme
r, err := updateTermRendered(resultStyle)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to update term renderer in renderStyles: %v\n", err)
return err
}
descriptionStyle = r
return nil
}
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) {
defer func() {
if rec := recover(); rec != nil {
fmt.Fprintf(os.Stderr, "An unexpected error occurred during PrintResultsWithStyle: %v\nPlease raise an issue on GitHub: https://github.com/Erfanm83/httpref/issues/new\n", rec)
os.Exit(1)
}
}()
if len(results) == 0 {
res := "Filter not found any results\n"
fmt.Fprintf(os.Stderr, res)
os.Exit(1)
}
if len(results) == 1 {
description, err := results[0].Describe(rootStyle)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to describe reference: %v\n", err)
os.Exit(1)
}
fmt.Printf("%s\n", description)
return
}
for _, r := range results {
fmt.Printf("%s\n", r.Summarize(rootStyle))
}
}
func renderStatusBar(name, summary string) string {
// styled using adaptive color to set one of the two colors based on the current theme
statusStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("#FF5F87")).
Background(lipgloss.Color("#353533")).
Bold(true).
Padding(0, 1)
statusText := lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "20", Dark: "178"}).
Background(lipgloss.AdaptiveColor{Light: "#D9DCCF", Dark: "#353533"}).
Padding(0, 2)
statusKey := statusStyle.Render(name)
statusVal := statusText.Render(summary)
bar := lipgloss.JoinHorizontal(lipgloss.Top,
statusKey,
statusVal,
)
return statusStyle.Width(101).Render(bar)
}