Add selection ability and cursor movement

This commit is contained in:
2023-02-11 21:58:36 -08:00
parent 2e01847d63
commit c890907cee
3 changed files with 83 additions and 10 deletions

View File

@@ -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()
}