gitstack

grindlemire/go-tui code browser

4.4 KB Go 219 lines 2026-06-03 · d15bb9f raw
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"sync"

	"github.com/grindlemire/go-tui/internal/formatter"
)

// runFmt implements the fmt subcommand.
// It formats .gsx files in place or checks formatting.
func runFmt(args []string) error {
	var (
		stdout bool // print to stdout instead of modifying file
		check  bool // check mode (exit 1 if not formatted)
		paths  []string
	)

	// Parse arguments
	for _, arg := range args {
		switch arg {
		case "--stdout", "-stdout":
			stdout = true
		case "--check", "-check":
			check = true
		default:
			paths = append(paths, arg)
		}
	}

	// Default to current directory if no paths specified
	if len(paths) == 0 {
		paths = []string{"."}
	}

	// Collect all .gsx files
	files, err := collectGsxFiles(paths)
	if err != nil {
		return err
	}

	if len(files) == 0 {
		return fmt.Errorf("no .gsx files found")
	}

	// Process files
	fmtr := formatter.New()

	if check {
		return runFmtCheck(fmtr, files)
	}

	if stdout {
		return runFmtStdout(fmtr, files)
	}

	return runFmtInPlace(fmtr, files)
}

// runFmtInPlace formats files in place, modifying them on disk.
func runFmtInPlace(fmtr *formatter.Formatter, files []string) error {
	type result struct {
		path    string
		changed bool
		err     error
	}

	results := make(chan result, len(files))
	var wg sync.WaitGroup

	// Process files in parallel
	for _, path := range files {
		wg.Add(1)
		go func(p string) {
			defer wg.Done()

			source, err := os.ReadFile(p)
			if err != nil {
				results <- result{path: p, err: fmt.Errorf("reading file: %w", err)}
				return
			}

			res, err := fmtr.FormatWithResult(filepath.Base(p), string(source))
			if err != nil {
				results <- result{path: p, err: err}
				return
			}

			if res.Changed {
				if err := os.WriteFile(p, []byte(res.Content), 0o644); err != nil {
					results <- result{path: p, err: fmt.Errorf("writing file: %w", err)}
					return
				}
			}

			results <- result{path: p, changed: res.Changed}
		}(path)
	}

	// Wait for all goroutines and close results channel
	go func() {
		wg.Wait()
		close(results)
	}()

	// Collect results
	var errorCount int
	for res := range results {
		if res.err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", res.path, res.err)
			errorCount++
		} else if res.changed {
			fmt.Printf("Formatted: %s\n", res.path)
		}
	}

	if errorCount > 0 {
		return fmt.Errorf("%d file(s) had errors", errorCount)
	}

	return nil
}

// runFmtStdout formats files and prints to stdout.
func runFmtStdout(fmtr *formatter.Formatter, files []string) error {
	var errorCount int

	for _, path := range files {
		source, err := os.ReadFile(path)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: reading file: %v\n", path, err)
			errorCount++
			continue
		}

		formatted, err := fmtr.Format(filepath.Base(path), string(source))
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", path, err)
			errorCount++
			continue
		}

		if len(files) > 1 {
			fmt.Printf("// %s\n", path)
		}
		fmt.Print(formatted)
	}

	if errorCount > 0 {
		return fmt.Errorf("%d file(s) had errors", errorCount)
	}

	return nil
}

// runFmtCheck checks if files are formatted without modifying them.
// Returns an error if any file is not formatted.
func runFmtCheck(fmtr *formatter.Formatter, files []string) error {
	type result struct {
		path         string
		notFormatted bool
		err          error
	}

	results := make(chan result, len(files))
	var wg sync.WaitGroup

	// Process files in parallel
	for _, path := range files {
		wg.Add(1)
		go func(p string) {
			defer wg.Done()

			source, err := os.ReadFile(p)
			if err != nil {
				results <- result{path: p, err: fmt.Errorf("reading file: %w", err)}
				return
			}

			res, err := fmtr.FormatWithResult(filepath.Base(p), string(source))
			if err != nil {
				results <- result{path: p, err: err}
				return
			}

			results <- result{path: p, notFormatted: res.Changed}
		}(path)
	}

	// Wait for all goroutines and close results channel
	go func() {
		wg.Wait()
		close(results)
	}()

	// Collect results
	var errorCount, notFormattedCount int
	for res := range results {
		if res.err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", res.path, res.err)
			errorCount++
		} else if res.notFormatted {
			fmt.Fprintf(os.Stderr, "ERROR: %s is not formatted\n", res.path)
			notFormattedCount++
		}
	}

	if errorCount > 0 {
		return fmt.Errorf("%d file(s) had errors", errorCount)
	}

	if notFormattedCount > 0 {
		return fmt.Errorf("%d file(s) not formatted", notFormattedCount)
	}

	return nil
}