use single line strings for file list save and load
This commit is contained in:
1 parent
b83833ed1c
commit
23f89f7c7f
3 files changed
+43
-60
No files matched your search
@@ -78,7 +78,7 @@ func readExpr(c net.Conn) chan Expr {
|
||||
return ch
|
||||
}
|
||||
|
||||
func saveFiles(list []string, keep bool) error {
|
||||
func saveFiles(list []string, copy bool) error {
|
||||
c, err := net.Dial("unix", gSocketPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("dialing to save files: %s", err)
|
||||
@@ -87,22 +87,20 @@ func saveFiles(list []string, keep bool) error {
|
||||
|
||||
log.Printf("saving files: %v", list)
|
||||
|
||||
fmt.Fprintln(c, "save")
|
||||
fmt.Fprint(c, "save ")
|
||||
|
||||
if keep {
|
||||
fmt.Fprintln(c, "keep")
|
||||
if copy {
|
||||
fmt.Fprint(c, "copy ")
|
||||
} else {
|
||||
fmt.Fprintln(c, "move")
|
||||
fmt.Fprint(c, "move ")
|
||||
}
|
||||
|
||||
for _, f := range list {
|
||||
fmt.Fprintln(c, f)
|
||||
}
|
||||
fmt.Fprintln(c, strings.Join(list, ":"))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadFiles() (list []string, keep bool, err error) {
|
||||
func loadFiles() (list []string, copy bool, err error) {
|
||||
c, e := net.Dial("unix", gSocketPath)
|
||||
if e != nil {
|
||||
err = fmt.Errorf("dialing to load files: %s", e)
|
||||
@@ -114,19 +112,22 @@ func loadFiles() (list []string, keep bool, err error) {
|
||||
|
||||
s := bufio.NewScanner(c)
|
||||
|
||||
switch s.Scan(); s.Text() {
|
||||
case "keep":
|
||||
keep = true
|
||||
s.Scan()
|
||||
|
||||
word, rest := splitWord(s.Text())
|
||||
log.Printf("load: %s", s.Text())
|
||||
|
||||
switch word {
|
||||
case "copy":
|
||||
copy = true
|
||||
case "move":
|
||||
keep = false
|
||||
copy = false
|
||||
default:
|
||||
err = fmt.Errorf("unexpected option to keep file(s): %s", s.Text())
|
||||
err = fmt.Errorf("unexpected option to copy file(s): %s", word)
|
||||
return
|
||||
}
|
||||
|
||||
for s.Scan() {
|
||||
list = append(list, s.Text())
|
||||
}
|
||||
list = strings.Split(rest, ":")
|
||||
|
||||
if s.Err() != nil {
|
||||
err = fmt.Errorf("scanning file list: %s", s.Err())
|
||||
|
||||
@@ -420,14 +420,14 @@ func (nav *Nav) invert() {
|
||||
}
|
||||
}
|
||||
|
||||
func (nav *Nav) save(keep bool) error {
|
||||
func (nav *Nav) save(copy bool) error {
|
||||
if len(nav.marks) == 0 {
|
||||
curr, err := nav.currFile()
|
||||
if err != nil {
|
||||
return errors.New("no file selected")
|
||||
}
|
||||
|
||||
if err := saveFiles([]string{curr.Path}, keep); err != nil {
|
||||
if err := saveFiles([]string{curr.Path}, copy); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
@@ -436,7 +436,7 @@ func (nav *Nav) save(keep bool) error {
|
||||
fs = append(fs, f)
|
||||
}
|
||||
|
||||
if err := saveFiles(fs, keep); err != nil {
|
||||
if err := saveFiles(fs, copy); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -445,7 +445,7 @@ func (nav *Nav) save(keep bool) error {
|
||||
}
|
||||
|
||||
func (nav *Nav) put() error {
|
||||
list, keep, err := loadFiles()
|
||||
list, copy, err := loadFiles()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -459,7 +459,7 @@ func (nav *Nav) put() error {
|
||||
var args []string
|
||||
|
||||
var sh string
|
||||
if keep {
|
||||
if copy {
|
||||
sh = "cp"
|
||||
args = append(args, "-r")
|
||||
} else {
|
||||
|
||||
@@ -7,10 +7,11 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
gKeepFile bool
|
||||
gCopyFile bool
|
||||
gFileList []string
|
||||
gConnList map[int]net.Conn
|
||||
)
|
||||
@@ -55,11 +56,24 @@ func handleConn(c net.Conn) {
|
||||
word, rest := splitWord(s.Text())
|
||||
switch word {
|
||||
case "save":
|
||||
saveFilesServer(s)
|
||||
log.Printf("listen: save, list: %v, keep: %t", gFileList, gKeepFile)
|
||||
word2, rest2 := splitWord(rest)
|
||||
switch word2 {
|
||||
case "copy":
|
||||
gCopyFile = true
|
||||
case "move":
|
||||
gCopyFile = false
|
||||
default:
|
||||
log.Printf("unexpected option to copy file(s): %s", word2)
|
||||
break
|
||||
}
|
||||
gFileList = strings.Split(rest2, ":")
|
||||
case "load":
|
||||
loadFilesServer(c)
|
||||
log.Printf("listen: load, keep: %t", gKeepFile)
|
||||
if gCopyFile {
|
||||
fmt.Fprint(c, "copy ")
|
||||
} else {
|
||||
fmt.Fprint(c, "move ")
|
||||
}
|
||||
fmt.Fprintln(c, strings.Join(gFileList, ":"))
|
||||
case "conn":
|
||||
if rest != "" {
|
||||
word2, _ := splitWord(rest)
|
||||
@@ -91,40 +105,8 @@ func handleConn(c net.Conn) {
|
||||
}
|
||||
}
|
||||
|
||||
c.Close()
|
||||
}
|
||||
|
||||
func saveFilesServer(s *bufio.Scanner) {
|
||||
switch s.Scan(); s.Text() {
|
||||
case "keep":
|
||||
gKeepFile = true
|
||||
case "move":
|
||||
gKeepFile = false
|
||||
default:
|
||||
log.Printf("unexpected option to keep file(s): %s", s.Text())
|
||||
return
|
||||
}
|
||||
|
||||
gFileList = nil
|
||||
for s.Scan() {
|
||||
gFileList = append(gFileList, s.Text())
|
||||
}
|
||||
|
||||
if s.Err() != nil {
|
||||
log.Printf("scanning: %s", s.Err())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func loadFilesServer(c net.Conn) {
|
||||
if gKeepFile {
|
||||
fmt.Fprintln(c, "keep")
|
||||
} else {
|
||||
fmt.Fprintln(c, "move")
|
||||
}
|
||||
|
||||
for _, f := range gFileList {
|
||||
fmt.Fprintln(c, f)
|
||||
log.Printf("listening: %s", s.Err())
|
||||
}
|
||||
|
||||
c.Close()
|
||||
|
||||
Reference in new issue
Block a user