mirror of
https://github.com/makew0rld/amfora.git
synced 2024-06-01 18:31:08 +00:00
Compare commits
5 Commits
05468394c5
...
c3c69cecbe
Author | SHA1 | Date | |
---|---|---|---|
|
c3c69cecbe | ||
|
8c6950635a | ||
|
2c5d984759 | ||
|
441b6084dc | ||
|
36f765efb9 |
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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: [::-]")
|
||||
|
|
|
@ -18,6 +18,7 @@ type tabMode int
|
|||
const (
|
||||
tabModeDone tabMode = iota
|
||||
tabModeLoading
|
||||
tabModeSearch
|
||||
)
|
||||
|
||||
type tabHistory struct {
|
||||
|
|
Loading…
Reference in New Issue
Block a user