-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
841 lines (783 loc) · 25.7 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
package main
import (
"bytes"
"cmp"
"context"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"time"
modulev1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/module/v1"
"github.com/alecthomas/chroma/v2/formatters"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"github.com/bufbuild/httplb"
"github.com/bufbuild/protovalidate-go"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/cli/browser"
"github.com/jdx/go-netrc"
"github.com/peterbourgon/ff/v4"
"github.com/peterbourgon/ff/v4/ffhelp"
)
func main() {
if err := run(context.Background()); err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
}
}
func run(_ context.Context) error {
fs := ff.NewFlagSet("buftui")
var (
// `-r` is for reference, which should generally be preferred.
remoteFlag = fs.StringLong("remote", "buf.build", "BSR remote")
fullscreenFlag = fs.Bool('f', "fullscreen", "Enable fullscreen display")
usernameFlag = fs.String('u', "username", "", "Set username for authentication (default: login for remote in ~/.netrc)")
tokenFlag = fs.String('t', "token", "", "Set token for authentication (default: password for remote in ~/.netrc)")
referenceFlag = fs.String('r', "reference", "", "Set BSR reference to open")
)
if err := ff.Parse(fs, os.Args[1:]); err != nil {
fmt.Printf("%s\n", ffhelp.Flags(fs))
return err
}
username := *usernameFlag
token := *tokenFlag
parsedRemote, parsedReference, err := parseReference(*referenceFlag)
if err != nil {
return fmt.Errorf("parsing reference flag: %w", err)
}
if parsedRemote != "" && *remoteFlag != "" && *remoteFlag != parsedRemote {
return fmt.Errorf("cannot provide conflicting `--remote` flag (%s) and reference remote (%s)", *remoteFlag, parsedRemote)
}
// We know the remotes at least aren't conflicting, so take whichever is non-empty.
remote := cmp.Or(*remoteFlag, parsedRemote)
// Sanity check for `--remote ""`, or an invalid parsed reference.
if remote == "" {
return fmt.Errorf("remote cannot be empty")
}
// Either username && token should be provided at the CLI, or they should be loaded from the ~/.netrc.
if (username == "" && token != "") || (username != "" && token == "") {
return fmt.Errorf("must set both username and token flags, or neither (and load authentication from ~/.netrc)")
}
if username == "" && token == "" {
var err error
username, token, err = getUserTokenFromNetrc(remote)
if err != nil {
return fmt.Errorf("getting netrc credentials for remote %q: %w", remote, err)
}
}
if username == "" {
return fmt.Errorf("username must be set, either by flag or in the ~/.netrc file")
}
if token == "" {
return fmt.Errorf("token must be set, either by flag or in the ~/.netrc file")
}
listStyles := list.DefaultStyles()
listStyles.Title = listStyles.Title.Foreground(bufBlue).Background(bufTeal).Bold(true)
listItemStyles := list.NewDefaultItemStyles()
listItemStyles.SelectedTitle = listItemStyles.SelectedTitle.Foreground(bufBlue).BorderLeftForeground(bufBlue).Bold(true)
listItemStyles.SelectedDesc = listItemStyles.SelectedDesc.Foreground(bufBlue).BorderLeftForeground(bufBlue)
listItemStyles.NormalTitle = listItemStyles.NormalTitle.Foreground(bufBlue)
httpClient := httplb.NewClient()
defer httpClient.Close()
initialState := modelStateSearching
if parsedReference != nil {
initialState = modelStateLoadingReference
}
defaultWidth, defaultHeight := 100, 20
var moduleList list.Model
{
delegate := list.NewDefaultDelegate()
delegate.Styles = listItemStyles
// TODO: Show module description.
delegate.ShowDescription = false
delegate.SetSpacing(0)
moduleList = list.New(
nil,
delegate,
defaultWidth, // TODO: Figure out the width of the terminal?
defaultHeight,
)
}
var commitList list.Model
{
delegate := list.NewDefaultDelegate()
delegate.Styles = listItemStyles
commitList = list.New(
nil,
delegate,
defaultWidth, // TODO: Figure out the width of the terminal?
defaultHeight,
)
}
var commitFilesList list.Model
{
delegate := list.NewDefaultDelegate()
delegate.Styles = listItemStyles
delegate.ShowDescription = false
delegate.SetSpacing(0)
commitFilesList = list.New(
nil,
delegate,
defaultWidth,
defaultHeight, // TODO: Pick a reasonable value here.
)
}
model := model{
state: initialState,
currentOwner: username,
spinner: spinner.New(spinner.WithSpinner(spinner.Dot)),
listStyles: listStyles,
listItemStyles: listItemStyles,
client: newClient(httpClient, remote, username, token),
help: help.New(),
keys: keys,
currentReference: parsedReference,
searchInput: newSearchInput(),
remote: remote,
commitList: commitList,
moduleList: moduleList,
commitFilesList: commitFilesList,
}
var options []tea.ProgramOption
if *fullscreenFlag {
options = append(options, tea.WithAltScreen())
}
if _, err := tea.NewProgram(model, options...).Run(); err != nil {
return err
}
return nil
}
type modelState int
const (
modelStateBrowsingModules modelState = iota
modelStateBrowsingCommits
modelStateBrowsingCommitContents
modelStateBrowsingCommitFileContents
modelStateSearching
modelStateLoadingReference
modelStateLoadingModules
modelStateLoadingCommits
modelStateLoadingCommitFileContents
)
const (
bufBlue = lipgloss.Color("#151fd5")
bufTeal = lipgloss.Color("#91dffb")
)
type model struct {
// (Basically) Static
listStyles list.Styles
listItemStyles list.DefaultItemStyles
spinner spinner.Model
client *client
keys keyMap
remote string
// State - where are we?
state modelState
// Should exit when setting this.
err error
// State-related data
currentOwner string
currentModule string
currentCommit string
currentModules modulesMsg
currentCommits commitsMsg
currentCommitFiles []*modulev1.File
currentReference *modulev1.ResourceRef_Name
// Sub-models
moduleList list.Model
commitList list.Model
commitFilesList list.Model
fileViewport viewport.Model
searchInput textinput.Model
help help.Model
}
func (m model) Init() tea.Cmd {
inits := []tea.Cmd{m.spinner.Tick}
if m.currentReference != nil {
inits = append(inits, m.client.getResource(m.currentReference))
}
return tea.Batch(inits...)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
// If we set a width on the help menu it can gracefully truncate
// its view as needed.
m.help.Width = msg.Width
case resourceMsg:
switch retrievedResource := msg.retrievedResource.Value.(type) {
case *modulev1.Resource_Module:
m.currentOwner = msg.requestedResource.Owner
m.currentModule = retrievedResource.Module.Name
m.state = modelStateLoadingCommits
return m, m.client.listCommits(m.currentOwner, m.currentModule)
case *modulev1.Resource_Commit:
m.currentOwner = msg.requestedResource.Owner
m.currentModule = msg.requestedResource.Module
m.currentCommit = retrievedResource.Commit.Id
m.state = modelStateLoadingCommitFileContents
return m, m.client.getCommitContent(m.currentOwner, m.currentModule, m.currentCommit)
case *modulev1.Resource_Label:
// TODO: Is this possible? I guess so?
// We don't handle it right now, though.
m.err = fmt.Errorf("cannot handle type resource of type %T", retrievedResource)
return m, tea.Quit
default:
m.err = fmt.Errorf("cannot handle type resource of type %T", retrievedResource)
return m, tea.Quit
}
case modulesMsg:
m.state = modelStateBrowsingModules
m.currentModules = msg
if len(m.currentModules) == 0 {
return m, nil
}
modules := make([]list.Item, len(m.currentModules))
for i, currentModule := range m.currentModules {
modules[i] = &module{currentModule}
}
m.moduleList.SetHeight(len(m.currentModules) * 4) // TODO: Pick a reasonable value here.
m.moduleList.SetItems(modules)
m.moduleList.Title = fmt.Sprintf("Modules (Owner: %s)", m.currentOwner)
m.moduleList.Styles = m.listStyles
m.moduleList.AdditionalFullHelpKeys = func() []key.Binding {
return []key.Binding{keys.Right}
}
m.moduleList.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{keys.Right}
}
return m, nil
case commitsMsg:
m.state = modelStateBrowsingCommits
m.currentCommits = msg
if len(m.currentCommits) == 0 {
return m, nil
}
commits := make([]list.Item, len(m.currentCommits))
for i, currentCommit := range m.currentCommits {
commits[i] = &commit{currentCommit}
}
m.commitList.SetHeight(len(m.currentCommits) * 5)
m.commitList.SetItems(commits)
m.commitList.Title = fmt.Sprintf("Commits (Module: %s/%s)", m.currentOwner, m.currentModule)
m.commitList.Styles = m.listStyles
m.commitList.AdditionalFullHelpKeys = func() []key.Binding {
return []key.Binding{keys.Left, keys.Right}
}
m.commitList.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{keys.Left, keys.Right}
}
return m, nil
case contentsMsg:
m.state = modelStateBrowsingCommitContents
m.currentCommitFiles = msg.Files
commitFiles := make([]list.Item, len(m.currentCommitFiles))
for i, currentCommitFile := range m.currentCommitFiles {
commitFiles[i] = &commitFile{currentCommitFile}
}
m.commitFilesList.SetHeight(len(commitFiles) + 8) // TODO: Pick a reasonable value here.
m.commitFilesList.SetItems(commitFiles)
m.commitFilesList.Title = fmt.Sprintf("Commit %s (Module: %s/%s)", m.currentCommit, m.currentOwner, m.currentModule)
m.commitFilesList.SetShowStatusBar(false)
m.commitFilesList.Styles = m.listStyles
m.commitFilesList.AdditionalFullHelpKeys = func() []key.Binding {
return []key.Binding{keys.Left, keys.Right}
}
m.commitFilesList.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{keys.Left, keys.Right}
}
m.fileViewport = viewport.New(100, max(len(msg.Files), 30))
// Set up the initial viewport.
commitFileItem := m.commitFilesList.SelectedItem()
commitFile, ok := commitFileItem.(*commitFile)
if !ok {
panic("only commit files should be in commit files list")
}
selectedFileName := commitFile.underlying.Path
var fileContents string
for _, file := range m.currentCommitFiles {
if file.Path == selectedFileName {
fileContents = string(file.Content)
break
}
}
highlightedFile, err := highlightFile(selectedFileName, fileContents)
if err != nil {
m.err = fmt.Errorf("can't highlight file: %w", err)
return m, tea.Quit
}
m.fileViewport.SetContent(highlightedFile)
return m, nil
case errMsg:
m.err = msg.err
return m, nil
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keys.Quit):
return m, tea.Quit
case key.Matches(msg, m.keys.Help):
m.help.ShowAll = !m.help.ShowAll
case key.Matches(msg, m.keys.Search):
// From anywhere other than the search state, "s"
// enters a search state.
if m.state != modelStateSearching {
// "s" -> "search"
m.state = modelStateSearching
m.searchInput = newSearchInput()
return m, nil
}
case key.Matches(msg, m.keys.Enter):
switch m.state {
case modelStateSearching:
m.currentOwner = m.searchInput.Value()
// TODO: Clear search input?
return m, m.client.getModules(m.currentOwner)
}
// enter or l are equivalent for all the cases below.
fallthrough
case key.Matches(msg, m.keys.Right):
switch m.state {
case modelStateBrowsingModules:
if len(m.currentModules) == 0 {
// Don't do anything.
return m, nil
}
m.state = modelStateLoadingCommits
item := m.moduleList.SelectedItem()
module, ok := item.(*module)
if !ok {
panic("items in moduleList should be modules")
}
m.currentModule = module.underlying.Name
return m, m.client.listCommits(m.currentOwner, m.currentModule)
case modelStateBrowsingCommits:
if len(m.currentCommits) == 0 {
// Don't do anything.
return m, nil
}
m.state = modelStateLoadingCommitFileContents
item := m.commitList.SelectedItem()
commit, ok := item.(*commit)
if !ok {
panic("items in commitList should be commits")
}
m.currentCommit = commit.underlying.Id
return m, m.client.getCommitContent(m.currentOwner, m.currentModule, m.currentCommit)
case modelStateBrowsingCommitContents:
m.state = modelStateBrowsingCommitFileContents
return m, nil
default:
// Don't do anything, yet, in other states.
}
case key.Matches(msg, m.keys.Left):
// "h" -> "Go out"
switch m.state {
case modelStateBrowsingCommitFileContents:
m.state = modelStateBrowsingCommitContents
return m, nil
case modelStateBrowsingCommitContents:
// NOTE: We don't necessarily have the commits
// list populated, because we may have gone
// directly to a reference.
// TODO: Hook this up to caching.
m.state = modelStateLoadingCommits
return m, m.client.listCommits(m.currentOwner, m.currentModule)
case modelStateBrowsingCommits:
// NOTE: We don't necessarily have the module
// list populated, because we may have gone
// directly to a reference.
// TODO: Hook this up to caching.
m.state = modelStateLoadingModules
return m, m.client.getModules(m.currentOwner)
}
case key.Matches(msg, m.keys.Browse):
var url string
var list list.Model
switch m.state {
case modelStateBrowsingCommitFileContents:
list = m.commitFilesList
// TODO: Can we go directly to the line that the user is viewing?
// For now, do the same as modelStateBrowsingCommitContents.
commitFile, ok := m.commitFilesList.SelectedItem().(*commitFile)
if !ok {
panic("selected item is not a commitFile")
}
url = "https://" + m.remote + "/" + m.currentOwner + "/" + m.currentModule + "/file/" + m.currentCommit + ":" + commitFile.underlying.Path
case modelStateBrowsingCommitContents:
list = m.commitFilesList
commitFile, ok := m.commitFilesList.SelectedItem().(*commitFile)
if !ok {
panic("selected item is not a commitFile")
}
url = "https://" + m.remote + "/" + m.currentOwner + "/" + m.currentModule + "/file/" + m.currentCommit + ":" + commitFile.underlying.Path
case modelStateBrowsingCommits:
list = m.commitList
commit, ok := m.commitList.SelectedItem().(*commit)
if !ok {
panic("selected item is not a commit")
}
url = "https://" + m.remote + "/" + m.currentOwner + "/" + m.currentModule + "/tree/" + commit.underlying.Id
case modelStateBrowsingModules:
list = m.moduleList
module, ok := m.moduleList.SelectedItem().(*module)
if !ok {
panic("selected item is not a module")
}
url = "https://" + m.remote + "/" + m.currentOwner + "/" + module.underlying.Name
}
if url != "" {
if err := browser.OpenURL(url); err != nil {
m.err = fmt.Errorf("opening URL %q: %w", url, err)
return m, nil
}
// TODO: Where does this show? Does it need more time?
return m, list.NewStatusMessage("opened " + url)
}
}
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
var cmd tea.Cmd
switch m.state {
case modelStateBrowsingModules:
m.moduleList, cmd = m.moduleList.Update(msg)
case modelStateBrowsingCommits:
m.commitList, cmd = m.commitList.Update(msg)
case modelStateBrowsingCommitContents:
m.commitFilesList, cmd = m.commitFilesList.Update(msg)
item := m.commitFilesList.SelectedItem()
commitFile, ok := item.(*commitFile)
if !ok {
panic("only commit files should be in item list")
}
selectedFileName := commitFile.underlying.Path
var fileContents string
for _, file := range m.currentCommitFiles {
if file.Path == selectedFileName {
fileContents = string(file.Content)
break
}
}
highlightedFile, err := highlightFile(selectedFileName, fileContents)
if err != nil {
m.err = fmt.Errorf("can't highlight file: %w", err)
return m, tea.Quit
}
m.fileViewport.SetContent(highlightedFile)
case modelStateBrowsingCommitFileContents:
m.fileViewport, cmd = m.fileViewport.Update(msg)
case modelStateSearching:
m.searchInput, cmd = m.searchInput.Update(msg)
}
return m, cmd
}
func (m model) View() string {
if m.err != nil {
return fmt.Sprintf("error: %v", m.err)
}
var view string
switch m.state {
case modelStateLoadingModules:
view = m.spinner.View() + " Loading modules"
case modelStateLoadingCommits:
view = m.spinner.View() + " Loading commits"
case modelStateLoadingCommitFileContents:
view = m.spinner.View() + " Loading commit file contents"
case modelStateLoadingReference:
view = m.spinner.View() + " Loading reference"
case modelStateBrowsingModules:
if len(m.currentModules) == 0 {
view += fmt.Sprintf("No modules found for owner; use %s to enter another owner", keys.Search.Keys())
} else {
view += m.moduleList.View()
}
case modelStateBrowsingCommits:
if len(m.currentCommits) == 0 {
view += "No commits found for module"
} else {
view += m.commitList.View()
}
case modelStateBrowsingCommitContents, modelStateBrowsingCommitFileContents:
fileView := m.fileViewport.View()
if m.state == modelStateBrowsingCommitFileContents {
fileViewStyle := lipgloss.NewStyle().
Border(lipgloss.RoundedBorder(), true).
BorderForeground(bufBlue)
fileView = fileViewStyle.Render(fileView)
} else {
fileViewStyle := lipgloss.NewStyle().
Border(lipgloss.RoundedBorder(), true).
BorderForeground(bufTeal)
fileView = fileViewStyle.Render(fileView)
}
view = lipgloss.JoinHorizontal(
lipgloss.Top,
m.commitFilesList.View(),
fileView,
)
case modelStateSearching:
header := "Enter an owner (user or organization)"
view = header + "\n\n" + m.searchInput.View()
view += "\n\n" + m.help.View(m)
default:
return fmt.Sprintf("unaccounted state: %v", m.state)
}
return view
}
type errMsg struct{ err error }
// For messages that contain errors it's often handy to also implement the
// error interface on the message.
func (e errMsg) Error() string { return e.err.Error() }
// getUserTokenFromNetrc returns the username and token for the remote in the
// ~/.netrc file, if it exists.
func getUserTokenFromNetrc(remote string) (username string, token string, err error) {
currentUser, err := user.Current()
if err != nil {
return "", "", fmt.Errorf("getting current user: %s", err)
}
netrcPath := filepath.Join(currentUser.HomeDir, ".netrc")
// Give up if we can't stat the netrcPath.
if _, err := os.Stat(netrcPath); err != nil {
return "", "", nil
}
parsedNetrc, err := netrc.Parse(netrcPath)
if err != nil {
return "", "", fmt.Errorf("parsing netrc: %s", err)
}
username = parsedNetrc.Machine(remote).Get("login")
token = parsedNetrc.Machine(remote).Get("password")
return username, token, nil
}
func highlightFile(filename, fileContents string) (string, error) {
// There are only a few filetypes that can actually exist in a module:
// - LICENSE
// - Documentation files (markdown)
// - protobuf
// Ref: https://buf.build/bufbuild/registry/docs/main:buf.registry.module.v1#buf.registry.module.v1.FileType
// Fallback is for LICENSE files.
lexer := cmp.Or(lexers.Match(filename), lexers.Fallback)
// TODO: Make this configurable?
// Probably not ;)
style := cmp.Or(styles.Get("algol_nu"), styles.Fallback)
// TODO: This seemingly works on my terminal, but we may need
// to select a different one based on terminal type.
// I think we should be able to figure that out from
// tea/termenv, somehow.
formatter := cmp.Or(formatters.TTY256, formatters.Fallback)
iterator, err := lexer.Tokenise(nil, fileContents)
if err != nil {
return "", fmt.Errorf("tokenizing file: %w", err)
}
var buffer bytes.Buffer
if err := formatter.Format(&buffer, style, iterator); err != nil {
return "", fmt.Errorf("formatting file: %w", err)
}
return buffer.String(), nil
}
func parseReference(reference string) (remote string, resourceRef *modulev1.ResourceRef_Name, err error) {
if reference == "" {
// Empty reference is fine.
return "", nil, nil
}
slashCount := strings.Count(reference, "/")
if slashCount != 1 && slashCount != 2 {
return "", nil, fmt.Errorf("expecting reference of form {<remote>/}<owner>/<module>{:<ref>}, got %s", reference)
}
if strings.Count(reference, ":") > 1 {
return "", nil, fmt.Errorf(`expecting reference of form {<remote>/}<owner>/<module>{:<ref>}, got multiple ":" in %s`, reference)
}
first, reference, hasReference := strings.Cut(reference, ":")
if slashCount == 2 {
var rest string
var valid bool
remote, rest, valid = strings.Cut(first, "/")
if !valid {
panic(fmt.Errorf("strings.Cut should be valid after check"))
}
first = rest
}
owner, module, valid := strings.Cut(first, "/")
// There must be a "/", regardless of anything else.
if !valid {
// We know this is true, from the check above.
panic(fmt.Errorf("strings.Cut should be valid after check"))
}
moduleRef := &modulev1.ResourceRef_Name{
Owner: owner,
Module: module,
}
// Not sure this needs to be separate, but we'll do it anyway.
if hasReference {
moduleRef.Child = &modulev1.ResourceRef_Name_Ref{
Ref: reference,
}
}
if err := protovalidate.Validate(moduleRef); err != nil {
return "", nil, fmt.Errorf("validating reference: %w", err)
}
// TODO: Validate remote
// TODO: Can we use protovalidate/cel-go for this?
return remote, moduleRef, nil
}
// keyMap defines a set of keybindings. To work for help it must satisfy
// key.Map. It could also very easily be a map[string]key.Binding.
type keyMap struct {
Up key.Binding
Down key.Binding
Left key.Binding
Right key.Binding
Search key.Binding
Enter key.Binding
Help key.Binding
Quit key.Binding
Browse key.Binding
}
var keys = keyMap{
// TODO: Ideally we'd pull these from the viewing model KeyMap defaults
// (e.g. m.fileViewport.KeyMap); for now just give some basics.
Up: key.NewBinding(
key.WithKeys("up", "k"),
key.WithHelp("↑/k", "move up"),
),
Down: key.NewBinding(
key.WithKeys("down", "j"),
key.WithHelp("↓/j", "move down"),
),
Left: key.NewBinding(
key.WithKeys("left", "h"),
key.WithHelp("←/h", "go out"),
),
Right: key.NewBinding(
key.WithKeys("right", "l"),
key.WithHelp("→/l", "go in"),
),
Enter: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "search"),
),
Search: key.NewBinding(
key.WithKeys("s"),
key.WithHelp("s", "enter owner"),
),
Help: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "toggle help"),
),
Quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("q", "quit"),
),
Browse: key.NewBinding(
key.WithKeys("o"),
key.WithHelp("o", "open in browser"),
),
}
func (m model) ShortHelp() []key.Binding {
var shortHelp []key.Binding
switch m.state {
case modelStateBrowsingModules:
// Can't go Left while browsing modules; already at the "top".
shortHelp = []key.Binding{keys.Up, keys.Down}
if len(m.currentModules) == 0 {
// Can't go Right when no modules exist.
shortHelp = append(shortHelp, keys.Right)
}
case modelStateBrowsingCommits, modelStateBrowsingCommitContents:
shortHelp = []key.Binding{keys.Up, keys.Down, keys.Left}
if len(m.currentCommits) == 0 {
// Can't go Right when no commits exist.
shortHelp = append(shortHelp, keys.Right)
}
case modelStateBrowsingCommitFileContents:
// Can't go Right while browsing file contents; already at the "bottom".
shortHelp = []key.Binding{keys.Up, keys.Down, keys.Left}
case modelStateSearching:
shortHelp = []key.Binding{keys.Enter}
default:
// In the other states, just show Help and Quit.
return []key.Binding{keys.Help, keys.Quit}
}
// Always show the Help key.
return append(shortHelp, keys.Help)
}
func (m model) FullHelp() [][]key.Binding {
return [][]key.Binding{
m.ShortHelp(),
{keys.Search, keys.Help, keys.Quit},
}
}
func newSearchInput() textinput.Model {
searchInput := textinput.New()
// Style the input.
// TODO: This is probably too much.
searchStyle := lipgloss.NewStyle().Foreground(bufBlue).Background(bufTeal)
searchInput.PromptStyle = searchStyle
searchInput.PlaceholderStyle = searchStyle
searchInput.TextStyle = searchStyle
searchInput.Cursor.Style = searchStyle
searchInput.Focus()
searchInput.Placeholder = "bufbuild"
searchInput.Width = 20
return searchInput
}
type module struct {
underlying *modulev1.Module
}
// FilterValue implements list.Item.
func (m *module) FilterValue() string {
return m.underlying.Name
}
// Title implements list.DefaultItem.
func (m *module) Title() string {
// TODO: Incorporate visibility / state here?
return m.underlying.Name
}
// Description implements list.DefaultItem.
func (m *module) Description() string {
// TODO: Show module description.
return m.underlying.Description
}
type commit struct {
underlying *modulev1.Commit
}
// FilterValue implements list.Item.
func (m *commit) FilterValue() string {
// TODO: What to filter on?
return m.underlying.Id
}
// Title implements list.DefaultItem.
func (m *commit) Title() string {
return m.underlying.Id
}
// Description implements list.DefaultItem.
func (m *commit) Description() string {
// TODO: Support absolute/relative time.
return fmt.Sprintf("Create Time: %s", m.underlying.CreateTime.AsTime().Format(time.RFC3339Nano))
}
type commitFile struct {
underlying *modulev1.File
}
// FilterValue implements list.Item.
func (m *commitFile) FilterValue() string {
return m.underlying.Path
}
// Title implements list.DefaultItem.
func (m *commitFile) Title() string {
return m.underlying.Path
}
// Description implements list.DefaultItem.
// The description for a commit file is not shown.
func (m *commitFile) Description() string {
return ""
}