Adds search with terms inline, also gemini file rendering

This commit is contained in:
sloumdrone 2019-09-19 15:32:26 -07:00
parent 5114ac1a15
commit 0879592015
2 changed files with 44 additions and 31 deletions

View File

@ -277,7 +277,7 @@ func (c *client) simpleCommand(action string) {
c.BookMarks.ToggleOpen() c.BookMarks.ToggleOpen()
c.Draw() c.Draw()
case "SEARCH": case "SEARCH":
c.search() c.search("")
case "HELP", "?": case "HELP", "?":
go c.Visit(helplocation) go c.Visit(helplocation)
default: default:
@ -296,6 +296,8 @@ func (c *client) doCommand(action string, values []string) {
switch action { switch action {
case "CHECK", "C": case "CHECK", "C":
c.displayConfigValue(values[0]) c.displayConfigValue(values[0])
case "SEARCH":
c.search(strings.Join(values, " "))
default: default:
c.SetMessage(fmt.Sprintf("Unknown action %q", action), true) c.SetMessage(fmt.Sprintf("Unknown action %q", action), true)
c.DrawMessage() c.DrawMessage()
@ -505,21 +507,27 @@ func (c *client) doLinkCommand(action, target string) {
} }
func (c *client) search() { func (c *client) search(q string) {
c.ClearMessage() var entry string
c.ClearMessageLine() var err error
// TODO handle keeping the full command bar here if q == "" {
// like was done for regular command entry c.ClearMessage()
// maybe split into separate function c.ClearMessageLine()
fmt.Print("?") if c.Options["theme"] == "normal" {
entry, err := cui.GetLine() fmt.Printf("\033[7m%*.*s\r", c.Width, c.Width, "")
c.ClearMessageLine() }
if err != nil { fmt.Print("?")
c.SetMessage(err.Error(), true) entry, err = cui.GetLine()
c.DrawMessage() c.ClearMessageLine()
return if err != nil {
} else if strings.TrimSpace(entry) == "" { c.SetMessage(err.Error(), true)
return c.DrawMessage()
return
} else if strings.TrimSpace(entry) == "" {
return
}
} else {
entry = q
} }
u, err := MakeUrl(c.Options["searchengine"]) u, err := MakeUrl(c.Options["searchengine"])
if err != nil { if err != nil {
@ -614,7 +622,7 @@ func (c *client) displayConfigValue(setting string) {
func (c *client) SetMessage(msg string, isError bool) { func (c *client) SetMessage(msg string, isError bool) {
c.MessageIsErr = isError c.MessageIsErr = isError
c.Message = msg c.Message = strings.ReplaceAll(msg, "\t", "%09")
} }
func (c *client) DrawMessage() { func (c *client) DrawMessage() {
@ -682,7 +690,7 @@ func (c *client) goToLink(l string) {
func (c *client) SetHeaderUrl() { func (c *client) SetHeaderUrl() {
if c.PageState.Length > 0 { if c.PageState.Length > 0 {
u := c.PageState.History[c.PageState.Position].Location.Full u := c.PageState.History[c.PageState.Position].Location.Full
c.TopBar.url = u c.TopBar.url = strings.ReplaceAll(u, "\t", "%09")
} else { } else {
c.TopBar.url = "" c.TopBar.url = ""
} }
@ -692,6 +700,7 @@ func (c *client) Visit(url string) {
c.SetMessage("Loading...", false) c.SetMessage("Loading...", false)
c.DrawMessage() c.DrawMessage()
url = strings.ReplaceAll(url, "%09", "\t")
u, err := MakeUrl(url) u, err := MakeUrl(url)
if err != nil { if err != nil {
c.SetMessage(err.Error(), true) c.SetMessage(err.Error(), true)
@ -741,9 +750,6 @@ func (c *client) Visit(url string) {
c.DrawMessage() c.DrawMessage()
} }
} }
// c.SetMessage("Bombadillo has not mastered Gemini yet, check back soon", false)
// c.DrawMessage()
case "telnet": case "telnet":
c.SetMessage("Attempting to start telnet session", false) c.SetMessage("Attempting to start telnet session", false)
c.DrawMessage() c.DrawMessage()

View File

@ -131,19 +131,26 @@ func parseGemini(b, rootUrl string) (string, []string) {
for i, ln := range splitContent { for i, ln := range splitContent {
splitContent[i] = strings.Trim(ln, "\r\n") splitContent[i] = strings.Trim(ln, "\r\n")
if len([]rune(ln)) > 3 && ln[:2] == "=>" { if len([]rune(ln)) > 3 && ln[:2] == "=>" {
trimmedSubLn := strings.Trim(ln[2:], "\r\n\t \a") var link, decorator string
lineSplit := strings.SplitN(trimmedSubLn, " ", 2) subLn := strings.Trim(ln[2:], "\r\n\t \a")
if len(lineSplit) != 2 { splitPoint := strings.IndexAny(subLn, " \t")
lineSplit = append(lineSplit, lineSplit[0])
if splitPoint < 0 || len([]rune(subLn)) - 1 <= splitPoint {
link = subLn
decorator = subLn
} else {
link = strings.Trim(subLn[:splitPoint], "\t\n\r \a")
decorator = strings.Trim(subLn[splitPoint:], "\t\n\r \a")
} }
lineSplit[0] = strings.Trim(lineSplit[0], "\t\n\r \a")
lineSplit[1] = strings.Trim(lineSplit[1], "\t\n\r \a") if len(link) > 0 && link[0] == '/' {
if len(lineSplit[0]) > 0 && lineSplit[0][0] == '/' { link = fmt.Sprintf("%s%s", rootUrl, link)
lineSplit[0] = fmt.Sprintf("%s%s", rootUrl, lineSplit[0]) } else if len(link) > 0 && strings.Index(link, "://") < 0 {
link = fmt.Sprintf("%s/%s", rootUrl, link)
} }
links = append(links, lineSplit[0]) links = append(links, link)
linknum := fmt.Sprintf("[%d]", len(links)) linknum := fmt.Sprintf("[%d]", len(links))
splitContent[i] = fmt.Sprintf("%-5s %s", linknum, lineSplit[1]) splitContent[i] = fmt.Sprintf("%-5s %s", linknum, decorator)
} }
} }
return strings.Join(splitContent, "\n"), links return strings.Join(splitContent, "\n"), links