-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile_view.go
187 lines (158 loc) · 4.43 KB
/
profile_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"fmt"
"os"
"os/user"
"strings"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type ViewConfig struct {
filePath string
profileName string
entries int
lastMod time.Time
}
type Viewer struct {
app *tview.Application
config ViewConfig
textView *tview.TextView
header *tview.TextView
status *tview.TextView
}
func NewViewer(config ViewConfig) *Viewer {
app := tview.NewApplication()
viewer := &Viewer{
app: app,
config: config,
textView: tview.NewTextView(),
header: tview.NewTextView(),
status: tview.NewTextView(),
}
viewer.textView.SetDynamicColors(true)
viewer.textView.SetRegions(true)
viewer.textView.SetScrollable(true)
viewer.textView.SetWrap(false)
viewer.header.SetTextAlign(tview.AlignLeft)
viewer.header.SetDynamicColors(true)
viewer.header.SetBackgroundColor(tcell.ColorDefault)
viewer.status.SetTextAlign(tview.AlignCenter)
viewer.status.SetDynamicColors(true)
viewer.status.SetBackgroundColor(tcell.ColorDefault)
return viewer
}
func (v *Viewer) layout() *tview.Flex {
flex := tview.NewFlex().SetDirection(tview.FlexRow)
flex.AddItem(v.header, 2, 0, false)
flex.AddItem(v.textView, 0, 1, true)
flex.AddItem(v.status, 1, 0, false)
return flex
}
func (v *Viewer) updateHeader() {
headerText := fmt.Sprintf(
"[yellow]Profile:[white] %s [yellow]Entries:[white] %d [yellow]Last Modified:[white] %s",
v.config.profileName,
v.config.entries,
v.config.lastMod.Format("2006-01-02 15:04:05"),
)
v.header.SetText(headerText)
}
func (v *Viewer) updateStatus() {
statusText := "[yellow]↑↓:[white] Scroll [yellow]PgUp/PgDn:[white] Page Scroll [yellow]q:[white] Quit"
v.status.SetText(statusText)
}
func (v *Viewer) highlightContent(content string) string {
var highlighted strings.Builder
lines := strings.Split(content, "\n")
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "#") {
highlighted.WriteString("[green]" + line + "[white]\n")
} else if strings.Contains(line, "=") {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
highlighted.WriteString(fmt.Sprintf("[yellow]%s[white]=%s\n", parts[0], parts[1]))
} else {
highlighted.WriteString(line + "\n")
}
} else {
highlighted.WriteString(line + "\n")
}
}
return highlighted.String()
}
func (v *Viewer) Run() error {
content, err := os.ReadFile(v.config.filePath)
if err != nil {
return fmt.Errorf("failed to read profile: %v", err)
}
lines := strings.Split(string(content), "\n")
entryCount := 0
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if trimmed != "" && !strings.HasPrefix(trimmed, "#") {
entryCount++
}
}
v.config.entries = entryCount
fileInfo, err := os.Stat(v.config.filePath)
if err == nil {
v.config.lastMod = fileInfo.ModTime()
}
v.updateHeader()
v.updateStatus()
v.textView.SetText(v.highlightContent(string(content)))
v.app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCtrlC, tcell.KeyRune:
if event.Rune() == 'q' {
v.app.Stop()
return nil
}
case tcell.KeyPgUp:
row, _ := v.textView.GetScrollOffset()
v.textView.ScrollTo(row-10, 0)
return nil
case tcell.KeyPgDn:
row, _ := v.textView.GetScrollOffset()
v.textView.ScrollTo(row+10, 0)
return nil
}
return event
})
if err := v.app.SetRoot(v.layout(), true).Run(); err != nil {
return fmt.Errorf("failed to start viewer: %v", err)
}
return nil
}
func ViewProfile(name string) error {
currentUser, err := user.Current()
if err != nil {
return fmt.Errorf("failed to get current user: %v", err)
}
configFilePath := fmt.Sprintf("/home/%s/.config/%s/%s", currentUser.Username, ProjectName, configFileName)
configContent, err := os.ReadFile(configFilePath)
if err != nil {
return fmt.Errorf("failed to read config file: %v", err)
}
var profileDir string
for _, line := range strings.Split(string(configContent), "\n") {
if strings.HasPrefix(line, "PROFILE_DIR=") {
profileDir = strings.TrimPrefix(line, "PROFILE_DIR=")
profileDir = strings.Split(profileDir, "#")[0]
profileDir = strings.TrimSpace(profileDir)
break
}
}
if profileDir == "" {
return fmt.Errorf("PROFILE_DIR not found in config")
}
profilePath := fmt.Sprintf("%s/%s.env", profileDir, name)
config := ViewConfig{
filePath: profilePath,
profileName: name,
}
viewer := NewViewer(config)
return viewer.Run()
}