Skip to content

Commit

Permalink
Just one enter on autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmusLindroth committed Apr 11, 2020
1 parent 526f26d commit 1345d9f
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 123 deletions.
124 changes: 124 additions & 0 deletions cmdbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"strings"

"github.com/gdamore/tcell"
"github.com/rivo/tview"
)

Expand All @@ -14,6 +15,7 @@ func NewCmdBar(app *App) *CmdBar {

c.Input.SetFieldBackgroundColor(app.Config.Style.Background)
c.Input.SetFieldTextColor(app.Config.Style.Text)
c.Input.SetDoneFunc(c.DoneFunc)

return c
}
Expand All @@ -35,4 +37,126 @@ func (c *CmdBar) ShowError(s string) {
func (c *CmdBar) ClearInput() {
c.Input.SetFieldTextColor(c.app.Config.Style.Text)
c.Input.SetText("")
c.Input.Autocomplete()
}

func (c *CmdBar) DoneFunc(key tcell.Key) {
input := c.GetInput()
parts := strings.Split(input, " ")
if len(parts) == 0 {
return
}
switch parts[0] {
case ":q":
fallthrough
case ":quit":
c.app.UI.Root.Stop()
case ":compose":
c.app.UI.NewToot()
c.app.UI.CmdBar.ClearInput()
case ":blocking":
c.app.UI.StatusView.AddFeed(NewUserListFeed(c.app, UserListBlocking, ""))
c.app.UI.SetFocus(LeftPaneFocus)
c.app.UI.CmdBar.ClearInput()
case ":boosts":
c.app.UI.CmdBar.ClearInput()
status := c.app.UI.StatusView.GetCurrentStatus()
if status == nil {
return
}

if status.Reblog != nil {
status = status.Reblog
}
c.app.UI.StatusView.AddFeed(NewUserListFeed(c.app, UserListBoosts, string(status.ID)))
c.app.UI.SetFocus(LeftPaneFocus)
case ":favorites":
c.app.UI.CmdBar.ClearInput()
status := c.app.UI.StatusView.GetCurrentStatus()
if status == nil {
return
}
if status.Reblog != nil {
status = status.Reblog
}
c.app.UI.StatusView.AddFeed(NewUserListFeed(c.app, UserListFavorites, string(status.ID)))
c.app.UI.SetFocus(LeftPaneFocus)
/*
case ":followers":
app.UI.CmdBar.ClearInput()
user := app.UI.StatusView.GetCurrentUser()
if user == nil {
return
}
app.UI.StatusView.AddFeed(NewUserListFeed(app, UserListFollowers, string(user.ID)))
app.UI.SetFocus(LeftPaneFocus)
case ":following":
app.UI.CmdBar.ClearInput()
user := app.UI.StatusView.GetCurrentUser()
if user == nil {
return
}
app.UI.StatusView.AddFeed(NewUserListFeed(app, UserListFollowing, string(user.ID)))
app.UI.SetFocus(LeftPaneFocus)
*/
case ":muting":
c.app.UI.StatusView.AddFeed(NewUserListFeed(c.app, UserListMuting, ""))
c.app.UI.SetFocus(LeftPaneFocus)
c.app.UI.CmdBar.ClearInput()
case ":profile":
c.app.UI.CmdBar.ClearInput()
if c.app.Me == nil {
return
}
c.app.UI.StatusView.AddFeed(NewUserFeed(c.app, *c.app.Me))
c.app.UI.SetFocus(LeftPaneFocus)
case ":timeline", ":tl":
if len(parts) < 2 {
break
}
switch parts[1] {
case "local", "l":
c.app.UI.StatusView.AddFeed(NewTimelineFeed(c.app, TimelineLocal))
c.app.UI.SetFocus(LeftPaneFocus)
c.app.UI.CmdBar.ClearInput()
case "federated", "f":
c.app.UI.StatusView.AddFeed(NewTimelineFeed(c.app, TimelineFederated))
c.app.UI.SetFocus(LeftPaneFocus)
c.app.UI.CmdBar.ClearInput()
case "direct", "d":
c.app.UI.StatusView.AddFeed(NewTimelineFeed(c.app, TimelineDirect))
c.app.UI.SetFocus(LeftPaneFocus)
c.app.UI.CmdBar.ClearInput()
case "home", "h":
c.app.UI.StatusView.AddFeed(NewTimelineFeed(c.app, TimelineHome))
c.app.UI.SetFocus(LeftPaneFocus)
c.app.UI.CmdBar.ClearInput()
case "notifications", "n":
c.app.UI.StatusView.AddFeed(NewNoticifationsFeed(c.app))
c.app.UI.SetFocus(LeftPaneFocus)
c.app.UI.CmdBar.ClearInput()
}
case ":tag":
if len(parts) < 2 {
break
}
tag := strings.TrimSpace(strings.TrimPrefix(parts[1], "#"))
if len(tag) == 0 {
break
}
c.app.UI.StatusView.AddFeed(NewTagFeed(c.app, tag))
c.app.UI.SetFocus(LeftPaneFocus)
c.app.UI.CmdBar.ClearInput()
case ":user":
if len(parts) < 2 {
break
}
user := strings.TrimSpace(parts[1])
if len(user) == 0 {
break
}
c.app.UI.StatusView.AddFeed(NewUserListFeed(c.app, UserListSearch, user))
c.app.UI.SetFocus(LeftPaneFocus)
c.app.UI.CmdBar.ClearInput()
}
}
131 changes: 8 additions & 123 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ func main() {

if app.UI.Focus == CmdBarFocus {
switch event.Key() {
case tcell.KeyEnter:
app.UI.CmdBar.DoneFunc(tcell.KeyEnter)
case tcell.KeyEsc:
app.UI.CmdBar.Input.SetText("")
app.UI.CmdBar.Input.Autocomplete().Blur()
app.UI.CmdBar.ClearInput()
app.UI.SetFocus(LeftPaneFocus)
return nil
}
Expand Down Expand Up @@ -205,6 +206,11 @@ func main() {
app.UI.MediaOverlay.InputField.HandleChanges,
)

app.UI.CmdBar.Input.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {

return event
})

app.UI.CmdBar.Input.SetAutocompleteFunc(func(currentText string) (entries []string) {
words := strings.Split(":blocking,:boosts,:compose,:favorites,:muting,:profile,:tag,:timeline,:tl,:user,:quit,:q", ",")
if currentText == "" {
Expand All @@ -229,127 +235,6 @@ func main() {
return
})

app.UI.CmdBar.Input.SetDoneFunc(func(key tcell.Key) {
input := app.UI.CmdBar.GetInput()
parts := strings.Split(input, " ")
if len(parts) == 0 {
return
}
switch parts[0] {
case ":q":
fallthrough
case ":quit":
app.UI.Root.Stop()
case ":compose":
app.UI.NewToot()
app.UI.CmdBar.ClearInput()
case ":blocking":
app.UI.StatusView.AddFeed(NewUserListFeed(app, UserListBlocking, ""))
app.UI.SetFocus(LeftPaneFocus)
app.UI.CmdBar.ClearInput()
case ":boosts":
app.UI.CmdBar.ClearInput()
status := app.UI.StatusView.GetCurrentStatus()
if status == nil {
return
}

if status.Reblog != nil {
status = status.Reblog
}
app.UI.StatusView.AddFeed(NewUserListFeed(app, UserListBoosts, string(status.ID)))
app.UI.SetFocus(LeftPaneFocus)
case ":favorites":
app.UI.CmdBar.ClearInput()
status := app.UI.StatusView.GetCurrentStatus()
if status == nil {
return
}
if status.Reblog != nil {
status = status.Reblog
}
app.UI.StatusView.AddFeed(NewUserListFeed(app, UserListFavorites, string(status.ID)))
app.UI.SetFocus(LeftPaneFocus)
/*
case ":followers":
app.UI.CmdBar.ClearInput()
user := app.UI.StatusView.GetCurrentUser()
if user == nil {
return
}
app.UI.StatusView.AddFeed(NewUserListFeed(app, UserListFollowers, string(user.ID)))
app.UI.SetFocus(LeftPaneFocus)
case ":following":
app.UI.CmdBar.ClearInput()
user := app.UI.StatusView.GetCurrentUser()
if user == nil {
return
}
app.UI.StatusView.AddFeed(NewUserListFeed(app, UserListFollowing, string(user.ID)))
app.UI.SetFocus(LeftPaneFocus)
*/
case ":muting":
app.UI.StatusView.AddFeed(NewUserListFeed(app, UserListMuting, ""))
app.UI.SetFocus(LeftPaneFocus)
app.UI.CmdBar.ClearInput()
case ":profile":
app.UI.CmdBar.ClearInput()
if app.Me == nil {
return
}
app.UI.StatusView.AddFeed(NewUserFeed(app, *app.Me))
app.UI.SetFocus(LeftPaneFocus)
case ":timeline", ":tl":
if len(parts) < 2 {
break
}
switch parts[1] {
case "local", "l":
app.UI.StatusView.AddFeed(NewTimelineFeed(app, TimelineLocal))
app.UI.SetFocus(LeftPaneFocus)
app.UI.CmdBar.ClearInput()
case "federated", "f":
app.UI.StatusView.AddFeed(NewTimelineFeed(app, TimelineFederated))
app.UI.SetFocus(LeftPaneFocus)
app.UI.CmdBar.ClearInput()
case "direct", "d":
app.UI.StatusView.AddFeed(NewTimelineFeed(app, TimelineDirect))
app.UI.SetFocus(LeftPaneFocus)
app.UI.CmdBar.ClearInput()
case "home", "h":
app.UI.StatusView.AddFeed(NewTimelineFeed(app, TimelineHome))
app.UI.SetFocus(LeftPaneFocus)
app.UI.CmdBar.ClearInput()
case "notifications", "n":
app.UI.StatusView.AddFeed(NewNoticifationsFeed(app))
app.UI.SetFocus(LeftPaneFocus)
app.UI.CmdBar.ClearInput()
}
case ":tag":
if len(parts) < 2 {
break
}
tag := strings.TrimSpace(strings.TrimPrefix(parts[1], "#"))
if len(tag) == 0 {
break
}
app.UI.StatusView.AddFeed(NewTagFeed(app, tag))
app.UI.SetFocus(LeftPaneFocus)
app.UI.CmdBar.ClearInput()
case ":user":
if len(parts) < 2 {
break
}
user := strings.TrimSpace(parts[1])
if len(user) == 0 {
break
}
app.UI.StatusView.AddFeed(NewUserListFeed(app, UserListSearch, user))
app.UI.SetFocus(LeftPaneFocus)
app.UI.CmdBar.ClearInput()
}
})

app.UI.AuthOverlay.Input.SetDoneFunc(func(key tcell.Key) {
app.UI.AuthOverlay.GotInput()
})
Expand Down

0 comments on commit 1345d9f

Please sign in to comment.