2016-08-13 12:49:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2017-09-17 11:48:27 +00:00
|
|
|
"io"
|
2016-08-13 12:49:04 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
2016-10-29 23:20:35 +00:00
|
|
|
"strings"
|
2016-08-13 12:49:04 +00:00
|
|
|
|
|
|
|
"github.com/nsf/termbox-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
func client() {
|
|
|
|
logFile, err := os.Create(gLogPath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-11-22 18:01:29 +00:00
|
|
|
defer os.Remove(gLogPath)
|
2016-08-13 12:49:04 +00:00
|
|
|
defer logFile.Close()
|
|
|
|
log.SetOutput(logFile)
|
|
|
|
|
|
|
|
log.Print("hi!")
|
|
|
|
|
|
|
|
if err := termbox.Init(); err != nil {
|
|
|
|
log.Fatalf("initializing termbox: %s", err)
|
|
|
|
}
|
|
|
|
defer termbox.Close()
|
|
|
|
|
2016-10-27 19:24:42 +00:00
|
|
|
app := newApp()
|
2016-08-13 12:49:04 +00:00
|
|
|
|
2016-10-16 11:19:19 +00:00
|
|
|
app.ui.loadFile(app.nav)
|
|
|
|
|
2017-02-11 10:42:16 +00:00
|
|
|
var serverChan <-chan expr
|
|
|
|
|
2017-07-20 22:26:36 +00:00
|
|
|
c, err := net.Dial(gSocketProt, gSocketPath)
|
2017-02-11 10:42:16 +00:00
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("connecting server: %s", err)
|
|
|
|
app.ui.message = msg
|
|
|
|
log.Printf(msg)
|
|
|
|
} else {
|
|
|
|
serverChan = readExpr(c)
|
|
|
|
}
|
|
|
|
|
2016-11-10 20:43:54 +00:00
|
|
|
if err := app.nav.sync(); err != nil {
|
|
|
|
msg := fmt.Sprintf("sync: %s", err)
|
|
|
|
app.ui.message = msg
|
|
|
|
log.Printf(msg)
|
|
|
|
}
|
|
|
|
|
2016-08-17 19:35:18 +00:00
|
|
|
if _, err := os.Stat(gConfigPath); err == nil {
|
|
|
|
log.Printf("reading configuration file: %s", gConfigPath)
|
|
|
|
|
|
|
|
rcFile, err := os.Open(gConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("opening configuration file: %s", err)
|
|
|
|
app.ui.message = msg
|
|
|
|
log.Printf(msg)
|
|
|
|
}
|
|
|
|
defer rcFile.Close()
|
|
|
|
|
|
|
|
p := newParser(rcFile)
|
|
|
|
for p.parse() {
|
|
|
|
p.expr.eval(app, nil)
|
|
|
|
}
|
2016-10-16 18:27:40 +00:00
|
|
|
|
|
|
|
if p.err != nil {
|
|
|
|
app.ui.message = p.err.Error()
|
|
|
|
log.Print(p.err)
|
|
|
|
}
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.ui.draw(app.nav)
|
|
|
|
|
2017-02-11 10:42:16 +00:00
|
|
|
app.handleInp(serverChan)
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
2016-12-18 15:01:45 +00:00
|
|
|
func readExpr(c net.Conn) <-chan expr {
|
2016-12-17 21:47:37 +00:00
|
|
|
ch := make(chan expr)
|
2016-10-29 23:20:35 +00:00
|
|
|
|
|
|
|
go func() {
|
2016-12-17 21:47:37 +00:00
|
|
|
fmt.Fprintf(c, "conn %d\n", gClientID)
|
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
|
|
|
|
}
|
|
|
|
|
2016-11-06 18:32:14 +00:00
|
|
|
func saveFiles(list []string, copy bool) error {
|
2017-07-20 22:26:36 +00:00
|
|
|
c, err := net.Dial(gSocketProt, gSocketPath)
|
2016-08-13 12:49:04 +00: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 20:55:44 +00:00
|
|
|
fmt.Fprintln(c, "save")
|
2016-08-13 12:49:04 +00:00
|
|
|
|
2016-11-06 18:32:14 +00:00
|
|
|
if copy {
|
2017-07-24 20:55:44 +00:00
|
|
|
fmt.Fprintln(c, "copy")
|
2016-08-13 12:49:04 +00:00
|
|
|
} else {
|
2017-07-24 20:55:44 +00:00
|
|
|
fmt.Fprintln(c, "move")
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 20:55:44 +00:00
|
|
|
for _, f := range list {
|
|
|
|
fmt.Fprintln(c, f)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(c)
|
2016-08-13 12:49:04 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-06 18:32:14 +00:00
|
|
|
func loadFiles() (list []string, copy bool, err error) {
|
2017-07-20 22:26:36 +00:00
|
|
|
c, e := net.Dial(gSocketProt, gSocketPath)
|
2016-08-13 12:49:04 +00: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 18:32:14 +00:00
|
|
|
s.Scan()
|
|
|
|
|
2017-07-24 20:55:44 +00:00
|
|
|
switch s.Text() {
|
2016-11-06 18:32:14 +00:00
|
|
|
case "copy":
|
|
|
|
copy = true
|
2016-08-13 12:49:04 +00:00
|
|
|
case "move":
|
2016-11-06 18:32:14 +00:00
|
|
|
copy = false
|
2016-08-13 12:49:04 +00:00
|
|
|
default:
|
2017-07-24 20:55:44 +00:00
|
|
|
err = fmt.Errorf("unexpected option to copy file(s): %s", s.Text())
|
2016-08-13 12:49:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-24 20:55:44 +00:00
|
|
|
for s.Scan() && s.Text() != "" {
|
|
|
|
list = append(list, s.Text())
|
|
|
|
}
|
2016-08-13 12:49:04 +00: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 20:25:03 +00:00
|
|
|
|
2016-12-19 18:28:57 +00:00
|
|
|
func sendRemote(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
|
|
|
|
}
|