2016-08-13 15:49:04 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2017-09-17 14:48:27 +03:00
|
|
|
"io"
|
2016-08-13 15:49:04 +03:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
2016-10-30 02:20:35 +03:00
|
|
|
"strings"
|
2018-07-26 21:58:09 +03:00
|
|
|
"time"
|
2016-08-13 15:49:04 +03:00
|
|
|
|
|
|
|
"github.com/nsf/termbox-go"
|
|
|
|
)
|
|
|
|
|
2017-11-19 21:55:13 +03:00
|
|
|
func run() {
|
2019-02-18 14:18:02 +03:00
|
|
|
if err := termbox.Init(); err != nil {
|
|
|
|
log.Fatalf("initializing termbox: %s", err)
|
|
|
|
}
|
|
|
|
defer termbox.Close()
|
|
|
|
|
|
|
|
setColorMode()
|
|
|
|
|
2018-05-20 20:30:41 +03:00
|
|
|
f, err := os.Create(gLogPath)
|
2016-08-13 15:49:04 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-11-22 21:01:29 +03:00
|
|
|
defer os.Remove(gLogPath)
|
2018-05-20 20:30:41 +03:00
|
|
|
defer f.Close()
|
|
|
|
log.SetOutput(f)
|
2016-08-13 15:49:04 +03:00
|
|
|
|
|
|
|
log.Print("hi!")
|
|
|
|
|
2016-10-27 22:24:42 +03:00
|
|
|
app := newApp()
|
2016-08-13 15:49:04 +03:00
|
|
|
|
2018-07-11 20:09:26 +03:00
|
|
|
if err := app.nav.readMarks(); err != nil {
|
2019-02-28 21:58:14 +03:00
|
|
|
app.ui.echoerrf("reading marks file: %s", err)
|
2018-07-11 20:09:26 +03:00
|
|
|
}
|
|
|
|
|
2018-07-11 20:49:55 +03:00
|
|
|
if err := app.readHistory(); err != nil {
|
2019-02-28 21:58:14 +03:00
|
|
|
app.ui.echoerrf("reading history file: %s", err)
|
2018-07-11 20:49:55 +03:00
|
|
|
}
|
|
|
|
|
2017-11-19 21:55:13 +03:00
|
|
|
app.loop()
|
2016-08-13 15:49:04 +03:00
|
|
|
}
|
|
|
|
|
2017-11-19 21:55:13 +03:00
|
|
|
func readExpr() <-chan expr {
|
2016-12-18 00:47:37 +03:00
|
|
|
ch := make(chan expr)
|
2016-10-30 02:20:35 +03:00
|
|
|
|
|
|
|
go func() {
|
2018-07-26 21:58:09 +03:00
|
|
|
duration := 1 * time.Second
|
|
|
|
|
2017-11-19 21:55:13 +03:00
|
|
|
c, err := net.Dial(gSocketProt, gSocketPath)
|
2018-07-26 21:58:09 +03:00
|
|
|
for err != nil {
|
2017-11-19 21:55:13 +03:00
|
|
|
log.Printf(fmt.Sprintf("connecting server: %s", err))
|
2018-07-26 21:58:09 +03:00
|
|
|
time.Sleep(duration)
|
|
|
|
duration *= 2
|
2018-07-26 22:11:05 +03:00
|
|
|
c, err = net.Dial(gSocketProt, gSocketPath)
|
2017-11-19 21:55:13 +03:00
|
|
|
}
|
|
|
|
|
2016-12-18 00:47:37 +03:00
|
|
|
fmt.Fprintf(c, "conn %d\n", gClientID)
|
2016-10-30 02:20:35 +03:00
|
|
|
|
2018-04-12 18:04:37 +03:00
|
|
|
ch <- &callExpr{"sync", nil, 1}
|
2017-11-19 21:55:13 +03:00
|
|
|
|
2016-10-30 02:20:35 +03:00
|
|
|
s := bufio.NewScanner(c)
|
|
|
|
for s.Scan() {
|
2016-11-06 17:06:25 +03:00
|
|
|
log.Printf("recv: %s", s.Text())
|
2016-10-30 02:20:35 +03:00
|
|
|
p := newParser(strings.NewReader(s.Text()))
|
|
|
|
if p.parse() {
|
|
|
|
ch <- p.expr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2018-06-27 21:15:34 +03:00
|
|
|
func saveFiles(list []string, cp bool) error {
|
2017-07-21 01:26:36 +03:00
|
|
|
c, err := net.Dial(gSocketProt, gSocketPath)
|
2016-08-13 15:49:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dialing to save files: %s", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
log.Printf("saving files: %v", list)
|
|
|
|
|
2017-07-24 23:55:44 +03:00
|
|
|
fmt.Fprintln(c, "save")
|
2016-08-13 15:49:04 +03:00
|
|
|
|
2018-06-27 21:15:34 +03:00
|
|
|
if cp {
|
2017-07-24 23:55:44 +03:00
|
|
|
fmt.Fprintln(c, "copy")
|
2016-08-13 15:49:04 +03:00
|
|
|
} else {
|
2017-07-24 23:55:44 +03:00
|
|
|
fmt.Fprintln(c, "move")
|
2016-08-13 15:49:04 +03:00
|
|
|
}
|
|
|
|
|
2017-07-24 23:55:44 +03:00
|
|
|
for _, f := range list {
|
|
|
|
fmt.Fprintln(c, f)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(c)
|
2016-08-13 15:49:04 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-27 21:15:34 +03:00
|
|
|
func loadFiles() (list []string, cp bool, err error) {
|
2017-07-21 01:26:36 +03:00
|
|
|
c, e := net.Dial(gSocketProt, gSocketPath)
|
2016-08-13 15:49:04 +03:00
|
|
|
if e != nil {
|
|
|
|
err = fmt.Errorf("dialing to load files: %s", e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
fmt.Fprintln(c, "load")
|
|
|
|
|
|
|
|
s := bufio.NewScanner(c)
|
|
|
|
|
2016-11-06 21:32:14 +03:00
|
|
|
s.Scan()
|
|
|
|
|
2017-07-24 23:55:44 +03:00
|
|
|
switch s.Text() {
|
2016-11-06 21:32:14 +03:00
|
|
|
case "copy":
|
2018-06-27 21:15:34 +03:00
|
|
|
cp = true
|
2016-08-13 15:49:04 +03:00
|
|
|
case "move":
|
2018-06-27 21:15:34 +03:00
|
|
|
cp = false
|
2016-08-13 15:49:04 +03:00
|
|
|
default:
|
2017-07-24 23:55:44 +03:00
|
|
|
err = fmt.Errorf("unexpected option to copy file(s): %s", s.Text())
|
2016-08-13 15:49:04 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-24 23:55:44 +03:00
|
|
|
for s.Scan() && s.Text() != "" {
|
|
|
|
list = append(list, s.Text())
|
|
|
|
}
|
2016-08-13 15:49:04 +03:00
|
|
|
|
|
|
|
if s.Err() != nil {
|
|
|
|
err = fmt.Errorf("scanning file list: %s", s.Err())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("loading files: %v", list)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2016-11-10 23:25:03 +03:00
|
|
|
|
2019-02-26 21:27:04 +03:00
|
|
|
func remote(cmd string) error {
|
2017-07-21 01:26:36 +03:00
|
|
|
c, err := net.Dial(gSocketProt, gSocketPath)
|
2016-11-10 23:25:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dialing to send server: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-11-18 23:38:38 +03:00
|
|
|
fmt.Fprintln(c, cmd)
|
2016-11-10 23:25:03 +03:00
|
|
|
|
2017-09-17 14:48:27 +03: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 23:25:03 +03:00
|
|
|
return nil
|
|
|
|
}
|