0.1.21 fbfa93c

progressbar.go RawHistoryFolder

1package mvu
2
3import (
4	"io"
5	"strings"
6
7	"github.com/charmbracelet/bubbles/progress"
8	tea "github.com/charmbracelet/bubbletea"
9	"github.com/charmbracelet/lipgloss"
10)
11
12const (
13	padding  = 2
14	maxWidth = 80
15)
16
17var helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#626262")).Render
18
19type ProgressMsg float64
20
21type Model struct {
22	progress progress.Model
23	Done     bool
24}
25
26type ProgressReader struct {
27	Reader     io.ReadCloser
28	Total      int64
29	ReadBytes  int64
30	ProgressCh chan<- ProgressMsg
31}
32
33func NewPBModel() Model {
34	m := progress.New(progress.WithGradient("#007BC0", "#011E5C"))
35	m.Width = maxWidth
36
37	return Model{
38		progress: m,
39	}
40}
41
42func (pr *ProgressReader) Read(p []byte) (int, error) {
43	n, err := pr.Reader.Read(p)
44	if err != nil {
45		return n, err
46	}
47
48	pr.ReadBytes += int64(n)
49	percentage := float64(pr.ReadBytes) / float64(pr.Total)
50	pr.ProgressCh <- ProgressMsg(percentage) // Send the percentage on the channel.
51	return n, err
52}
53
54func (pr *ProgressReader) Close() error {
55	if closer, ok := pr.Reader.(io.Closer); ok {
56		return closer.Close()
57	}
58	return nil
59}
60
61func (m Model) Init() tea.Cmd {
62	m.progress.Init()
63	m.progress.SetPercent(0)
64	return nil
65}
66
67func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
68	switch msg := msg.(type) {
69	case tea.KeyMsg:
70		return m, tea.Quit
71
72	case tea.WindowSizeMsg:
73		m.progress.Width = msg.Width - padding*2 - 4
74		if m.progress.Width > maxWidth {
75			m.progress.Width = maxWidth
76		}
77		return m, nil
78
79	case ProgressMsg:
80		// Update the progress bar with the percentage received on the channel.
81		cmd := m.progress.SetPercent(float64(msg))
82
83		// If the percentage is 100, set Done to true.
84		if msg == 1 {
85			m.Done = true
86			// quit
87			return m, tea.Quit
88		}
89
90		return m, cmd
91
92	case progress.FrameMsg:
93		progressModel, cmd := m.progress.Update(msg)
94		m.progress = progressModel.(progress.Model)
95		return m, cmd
96
97	default:
98		return m, nil
99	}
100}
101
102func (m Model) View() string {
103	pad := strings.Repeat(" ", padding)
104	return "\n" +
105		pad + m.progress.View() + "\n\n" +
106		pad + helpStyle("Press any key to quit")
107}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close