From 5e0d6522b03be4496406db1712ad700e62990835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niko=20K=C3=B6ser?= Date: Thu, 10 Jun 2021 14:12:05 +0200 Subject: [PATCH] refactor: accept enter in user prompts Until now, the process of receiving a file was aborted when an empty entry (Enter) was made. It is common under *nix that pressing Enter without prior input of other characters selects the default answer. This would also make the process more fluid for croc. In most usecases the default choice will be 'Y'/'Yes'. The only exception is the prompt for overwriting existing files. We default to 'N'/'No' in this prompt to prevent accidental overwriting of files. --- src/cli/cli.go | 6 +++--- src/croc/croc.go | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/cli/cli.go b/src/cli/cli.go index b66cb57..e199c24 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -116,8 +116,9 @@ func Run() (err error) { _, basename := filepath.Split(fpath) fnames = append(fnames, "'"+basename+"'") } - yn := utils.GetInput(fmt.Sprintf("Did you mean to send %s? (y/n) ", strings.Join(fnames, ", "))) - if strings.ToLower(yn) == "y" { + promptMessage := fmt.Sprintf("Did you mean to send %s? (Y/n) ", strings.Join(fnames, ", ")) + choice := strings.ToLower(utils.GetInput(promptMessage)) + if choice == "" || choice == "y" || choice == "yes" { return send(c) } } @@ -335,7 +336,6 @@ func makeTempFileWithString(s string) (fnames []string, err error) { } fnames = []string{f.Name()} return - } func getPaths(fnames []string) (paths []string, haveFolder bool, err error) { diff --git a/src/croc/croc.go b/src/croc/croc.go index 49ef403..ff936e4 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -812,11 +812,12 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error if !c.Options.NoPrompt || c.Options.Ask || senderInfo.Ask { if c.Options.Ask || senderInfo.Ask { machID, _ := machineid.ID() - fmt.Fprintf(os.Stderr, "\rYour machine id is '%s'.\n%s %s (%s) from '%s'? (y/n) ", machID, action, fname, utils.ByteCountDecimal(totalSize), senderInfo.MachineID) + fmt.Fprintf(os.Stderr, "\rYour machine id is '%s'.\n%s %s (%s) from '%s'? (Y/n) ", machID, action, fname, utils.ByteCountDecimal(totalSize), senderInfo.MachineID) } else { - fmt.Fprintf(os.Stderr, "\r%s %s (%s)? (y/n) ", action, fname, utils.ByteCountDecimal(totalSize)) + fmt.Fprintf(os.Stderr, "\r%s %s (%s)? (Y/n) ", action, fname, utils.ByteCountDecimal(totalSize)) } - if strings.ToLower(strings.TrimSpace(utils.GetInput(""))) != "y" { + choice := strings.ToLower(utils.GetInput("")) + if choice != "" && choice != "y" && choice != "yes" { err = message.Send(c.conn[0], c.Key, message.Message{ Type: "error", Message: "refusing files", @@ -1011,8 +1012,9 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) { c.Step3RecipientRequestFile = true if c.Options.Ask { - fmt.Fprintf(os.Stderr, "Send to machine '%s'? (y/n) ", remoteFile.MachineID) - if strings.ToLower(strings.TrimSpace(utils.GetInput(""))) != "y" { + fmt.Fprintf(os.Stderr, "Send to machine '%s'? (Y/n) ", remoteFile.MachineID) + choice := strings.ToLower(utils.GetInput("")) + if choice != "" && choice != "y" && choice != "yes" { err = message.Send(c.conn[0], c.Key, message.Message{ Type: "error", Message: "refusing files", @@ -1268,8 +1270,9 @@ func (c *Client) updateIfRecipientHasFileInfo() (err error) { log.Debugf("hashes are not equal %x != %x", fileHash, fileInfo.Hash) if errHash == nil && !c.Options.Overwrite && errRecipientFile == nil && !strings.HasPrefix(fileInfo.Name, "croc-stdin-") { log.Debug("asking to overwrite") - ans := utils.GetInput(fmt.Sprintf("\nOverwrite '%s'? (y/n) ", path.Join(fileInfo.FolderRemote, fileInfo.Name))) - if strings.TrimSpace(strings.ToLower(ans)) != "y" { + prompt := fmt.Sprintf("\nOverwrite '%s'? (y/N) ", path.Join(fileInfo.FolderRemote, fileInfo.Name)) + choice := strings.ToLower(utils.GetInput(prompt)) + if choice != "y" && choice != "yes" { fmt.Fprintf(os.Stderr, "skipping '%s'", path.Join(fileInfo.FolderRemote, fileInfo.Name)) continue } @@ -1516,7 +1519,6 @@ func (c *Client) sendData(i int) { c.Key, ) } else { - dataToSend, err = crypt.Encrypt( compress.Compress( append(posByte, data[:n]...),