2016-08-13 12:49:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2017-09-17 11:48:27 +00:00
|
|
|
"io"
|
2022-02-06 16:43:01 +00:00
|
|
|
"io/ioutil"
|
2016-08-13 12:49:04 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
2016-10-29 23:20:35 +00:00
|
|
|
"strings"
|
2018-07-26 18:58:09 +00:00
|
|
|
"time"
|
2016-08-13 12:49:04 +00:00
|
|
|
|
2020-12-04 20:59:12 +00:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2016-08-13 12:49:04 +00:00
|
|
|
)
|
|
|
|
|
2017-11-19 18:55:13 +00:00
|
|
|
func run() {
|
2020-09-01 12:42:44 +00:00
|
|
|
var screen tcell.Screen
|
|
|
|
var err error
|
|
|
|
if screen, err = tcell.NewScreen(); err != nil {
|
|
|
|
log.Fatalf("creating screen: %s", err)
|
|
|
|
} else if err = screen.Init(); err != nil {
|
|
|
|
log.Fatalf("initializing screen: %s", err)
|
2019-02-18 11:18:02 +00:00
|
|
|
}
|
2021-01-29 16:36:27 +00:00
|
|
|
if gOpts.mouse {
|
|
|
|
screen.EnableMouse()
|
|
|
|
}
|
2019-02-18 11:18:02 +00:00
|
|
|
|
2022-02-06 16:43:01 +00:00
|
|
|
if gLogPath != "" {
|
|
|
|
f, err := os.OpenFile(gLogPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
log.SetOutput(f)
|
|
|
|
} else {
|
|
|
|
log.SetOutput(ioutil.Discard)
|
2022-01-22 10:47:18 +00:00
|
|
|
}
|
2016-08-13 12:49:04 +00:00
|
|
|
|
|
|
|
log.Print("hi!")
|
|
|
|
|
2021-02-21 14:48:23 +00:00
|
|
|
ui := newUI(screen)
|
|
|
|
nav := newNav(ui.wins[0].h)
|
|
|
|
app := newApp(ui, nav)
|
2016-08-13 12:49:04 +00:00
|
|
|
|
2021-05-19 20:38:05 +00:00
|
|
|
if err := nav.sync(); err != nil {
|
|
|
|
app.ui.echoerrf("sync: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:09:26 +00:00
|
|
|
if err := app.nav.readMarks(); err != nil {
|
2019-02-28 18:58:14 +00:00
|
|
|
app.ui.echoerrf("reading marks file: %s", err)
|
2018-07-11 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 17:49:55 +00:00
|
|
|
if err := app.readHistory(); err != nil {
|
2019-02-28 18:58:14 +00:00
|
|
|
app.ui.echoerrf("reading history file: %s", err)
|
2018-07-11 17:49:55 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 18:55:13 +00:00
|
|
|
app.loop()
|
2021-02-21 14:48:23 +00:00
|
|
|
|
2020-09-01 12:42:44 +00:00
|
|
|
app.ui.screen.Fini()
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 18:55:13 +00:00
|
|
|
func readExpr() <-chan expr {
|
2016-12-17 21:47:37 +00:00
|
|
|
ch := make(chan expr)
|
2016-10-29 23:20:35 +00:00
|
|
|
|
|
|
|
go func() {
|
2018-07-26 18:58:09 +00:00
|
|
|
duration := 1 * time.Second
|
|
|
|
|
2017-11-19 18:55:13 +00:00
|
|
|
c, err := net.Dial(gSocketProt, gSocketPath)
|
2018-07-26 18:58:09 +00:00
|
|
|
for err != nil {
|
2019-08-12 11:52:27 +00:00
|
|
|
log.Printf("connecting server: %s", err)
|
2018-07-26 18:58:09 +00:00
|
|
|
time.Sleep(duration)
|
|
|
|
duration *= 2
|
2018-07-26 19:11:05 +00:00
|
|
|
c, err = net.Dial(gSocketProt, gSocketPath)
|
2017-11-19 18:55:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-17 21:47:37 +00:00
|
|
|
fmt.Fprintf(c, "conn %d\n", gClientID)
|
2016-10-29 23:20:35 +00:00
|
|
|
|
2018-04-12 15:04:37 +00:00
|
|
|
ch <- &callExpr{"sync", nil, 1}
|
2017-11-19 18:55:13 +00:00
|
|
|
|
2016-10-29 23:20:35 +00:00
|
|
|
s := bufio.NewScanner(c)
|
|
|
|
for s.Scan() {
|
2016-11-06 14:06:25 +00:00
|
|
|
log.Printf("recv: %s", s.Text())
|
2016-10-29 23:20:35 +00:00
|
|
|
p := newParser(strings.NewReader(s.Text()))
|
|
|
|
if p.parse() {
|
|
|
|
ch <- p.expr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2019-02-26 18:27:04 +00:00
|
|
|
func remote(cmd string) error {
|
2017-07-20 22:26:36 +00:00
|
|
|
c, err := net.Dial(gSocketProt, gSocketPath)
|
2016-11-10 20:25:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dialing to send server: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:38:38 +00:00
|
|
|
fmt.Fprintln(c, cmd)
|
2016-11-10 20:25:03 +00:00
|
|
|
|
2017-09-17 11:48:27 +00:00
|
|
|
// XXX: Standard net.Conn interface does not include a CloseWrite method
|
|
|
|
// but net.UnixConn and net.TCPConn implement it so the following should be
|
|
|
|
// safe as long as we do not use other types of connections. We need
|
|
|
|
// CloseWrite to notify the server that this is not a persistent connection
|
|
|
|
// and it should be closed after the response.
|
|
|
|
if v, ok := c.(interface {
|
|
|
|
CloseWrite() error
|
|
|
|
}); ok {
|
|
|
|
v.CloseWrite()
|
|
|
|
}
|
|
|
|
|
|
|
|
io.Copy(os.Stdout, c)
|
|
|
|
|
|
|
|
c.Close()
|
|
|
|
|
2016-11-10 20:25:03 +00:00
|
|
|
return nil
|
|
|
|
}
|