Skip to content

Commit

Permalink
exotui: add ability to navigate backwards through tag stack with new …
Browse files Browse the repository at this point in the history
…'b' command
  • Loading branch information
neutralinsomniac committed Apr 4, 2020
1 parent 99f954d commit 44035ce
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
45 changes: 40 additions & 5 deletions cmd/exotui/exotui.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type state struct {
tagShortcutsRev map[int]db.Tag
snarfedRows []db.Row
allTagNames map[string]bool
tagStack []string // tag.Name
lastError string
}

Expand Down Expand Up @@ -98,13 +99,44 @@ func (s *state) Refresh() {
}

func (s *state) SwitchTag(tag db.Tag) {
if tag != s.CurrentDBTag {
err := s.DeleteTagIfEmpty(s.CurrentDBTag.ID)
if tag.ID != s.CurrentDBTag.ID {
deleted, err := s.DeleteTagIfEmpty(s.CurrentDBTag.ID)
checkErr(err)
// if we're switching to a new tag, and this tag didn't just get wiped,
// push it onto our tag stack
if !deleted {
s.tagStack = append(s.tagStack, s.CurrentDBTag.Name)
}
}

s.CurrentDBTag = tag
s.Refresh()
}

func (s *state) PopTag() {
if len(s.tagStack) == 0 {
s.lastError = "tag stack empty"
return
}

l := len(s.tagStack)
tagName := s.tagStack[l-1]
s.tagStack = s.tagStack[:l-1]

tag, err := s.DB.AddTag(tagName)
checkErr(err)

// have to do the manual tag switch dance since SwitchTag() will push onto the tag stack
if tag.ID != s.CurrentDBTag.ID {
_, err := s.DeleteTagIfEmpty(s.CurrentDBTag.ID)
checkErr(err)
}

s.CurrentDBTag = tag
s.Refresh()
s.lastError = ""

return
}

func (s *state) GetShortcutForTag(tag db.Tag) int {
Expand Down Expand Up @@ -720,12 +752,13 @@ func (s *state) printHelp() {
fmt.Println("[Tags]")
fmt.Println("h: jump to today tag ('h'ome)")
fmt.Println("t: open all tags menu ('t'ags)")
fmt.Println("g: open date picker ('g'oto)")
fmt.Println("<: go back one day (left)")
fmt.Println(">: go forward one day (right)")
fmt.Println("t/<text>: search tag names for <text>")
fmt.Println("t <text>: jump to or create to exact tag <text>")
fmt.Println("r [text]: rename current tag with text <text> ('r'ename)")
fmt.Println("g: open date picker ('g'oto)")
fmt.Println("<: go back one day (left)")
fmt.Println(">: go forward one day (right)")
fmt.Println("b: jump backwards in tag stack ('b'ack)")
fmt.Println("")
fmt.Println("[Rows]")
fmt.Println("[num]: jump to row-referenced tag")
Expand Down Expand Up @@ -782,6 +815,8 @@ func main() {
programState.NewRow(line[1:])
case 'A':
programState.InsertRow(line[1:])
case 'b':
programState.PopTag()
case 'd':
programState.DeleteRows(line[1:])
case 'e':
Expand Down
7 changes: 5 additions & 2 deletions db/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ End:
return err
}

func (s *State) DeleteTagIfEmpty(id int64) error {
func (s *State) DeleteTagIfEmpty(id int64) (bool, error) {
var rows []Row
var refs Refs
var err error

deleted := false

rows, err = s.DB.GetRowsForTagID(id)
if err != nil {
goto End
Expand All @@ -61,8 +63,9 @@ func (s *State) DeleteTagIfEmpty(id int64) error {

if len(rows)+len(refs) == 0 {
err = s.DB.DeleteTagByID(id)
deleted = true
}

End:
return err
return deleted, err
}

0 comments on commit 44035ce

Please sign in to comment.