From c890907cee8352a7642b2d2c8bb3b4421cee8217 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Sat, 11 Feb 2023 21:58:36 -0800 Subject: [PATCH] Add selection ability and cursor movement --- cmd/cli/cli.go | 50 ++++++++++++++++++++++++++++++++++++---------- graph/term/term.go | 39 ++++++++++++++++++++++++++++++++++++ types/helpers.go | 4 ++++ 3 files changed, 83 insertions(+), 10 deletions(-) diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index c634c18..9d237aa 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -42,6 +42,7 @@ type model struct { CommitLogModel CommitLog HelpModel Help quitting bool + cursor Cursor err error } @@ -63,6 +64,14 @@ var quitKeys = key.NewBinding( key.WithHelp("", "press q to quit"), ) +const ( + settings Cursor = iota + graph + commitLog +) + +type Cursor int + func initialModel() (model, error) { var m model var err error @@ -70,6 +79,7 @@ func initialModel() (model, error) { if err != nil { return m, err } + m.cursor = graph return m, nil } @@ -103,15 +113,28 @@ func (m CommitLog) View() string { func (m Graph) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { - case tea.Key: + case tea.KeyMsg: switch msg.String() { + case "down": + if m.Selected%7 != 6 { + m.Selected++ + } case "up": + if m.Selected%7 != 0 { + m.Selected-- + } case "left": if m.Selected > 6 { m.Selected -= 7 } else { // TODO calculate the square for this day last year } + case "right": + if m.Selected < 358 { + m.Selected += 7 + } else { + // TODO + } } } return m, nil @@ -120,7 +143,7 @@ func (m Graph) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func NewGraph() (Graph, error) { var m Graph now := time.Now() - today := now.YearDay() + today := now.YearDay() - 1 year := now.Year() aName, _ := commits.GetAuthorName() aEmail, _ := commits.GetAuthorEmail() @@ -137,33 +160,40 @@ func NewGraph() (Graph, error) { } func (m Graph) Init() tea.Cmd { + go func() { + mr := commits.RepoSet(m.Repos) + mr.FrequencyChan(m.Year-1, m.Authors) + }() return nil } func (m Graph) View() string { mr := commits.RepoSet(m.Repos) gfreq, _ := mr.FrequencyChan(m.Year, m.Authors) - return gfreq.String() + return gfreq.StringSelected(m.Selected) } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - switch msg := msg.(type) { + switch cmd := msg.(type) { case tea.KeyMsg: - if key.Matches(msg, quitKeys) { + if key.Matches(cmd, quitKeys) { m.quitting = true return m, tea.Quit - } - return m, nil case errMsg: - m.err = msg + m.err = cmd return m, nil default: - var cmd tea.Cmd + } + switch m.cursor { + case graph: + tmp, cmd := m.GraphModel.Update(msg) + m.GraphModel, _ = tmp.(Graph) return m, cmd } + return m, nil } func (m model) View() string { @@ -171,7 +201,7 @@ func (m model) View() string { return m.err.Error() } if m.quitting { - return "\n" + return "" } return m.GraphModel.View() } diff --git a/graph/term/term.go b/graph/term/term.go index 31ed667..21ef6a4 100644 --- a/graph/term/term.go +++ b/graph/term/term.go @@ -42,6 +42,15 @@ func drawWeekUnicode(c []sc.SimpleColor) string { return s.String() } +func GetYearUnicodeSelected(frequencies []int, selected int) string { + squareColors := []sc.SimpleColor{} + min, max := common.MinMax(frequencies) + for _, f := range frequencies { + squareColors = append(squareColors, common.ColorForFrequency(f, min, max)) + } + return drawYearUnicodeSelected(squareColors, selected) +} + func GetYearUnicode(frequencies []int) string { squareColors := []sc.SimpleColor{} min, max := common.MinMax(frequencies) @@ -51,6 +60,36 @@ func GetYearUnicode(frequencies []int) string { return drawYearUnicode(squareColors) } +func drawYearUnicodeSelected(c []sc.SimpleColor, selected int) string { + // o := termenv.NewOutput(os.Stdout) + var s strings.Builder + o := termenv.NewOutput(os.Stdout, termenv.WithProfile(termenv.TrueColor)) + weekRows := [7][]sc.SimpleColor{{}} + for i := 0; i < 7; i++ { + weekRows[i] = []sc.SimpleColor{} + } + for i := 0; i < len(c); i++ { + weekRows[i%7] = append(weekRows[i%7], c[i]) + } + for i, row := range weekRows { + for w, d := range row { + style := o.String(block).Foreground(termenv.TrueColor.Color(d.ToHex())) + if w*7+i == selected { + style = o.String(block).Foreground(termenv.TrueColor.Color("#FFFFFF")) + // style = style.Background(termenv.TrueColor.Color("#FF0000")) + } + s.WriteString(style.String()) + if w == len(row)-1 { + s.WriteString("\n") + } else { + s.WriteString(" ") + } + + } + } + return s.String() +} + func drawYearUnicode(c []sc.SimpleColor) string { // o := termenv.NewOutput(os.Stdout) var s strings.Builder diff --git a/types/helpers.go b/types/helpers.go index 59725a6..ccece49 100644 --- a/types/helpers.go +++ b/types/helpers.go @@ -19,6 +19,10 @@ func NewCommit(Author, Message, Repo, Path string, Added, Deleted, FilesChanged return ci } +func (yf Freq) StringSelected(selected int) string { + return gterm.GetYearUnicodeSelected(yf, selected) +} + func (yf Freq) String() string { return gterm.GetYearUnicode(yf) }