2020-02-23 21:53:30 +11:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-08-06 18:50:52 +10:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
|
2020-02-23 21:53:30 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/theme"
|
|
|
|
)
|
|
|
|
|
2021-04-02 19:20:40 +11:00
|
|
|
func (gui *Gui) handleOpenSearch(viewName string) error {
|
|
|
|
view, err := gui.g.View(viewName)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-23 21:53:30 +11:00
|
|
|
gui.State.Searching.isSearching = true
|
2021-04-02 19:20:40 +11:00
|
|
|
gui.State.Searching.view = view
|
2020-08-19 19:07:14 +10:00
|
|
|
|
2021-10-17 13:00:44 +11:00
|
|
|
gui.Views.Search.ClearTextArea()
|
2020-08-19 19:07:14 +10:00
|
|
|
|
2022-01-16 14:46:53 +11:00
|
|
|
if err := gui.c.PushContext(gui.State.Contexts.Search); err != nil {
|
2020-03-09 11:34:10 +11:00
|
|
|
return err
|
|
|
|
}
|
2020-02-23 21:53:30 +11:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 19:20:40 +11:00
|
|
|
func (gui *Gui) handleSearch() error {
|
2021-10-17 13:00:44 +11:00
|
|
|
gui.State.Searching.searchString = gui.Views.Search.TextArea.GetContent()
|
2022-02-22 20:17:26 +11:00
|
|
|
if err := gui.c.PopContext(); err != nil {
|
2020-03-09 11:34:10 +11:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-26 19:31:49 +10:00
|
|
|
view := gui.State.Searching.view
|
|
|
|
if view == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := view.Search(gui.State.Searching.searchString); err != nil {
|
2020-02-23 21:53:30 +11:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) onSelectItemWrapper(innerFunc func(int) error) func(int, int, int) error {
|
2022-01-16 14:46:53 +11:00
|
|
|
keybindingConfig := gui.c.UserConfig.Keybinding
|
2020-10-03 14:54:55 +10:00
|
|
|
|
2020-02-23 21:53:30 +11:00
|
|
|
return func(y int, index int, total int) error {
|
|
|
|
if total == 0 {
|
2022-01-15 12:04:00 +11:00
|
|
|
return gui.renderString(
|
2021-04-05 00:31:52 +10:00
|
|
|
gui.Views.Search,
|
2020-02-23 21:53:30 +11:00
|
|
|
fmt.Sprintf(
|
|
|
|
"no matches for '%s' %s",
|
|
|
|
gui.State.Searching.searchString,
|
2022-08-06 18:50:52 +10:00
|
|
|
theme.OptionsFgColor.Sprintf("%s: exit search mode", keybindings.Label(keybindingConfig.Universal.Return)),
|
2020-02-23 21:53:30 +11:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2022-01-15 12:04:00 +11:00
|
|
|
_ = gui.renderString(
|
2021-04-05 00:31:52 +10:00
|
|
|
gui.Views.Search,
|
2020-02-23 21:53:30 +11:00
|
|
|
fmt.Sprintf(
|
|
|
|
"matches for '%s' (%d of %d) %s",
|
|
|
|
gui.State.Searching.searchString,
|
|
|
|
index+1,
|
|
|
|
total,
|
2021-07-27 15:00:37 +02:00
|
|
|
theme.OptionsFgColor.Sprintf(
|
|
|
|
"%s: next match, %s: previous match, %s: exit search mode",
|
2022-08-06 18:50:52 +10:00
|
|
|
keybindings.Label(keybindingConfig.Universal.NextMatch),
|
|
|
|
keybindings.Label(keybindingConfig.Universal.PrevMatch),
|
|
|
|
keybindings.Label(keybindingConfig.Universal.Return),
|
2020-02-23 21:53:30 +11:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if err := innerFunc(y); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) onSearchEscape() error {
|
|
|
|
gui.State.Searching.isSearching = false
|
2020-02-24 22:15:10 +11:00
|
|
|
if gui.State.Searching.view != nil {
|
|
|
|
gui.State.Searching.view.ClearSearch()
|
|
|
|
gui.State.Searching.view = nil
|
|
|
|
}
|
|
|
|
|
2020-02-23 21:53:30 +11:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 19:20:40 +11:00
|
|
|
func (gui *Gui) handleSearchEscape() error {
|
2020-08-26 19:31:49 +10:00
|
|
|
if err := gui.onSearchEscape(); err != nil {
|
2020-02-23 21:53:30 +11:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-22 20:17:26 +11:00
|
|
|
if err := gui.c.PopContext(); err != nil {
|
2020-08-26 19:31:49 +10:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-02-23 21:53:30 +11:00
|
|
|
}
|