mirror of
https://github.com/makew0rld/amfora.git
synced 2024-09-11 05:55:27 +03:00
use existing bottomBar for search
This commit is contained in:
parent
eaf4460134
commit
1a59487098
@ -30,9 +30,10 @@ var termH int
|
|||||||
var bottomBar = cview.NewInputField()
|
var bottomBar = cview.NewInputField()
|
||||||
|
|
||||||
var originalText []byte
|
var originalText []byte
|
||||||
var searchBar = cview.NewInputField()
|
|
||||||
var tagsRegex = regexp.MustCompile(`\[[a-zA-Z0-9_,;: \-\."#]+[^\[]*\]`)
|
var tagsRegex = regexp.MustCompile(`\[[a-zA-Z0-9_,;: \-\."#]+[^\[]*\]`)
|
||||||
|
var searchMode = false
|
||||||
var searchString = ""
|
var searchString = ""
|
||||||
|
var bottomBarText = ""
|
||||||
var matches = 0
|
var matches = 0
|
||||||
var curMatch = 0
|
var curMatch = 0
|
||||||
|
|
||||||
@ -146,6 +147,11 @@ func Init(version, commit, builtBy string) {
|
|||||||
// Reset func to set the bottomBar back to what it was before
|
// Reset func to set the bottomBar back to what it was before
|
||||||
// Use for errors.
|
// Use for errors.
|
||||||
reset := func() {
|
reset := func() {
|
||||||
|
if searchMode {
|
||||||
|
tabs[tab].mode = tabModeDone
|
||||||
|
searchMode = false
|
||||||
|
bottomBar.SetText(bottomBarText)
|
||||||
|
}
|
||||||
bottomBar.SetLabel("")
|
bottomBar.SetLabel("")
|
||||||
tabs[tab].applyAll()
|
tabs[tab].applyAll()
|
||||||
App.SetFocus(tabs[tab].view)
|
App.SetFocus(tabs[tab].view)
|
||||||
@ -154,6 +160,68 @@ func Init(version, commit, builtBy string) {
|
|||||||
//nolint:exhaustive
|
//nolint:exhaustive
|
||||||
switch key {
|
switch key {
|
||||||
case tcell.KeyEnter:
|
case tcell.KeyEnter:
|
||||||
|
|
||||||
|
if searchMode {
|
||||||
|
// Escape the search string to not find regexp symbols
|
||||||
|
searchString = regexp.QuoteMeta(bottomBar.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
|
||||||
|
tagsIdx := tagsRegex.FindAllIndex(originalText, -1)
|
||||||
|
|
||||||
|
text := make([]byte, 0)
|
||||||
|
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)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Figure out whether it's a URL, link number, or search
|
// Figure out whether it's a URL, link number, or search
|
||||||
// And send out a request
|
// And send out a request
|
||||||
|
|
||||||
@ -260,87 +328,6 @@ func Init(version, commit, builtBy string) {
|
|||||||
// Other potential keys are Tab and Backtab, they are ignored
|
// 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
|
|
||||||
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
|
// Render the default new tab content ONCE and store it for later
|
||||||
// This code is repeated in Reload()
|
// This code is repeated in Reload()
|
||||||
@ -414,7 +401,9 @@ func Init(version, commit, builtBy string) {
|
|||||||
if event.Key() == tcell.KeyEsc {
|
if event.Key() == tcell.KeyEsc {
|
||||||
tabs[curTab].mode = tabModeDone
|
tabs[curTab].mode = tabModeDone
|
||||||
tabs[curTab].view.SetBytes(originalText)
|
tabs[curTab].view.SetBytes(originalText)
|
||||||
layout.RemoveItem(searchBar)
|
searchMode = false
|
||||||
|
bottomBar.SetText(bottomBarText)
|
||||||
|
bottomBar.SetLabel("")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -439,10 +428,11 @@ func Init(version, commit, builtBy string) {
|
|||||||
App.SetFocus(bottomBar)
|
App.SetFocus(bottomBar)
|
||||||
return nil
|
return nil
|
||||||
case config.CmdSearch:
|
case config.CmdSearch:
|
||||||
layout.AddItem(searchBar, 2, 1, false)
|
bottomBar.SetLabel("[::b]Search: [::-]")
|
||||||
searchBar.SetLabel("")
|
bottomBarText = bottomBar.GetText()
|
||||||
searchBar.SetText("")
|
bottomBar.SetText("")
|
||||||
App.SetFocus(searchBar)
|
searchMode = true
|
||||||
|
App.SetFocus(bottomBar)
|
||||||
return nil
|
return nil
|
||||||
case config.CmdEdit:
|
case config.CmdEdit:
|
||||||
// Letter e allows to edit current URL
|
// Letter e allows to edit current URL
|
||||||
|
Loading…
Reference in New Issue
Block a user