Initial commit
This commit is contained in:
commit
a8c64767e3
2 changed files with 108 additions and 0 deletions
3
go.mod
Normal file
3
go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
module vimjump
|
||||||
|
|
||||||
|
go 1.23.4
|
||||||
105
main.go
Normal file
105
main.go
Normal file
|
|
@ -0,0 +1,105 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
// Standard
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"flag"
|
||||||
|
)
|
||||||
|
|
||||||
|
const PORT = 13700
|
||||||
|
|
||||||
|
var (
|
||||||
|
svg string
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
flag.StringVar(&svg, "svg", "", "Path to SVG file")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if svg == "" {
|
||||||
|
fmt.Printf("Specify -svg\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
http.HandleFunc("/", HandleIndex)
|
||||||
|
http.HandleFunc("/index.svg", HandleIndexSvg)
|
||||||
|
http.HandleFunc("/jumpto", HandleJumpTo)
|
||||||
|
|
||||||
|
addr := fmt.Sprintf("[::]:%d", PORT)
|
||||||
|
fmt.Printf("Listening on %s\n", addr)
|
||||||
|
err = http.ListenAndServe(addr, nil)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func HandleIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
|
page := `<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<script type="text/javascript">
|
||||||
|
window.goto = (event, fname, search) => {
|
||||||
|
const instance = document.getElementById('instance').value
|
||||||
|
fetch(`+"`http://localhost:13700/jumpto?instance=${instance}&file=${fname}&search=${search}&split=${event.shiftKey ? 'yes' : 'no'}`"+`)
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<input type="text" id="instance" value="dev" style="width: 64px">
|
||||||
|
<div style="margin-top: 8px;">
|
||||||
|
<embed src="./index.svg" type="image/svg+xml"></embed>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`
|
||||||
|
|
||||||
|
w.Write([]byte(page))
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleIndexSvg(w http.ResponseWriter, r *http.Request) {
|
||||||
|
content, err := os.ReadFile(svg)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Reading SVG file (%s): %s\n", svg, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Add("Content-Type", "image/svg+xml")
|
||||||
|
w.Write(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleJumpTo(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Add("Access-Control-Allow-Origin", "*")
|
||||||
|
|
||||||
|
instance := "/tmp/"+r.URL.Query().Get("instance")
|
||||||
|
|
||||||
|
var cmd string
|
||||||
|
fname := r.URL.Query().Get("file")
|
||||||
|
split := r.URL.Query().Get("split")
|
||||||
|
|
||||||
|
if split == "yes" {
|
||||||
|
cmd = fmt.Sprintf(`<ESC>:call CodeSplit("%s")<CR>:set hlsearch<CR>`, fname)
|
||||||
|
} else {
|
||||||
|
cmd = fmt.Sprintf(`<ESC>:drop %s<CR>:set hlsearch<CR>`, fname)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
searchFor := r.URL.Query().Get("search")
|
||||||
|
if searchFor != "" {
|
||||||
|
cmd += fmt.Sprintf("/%s<CR>z<CR>", searchFor)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Remote command: %s\n", cmd)
|
||||||
|
vim := exec.Command("nvim", "--server", instance, "--remote-send", cmd)
|
||||||
|
err := vim.Run()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%s\n", err)
|
||||||
|
w.Write([]byte(err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write([]byte("OK"))
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue