-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (107 loc) · 2.41 KB
/
main.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
package main
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"os"
)
type model struct {
err error
countReady bool
data *filesData
counters lineCounters
verbose bool
}
type parameters struct {
profs profiles
args *userArgs
}
func (m model) Init() tea.Cmd {
return func() tea.Msg {
p := parameters{}
// Reading embedded config
conf, err := embeddedConfig()
if err != nil {
return fmt.Errorf("embedded config: %w", err)
}
// Reading user input
isExecuting := false
p.args, isExecuting, err = userInput()
if !isExecuting {
return tea.Quit()
}
if err != nil {
return fmt.Errorf("user input: %w", err)
}
// Reading user config
if p.args.configFilename != "" {
conf, err = userConfig(p.args.configFilename)
if err != nil {
return fmt.Errorf("user config: %w", err)
}
}
//log.Println(conf)
// Filter profiles
p.profs, err = filterProfiles(conf.Profiles, p.args.profileNames)
if err != nil {
return fmt.Errorf("getting profiles: %w", err)
}
return p
}
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
//log.Println(reflect.TypeOf(msg), msg)
switch msg := msg.(type) {
case error:
m.err = msg
return m, tea.Quit
case parameters:
var err error
// Processing files
m.data, err = processPatterns(msg.args.patterns, msg.args.recursive, msg.args.dotFiles)
if err != nil {
m.err = fmt.Errorf("processing files: %w", err)
return m, tea.Quit
}
// Counting lines
m.counters, err = countLinesInFiles(m.data.filePaths, msg.profs)
if err != nil {
m.err = fmt.Errorf("counting lines: %w", err)
return m, tea.Quit
}
m.verbose = msg.args.verbose
m.countReady = true
return m, tea.Quit
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc":
return m, tea.Quit
}
}
return m, nil
}
func (m model) View() string {
if m.err != nil {
return fmt.Sprintf("Something went wrong: %s\n", m.err)
}
if m.countReady {
// Displaying output
return displayCounts(m.data.usedFiles, m.data.skippedFiles, m.counters, m.verbose)
}
return ""
}
func main() {
// Logging setup
//f, err := tea.LogToFile("debug.log", "debug")
//if err != nil {
// fmt.Println("Fatal:", err)
// os.Exit(1)
//}
//_ = f.Truncate(0)
// Program
if err := tea.NewProgram(&model{}).Start(); err != nil {
fmt.Printf("Opss, something went really wrong: %v", err)
os.Exit(1)
}
// Closing log file
//_ = f.Close()
}