diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-02-23 21:53:30 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-02-24 22:18:04 +1100 |
commit | 46be280c921994ba783b8c53232d118f0547ad14 (patch) | |
tree | 0f2e637414a54e2dd034081f0fce888346e31655 /pkg/gui/gui.go | |
parent | 2a5763a77190a579ae3cb57926a0d907964fcccb (diff) |
support searching in side panels
For now we're just doing side panels, because it will take more work
to support this in the various main panel contexts
Diffstat (limited to 'pkg/gui/gui.go')
-rw-r--r-- | pkg/gui/gui.go | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 929c4ba88..015e3ebfe 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -172,6 +172,12 @@ type panelStates struct { Status *statusPanelState } +type searchingState struct { + view *gocui.View + isSearching bool + searchString string +} + type guiState struct { Files []*commands.File Branches []*commands.Branch @@ -195,6 +201,7 @@ type guiState struct { RetainOriginalDir bool IsRefreshingFiles bool RefreshingFilesMutex sync.Mutex + Searching searchingState } // for now the split view will always be on @@ -338,6 +345,10 @@ func (gui *Gui) onFocusLost(v *gocui.View, newView *gocui.View) error { if v == nil { return nil } + if v.IsSearching() && newView.Name() != "search" { + gui.State.Searching.isSearching = false + v.ClearSearch() + } switch v.Name() { case "branches": if v.Context == "local-branches" { @@ -500,7 +511,6 @@ func (gui *Gui) layout(g *gocui.Gui) error { } } - userConfig := gui.Config.GetUserConfig() v, err := g.SetView(main, leftSideWidth+panelSpacing, 0, mainPanelRight, mainPanelBottom, gocui.LEFT) if err != nil { if err.Error() != "unknown view" { @@ -510,6 +520,9 @@ func (gui *Gui) layout(g *gocui.Gui) error { v.Wrap = true v.FgColor = textColor v.IgnoreCarriageReturns = true + v.SetOnSelectItem(gui.onSelectItemWrapper(func(selectedLine int) error { + return nil + })) } hiddenViewOffset := 0 @@ -542,6 +555,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { } filesView.Highlight = true filesView.Title = gui.Tr.SLocalize("FilesTitle") + filesView.SetOnSelectItem(gui.onSelectItemWrapper(gui.onFilesPanelSearchSelect)) } branchesView, err := g.SetViewBeneath("branches", "files", vHeights["branches"]) @@ -552,6 +566,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { branchesView.Title = gui.Tr.SLocalize("BranchesTitle") branchesView.Tabs = []string{"Local Branches", "Remotes", "Tags"} branchesView.FgColor = textColor + branchesView.SetOnSelectItem(gui.onSelectItemWrapper(gui.onBranchesPanelSearchSelect)) } if v, err := g.SetViewBeneath("commitFiles", "branches", vHeights["commits"]); err != nil { @@ -560,6 +575,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { } v.Title = gui.Tr.SLocalize("CommitFiles") v.FgColor = textColor + v.SetOnSelectItem(gui.onSelectItemWrapper(gui.onCommitFilesPanelSearchSelect)) } commitsView, err := g.SetViewBeneath("commits", "branches", vHeights["commits"]) @@ -570,6 +586,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { commitsView.Title = gui.Tr.SLocalize("CommitsTitle") commitsView.Tabs = []string{"Commits", "Reflog"} commitsView.FgColor = textColor + commitsView.SetOnSelectItem(gui.onSelectItemWrapper(gui.onCommitsPanelSearchSelect)) } stashView, err := g.SetViewBeneath("stash", "commits", vHeights["stash"]) @@ -579,6 +596,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { } stashView.Title = gui.Tr.SLocalize("StashTitle") stashView.FgColor = textColor + stashView.SetOnSelectItem(gui.onSelectItemWrapper(gui.onStashPanelSearchSelect)) } if v, err := g.SetView("options", appStatusOptionsBoundary-1, height-2, optionsVersionBoundary-1, height, 0); err != nil { @@ -586,7 +604,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { return err } v.Frame = false - v.FgColor = theme.GetGocuiColor(userConfig.GetStringSlice("gui.theme.optionsTextColor")) + v.FgColor = theme.OptionsColor } if gui.getCommitMessageView() == nil { @@ -619,6 +637,35 @@ func (gui *Gui) layout(g *gocui.Gui) error { } } + searchViewOffset := hiddenViewOffset + if gui.State.Searching.isSearching { + searchViewOffset = 0 + } + + // this view takes up one character. Its only purpose is to show the slash when searching + searchPrefix := "search: " + if searchPrefixView, err := g.SetView("searchPrefix", appStatusOptionsBoundary-1+searchViewOffset, height-2+searchViewOffset, len(searchPrefix)+searchViewOffset, height+searchViewOffset, 0); err != nil { + if err.Error() != "unknown view" { + return err + } + + searchPrefixView.BgColor = gocui.ColorDefault + searchPrefixView.FgColor = gocui.ColorGreen + searchPrefixView.Frame = false + gui.setViewContent(gui.g, searchPrefixView, searchPrefix) + } + + if searchView, err := g.SetView("search", appStatusOptionsBoundary-1+searchViewOffset+len(searchPrefix), height-2+searchViewOffset, optionsVersionBoundary+searchViewOffset, height+searchViewOffset, 0); err != nil { + if err.Error() != "unknown view" { + return err + } + + searchView.BgColor = gocui.ColorDefault + searchView.FgColor = gocui.ColorGreen + searchView.Frame = false + searchView.Editable = true + } + if appStatusView, err := g.SetView("appStatus", -1, height-2, width, height, 0); err != nil { if err.Error() != "unknown view" { return err @@ -826,6 +873,12 @@ func (gui *Gui) Run() error { return err } defer g.Close() + + g.OnSearchEscape = gui.onSearchEscape + g.SearchEscapeKey = gui.getKey("universal.return") + g.NextSearchMatchKey = gui.getKey("universal.nextMatch") + g.PrevSearchMatchKey = gui.getKey("universal.prevMatch") + gui.stopChan = make(chan struct{}) g.ASCII = runtime.GOOS == "windows" && runewidth.IsEastAsian() |