Autodetection of mode via TTY

This commit is contained in:
2022-01-15 18:20:31 +10:30
parent 7a1c344964
commit e3cfa6f309
5 changed files with 39 additions and 6 deletions

28
main.go
View File

@@ -4,6 +4,9 @@ import (
"flag"
"fmt"
"log"
"os"
"github.com/mattn/go-isatty"
)
func main() {
@@ -12,7 +15,8 @@ func main() {
addr := flag.String("a", "61.245.149.58", "address to connect to.")
isServer := flag.Bool("s", false, "Set if running the server.")
isList := flag.Bool("l", false, "Set if requesting a list")
isReceive := flag.Bool("p", false, "Set if receiving a file")
isSend := flag.Bool("c", false, "Set if sending a file (copy)")
isReceive := flag.Bool("p", false, "Set if receiving a file (paste)")
flag.Parse()
@@ -21,7 +25,27 @@ func main() {
s := Server{port: *port}
s.Run()
} else {
c := Client{port: *port, address: *addr, list: *isList, receive: *isReceive}
if !*isList && !*isSend && !*isReceive {
// try to work out the intent based on whether or not stdin/stdout
// are ttys
stdinTTY := isatty.IsTerminal(os.Stdin.Fd())
stdoutTTY := isatty.IsTerminal(os.Stdout.Fd())
if stdinTTY && !stdoutTTY {
*isReceive = true
} else if !stdinTTY && stdoutTTY {
*isSend = true
}
}
if !*isList && !*isSend && !*isReceive {
// could default to list?
*isList = true
}
c := Client{port: *port, address: *addr, list: *isList, send: *isSend, receive: *isReceive}
err := c.Connect()
if err != nil {
fmt.Print(err)