105 lines
2.3 KiB
Go
105 lines
2.3 KiB
Go
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(`+"`${location.protocol}//${location.host}/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"))
|
|
}
|