1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-06-01 18:31:08 +00:00

Compare commits

...

5 Commits

Author SHA1 Message Date
Robin Schubert
c3c69cecbe
Merge 8c6950635a into 004851f651 2021-12-24 16:58:55 -07:00
schubisu
8c6950635a fixed non-exhaustive switch case 2021-06-26 11:48:53 +02:00
schubisu
2c5d984759 formatted and fixed linter errors 2021-06-26 11:40:19 +02:00
schubisu
441b6084dc skipping tag regions in text search 2021-06-26 11:15:59 +02:00
schubisu
36f765efb9 adding tabModeSearch to search in page 2021-05-30 19:50:26 +02:00
4 changed files with 132 additions and 0 deletions

View File

@ -254,6 +254,9 @@ func Init() error {
viper.SetDefault("keybindings.bind_copy_target_url", "c")
viper.SetDefault("keybindings.bind_beginning", []string{"Home", "g"})
viper.SetDefault("keybindings.bind_end", []string{"End", "G"})
viper.SetDefault("keybindings.bind_search", "/")
viper.SetDefault("keybindings.bind_next_match", "n")
viper.SetDefault("keybindings.bind_prev_match", "p")
viper.SetDefault("keybindings.shift_numbers", "")
viper.SetDefault("url-handlers.other", "default")
viper.SetDefault("cache.max_size", 0)

View File

@ -61,6 +61,9 @@ const (
CmdCopyTargetURL
CmdBeginning
CmdEnd
CmdSearch
CmdNextMatch
CmdPrevMatch
)
type keyBinding struct {
@ -198,6 +201,9 @@ func KeyInit() {
CmdCopyTargetURL: "keybindings.bind_copy_target_url",
CmdBeginning: "keybindings.bind_beginning",
CmdEnd: "keybindings.bind_end",
CmdSearch: "keybindings.bind_search",
CmdNextMatch: "keybindings.bind_next_match",
CmdPrevMatch: "keybindings.bind_prev_match",
}
// This is split off to allow shift_numbers to override bind_tab[1-90]
// (This is needed for older configs so that the default bind_tab values

View File

@ -30,6 +30,12 @@ var termH int
// The user input and URL display bar at the bottom
var bottomBar = cview.NewInputField()
var originalText []byte
var searchBar = cview.NewInputField()
var searchString = ""
var matches = 0
var curMatch = 0
// When the bottom bar string has a space, this regex decides whether it's
// a non-encoded URL or a search string.
// See this comment for details:
@ -257,6 +263,89 @@ func Init(version, commit, builtBy string) {
// Other potential keys are Tab and Backtab, they are ignored
})
searchBar.SetDoneFunc(func(key tcell.Key) {
tab := curTab
reset := func() {
searchBar.SetLabel("")
tabs[tab].applyAll()
App.SetFocus(tabs[tab].view)
layout.RemoveItem(searchBar)
tabs[tab].mode = tabModeDone
}
//nolint:exhaustive
switch key {
case tcell.KeyEnter:
// Figure out whether it's a URL, link number, or search
// And send out a request
// Escape the search string to not find regexp symbols
searchString = regexp.QuoteMeta(searchBar.GetText())
if strings.TrimSpace(searchString) == "" {
// Ignore
reset()
return
}
if tabs[tab].mode != tabModeSearch {
originalText = tabs[curTab].view.GetBytes(false)
}
tabs[tab].mode = tabModeSearch
// find all positions of the search string
searchRegex := regexp.MustCompile(searchString)
searchIdx := searchRegex.FindAllIndex(originalText, -1)
// find all positions of tags
tagsRegex := regexp.MustCompile(`\[.*?[^\[]\]`)
tagsIdx := tagsRegex.FindAllIndex(originalText, -1)
text := []byte("")
matches = 0
lastMatch := 0
var isMatch bool
// loops through all occurrences and check if they
// discard if they lie within tags.
// []byte text is build from the original text buffer
// with the actual search strings replaced by tagged regions
// to highlight.
for i, match := range searchIdx {
isMatch = true
for _, tag := range tagsIdx {
if match[0] >= tag[0] && match[1] <= tag[1] {
isMatch = false
break
}
}
if isMatch {
matches++
text = append(text, originalText[lastMatch:match[0]]...)
replacement := []byte(fmt.Sprint("[\"search-", i, "\"]", searchString, "[\"\"]"))
text = append(text, replacement...)
lastMatch = match[0] + len(searchString)
}
}
text = append(text, originalText[lastMatch:]...)
tabs[curTab].view.SetBytes(text)
curMatch = 0
tabs[curTab].view.Highlight(fmt.Sprint("search-", "0"))
tabs[curTab].view.ScrollToHighlight()
App.SetFocus(tabs[tab].view)
case tcell.KeyEsc:
// Set back to what it was
reset()
return
}
// Other potential keys are Tab and Backtab, they are ignored
})
// Render the default new tab content ONCE and store it for later
// This code is repeated in Reload()
newTabContent := getNewTabContent()
@ -309,6 +398,33 @@ func Init(version, commit, builtBy string) {
// keybinding in config/config.go and update the help panel in display/help.go
cmd := config.TranslateKeyEvent(event)
if tabs[curTab].mode == tabModeSearch {
switch cmd {
case config.CmdNextMatch:
if curMatch < (matches - 1) {
curMatch++
tabs[curTab].view.Highlight(fmt.Sprint("search-", curMatch))
}
tabs[curTab].view.ScrollToHighlight()
return nil
case config.CmdPrevMatch:
if curMatch > 0 {
curMatch--
tabs[curTab].view.Highlight(fmt.Sprint("search-", curMatch))
}
tabs[curTab].view.ScrollToHighlight()
return nil
case config.CmdInvalid:
if event.Key() == tcell.KeyEsc {
tabs[curTab].mode = tabModeDone
tabs[curTab].view.SetBytes(originalText)
layout.RemoveItem(searchBar)
return nil
}
}
return event
}
if tabs[curTab].mode == tabModeDone {
// All the keys and operations that can only work while NOT loading
//nolint:exhaustive
@ -326,6 +442,12 @@ func Init(version, commit, builtBy string) {
// Don't save bottom bar, so that whenever you switch tabs, it's not in that mode
App.SetFocus(bottomBar)
return nil
case config.CmdSearch:
layout.AddItem(searchBar, 2, 1, false)
searchBar.SetLabel("")
searchBar.SetText("")
App.SetFocus(searchBar)
return nil
case config.CmdEdit:
// Letter e allows to edit current URL
bottomBar.SetLabel("[::b]Edit URL: [::-]")

View File

@ -18,6 +18,7 @@ type tabMode int
const (
tabModeDone tabMode = iota
tabModeLoading
tabModeSearch
)
type tabHistory struct {