2020-02-23 12:53:30 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/theme"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleOpenSearch(viewName string) error {
|
|
|
|
view, err := gui.g.View(viewName)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-23 12:53:30 +02:00
|
|
|
gui.State.Searching.isSearching = true
|
2021-04-02 10:20:40 +02:00
|
|
|
gui.State.Searching.view = view
|
2020-08-19 11:07:14 +02:00
|
|
|
|
2020-08-15 08:36:39 +02:00
|
|
|
gui.renderString("search", "")
|
2020-08-19 11:07:14 +02:00
|
|
|
|
2021-04-03 06:56:11 +02:00
|
|
|
if err := gui.pushContext(gui.State.Contexts.Search); err != nil {
|
2020-03-09 02:34:10 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-02-23 12:53:30 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleSearch() error {
|
2021-04-04 15:51:59 +02:00
|
|
|
gui.State.Searching.searchString = gui.Views.Search.Buffer()
|
2020-08-26 11:31:49 +02:00
|
|
|
if err := gui.returnFromContext(); err != nil {
|
2020-03-09 02:34:10 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-26 11:31:49 +02:00
|
|
|
view := gui.State.Searching.view
|
|
|
|
if view == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := view.Search(gui.State.Searching.searchString); err != nil {
|
2020-02-23 12:53:30 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) onSelectItemWrapper(innerFunc func(int) error) func(int, int, int) error {
|
2020-10-03 06:54:55 +02:00
|
|
|
keybindingConfig := gui.Config.GetUserConfig().Keybinding
|
|
|
|
|
2020-02-23 12:53:30 +02:00
|
|
|
return func(y int, index int, total int) error {
|
|
|
|
if total == 0 {
|
|
|
|
gui.renderString(
|
|
|
|
"search",
|
|
|
|
fmt.Sprintf(
|
|
|
|
"no matches for '%s' %s",
|
|
|
|
gui.State.Searching.searchString,
|
|
|
|
utils.ColoredString(
|
2020-10-03 06:54:55 +02:00
|
|
|
fmt.Sprintf("%s: exit search mode", gui.getKeyDisplay(keybindingConfig.Universal.Return)),
|
2020-02-23 12:53:30 +02:00
|
|
|
theme.OptionsFgColor,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
gui.renderString(
|
|
|
|
"search",
|
|
|
|
fmt.Sprintf(
|
|
|
|
"matches for '%s' (%d of %d) %s",
|
|
|
|
gui.State.Searching.searchString,
|
|
|
|
index+1,
|
|
|
|
total,
|
|
|
|
utils.ColoredString(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%s: next match, %s: previous match, %s: exit search mode",
|
2020-10-03 06:54:55 +02:00
|
|
|
gui.getKeyDisplay(keybindingConfig.Universal.NextMatch),
|
|
|
|
gui.getKeyDisplay(keybindingConfig.Universal.PrevMatch),
|
|
|
|
gui.getKeyDisplay(keybindingConfig.Universal.Return),
|
2020-02-23 12:53:30 +02:00
|
|
|
),
|
|
|
|
theme.OptionsFgColor,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if err := innerFunc(y); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) onSearchEscape() error {
|
|
|
|
gui.State.Searching.isSearching = false
|
2020-02-24 13:15:10 +02:00
|
|
|
if gui.State.Searching.view != nil {
|
|
|
|
gui.State.Searching.view.ClearSearch()
|
|
|
|
gui.State.Searching.view = nil
|
|
|
|
}
|
|
|
|
|
2020-02-23 12:53:30 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleSearchEscape() error {
|
2020-08-26 11:31:49 +02:00
|
|
|
if err := gui.onSearchEscape(); err != nil {
|
2020-02-23 12:53:30 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-26 11:31:49 +02:00
|
|
|
if err := gui.returnFromContext(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-02-23 12:53:30 +02:00
|
|
|
}
|