From 62bf6743652819fd668d7b6972af64db5095427f Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Fri, 19 Oct 2018 06:36:45 -0700 Subject: [PATCH] add more info to win --- src/croc/croc.go | 1 + src/croc/recipient.go | 7 ++-- src/croc/sender.go | 7 ++-- src/win/main.go | 93 ++++++++++++++++++++++--------------------- 4 files changed, 55 insertions(+), 53 deletions(-) diff --git a/src/croc/croc.go b/src/croc/croc.go index 0382eb4d..11442c4c 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -57,6 +57,7 @@ type Croc struct { StateString string Bar *progressbar.ProgressBar FileInfo models.FileStats + OtherIP string // special for window WindowRecipientPrompt bool diff --git a/src/croc/recipient.go b/src/croc/recipient.go index fdde6dbe..f05a9f6c 100644 --- a/src/croc/recipient.go +++ b/src/croc/recipient.go @@ -47,7 +47,6 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string, var sessionKey []byte var transferTime time.Duration var hash256 []byte - var otherIP string var progressFile string var resumeFile bool var tcpConnections []comm.Comm @@ -103,8 +102,8 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string, switch step { case 0: // sender has initiated, sends their ip address - otherIP = string(message) - log.Debugf("sender IP: %s", otherIP) + cr.OtherIP = string(message) + log.Debugf("sender IP: %s", cr.OtherIP) // recipient begins by sending address ip := "" @@ -284,7 +283,7 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string, // start the ui for pgoress cr.StateString = "Recieving file..." bytesWritten := 0 - fmt.Fprintf(os.Stderr, "\nReceiving (<-%s)...\n", otherIP) + fmt.Fprintf(os.Stderr, "\nReceiving (<-%s)...\n", cr.OtherIP) cr.Bar = progressbar.NewOptions( int(cr.FileInfo.Size), progressbar.OptionSetRenderBlankState(true), diff --git a/src/croc/sender.go b/src/croc/sender.go index b954ec75..7e671115 100644 --- a/src/croc/sender.go +++ b/src/croc/sender.go @@ -48,7 +48,6 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL var f *os.File defer f.Close() // ignore the error if it wasn't opened :( var fileHash []byte - var otherIP string var startTransfer time.Time var tcpConnections []comm.Comm blocksToSkip := make(map[int64]struct{}) @@ -128,8 +127,8 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL c.WriteMessage(websocket.BinaryMessage, []byte(ip)) case 1: // first receive the IP address from the sender - otherIP = string(message) - log.Debugf("recipient IP: %s", otherIP) + cr.OtherIP = string(message) + log.Debugf("recipient IP: %s", cr.OtherIP) go func() { // recipient might want file! start gathering information about file @@ -357,7 +356,7 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL return errors.New("recipient refused file") } cr.StateString = "Transfer in progress..." - fmt.Fprintf(os.Stderr, "\rSending (->%s)...\n", otherIP) + fmt.Fprintf(os.Stderr, "\rSending (->%s)...\n", cr.OtherIP) // send file, compure hash simultaneously startTransfer = time.Now() diff --git a/src/win/main.go b/src/win/main.go index 5ec55495..8f1918e7 100644 --- a/src/win/main.go +++ b/src/win/main.go @@ -8,6 +8,7 @@ import ( "path/filepath" "time" + humanize "github.com/dustin/go-humanize" "github.com/schollz/croc/src/cli" "github.com/schollz/croc/src/croc" "github.com/schollz/croc/src/utils" @@ -82,12 +83,17 @@ func main() { go func(done chan bool) { for { - if cr.FileInfo.SentName != "" { - labels[0].UpdateTextFromGoroutine(fmt.Sprintf("Sending %s", cr.FileInfo.SentName)) + if cr.OtherIP != "" && cr.FileInfo.SentName != "" { + bytesString := humanize.Bytes(uint64(cr.FileInfo.Size)) + fileOrFolder := "file" + if cr.FileInfo.IsDir { + fileOrFolder = "folder" + } + labels[0].UpdateTextFromGoroutine(fmt.Sprintf("Sending %s %s '%s' to %s", bytesString, fileOrFolder, cr.FileInfo.SentName, cr.OtherIP)) } if cr.Bar != nil { barState := cr.Bar.State() - labels[1].UpdateTextFromGoroutine(fmt.Sprintf("%2.1f%% [%2.0f:%2.0f]", barState.CurrentPercent*100, barState.SecondsSince, barState.SecondsLeft)) + labels[1].UpdateTextFromGoroutine(fmt.Sprintf("%2.1f%% [%2.0fs:%2.0fs]", barState.CurrentPercent*100, barState.SecondsSince, barState.SecondsLeft)) } labels[2].UpdateTextFromGoroutine(cr.StateString) time.Sleep(100 * time.Millisecond) @@ -129,64 +135,61 @@ func main() { } var fn = folderDialog.SelectedFiles()[0] if len(fn) == 0 { - dialog(fmt.Sprintf("No folder selected")) + labels[2].SetText(fmt.Sprintf("No folder selected")) return } var codePhrase = widgets.QInputDialog_GetText(window, "croc", "Enter code phrase:", widgets.QLineEdit__Normal, "", true, core.Qt__Dialog, core.Qt__ImhNone) if len(codePhrase) < 3 { - dialog(fmt.Sprintf("Invalid codephrase: '%s'", codePhrase)) + labels[2].SetText(fmt.Sprintf("Invalid codephrase: '%s'", codePhrase)) return } + // change into the receiving directory cwd, _ := os.Getwd() + defer os.Chdir(cwd) + os.Chdir(fn) + cr := croc.Init(true) + cr.WindowRecipientPrompt = true + + done := make(chan bool) go func() { - os.Chdir(fn) - defer os.Chdir(cwd) - - cr := croc.Init(true) - cr.WindowRecipientPrompt = true - - done := make(chan bool) - - go func(done chan bool) { - for { - if cr.WindowReceivingString != "" { - var question = widgets.QMessageBox_Question(window, "croc", fmt.Sprintf("%s?", cr.WindowReceivingString), widgets.QMessageBox__Yes|widgets.QMessageBox__No, 0) - if question == widgets.QMessageBox__Yes { - cr.WindowRecipientAccept = true - labels[0].UpdateTextFromGoroutine(cr.WindowReceivingString) - } else { - cr.WindowRecipientAccept = false - labels[2].UpdateTextFromGoroutine("canceled") - return - } - cr.WindowRecipientPrompt = false - cr.WindowReceivingString = "" - } - - if cr.Bar != nil { - barState := cr.Bar.State() - labels[1].UpdateTextFromGoroutine(fmt.Sprintf("%2.1f%% [%2.0f:%2.0f]", barState.CurrentPercent*100, barState.SecondsSince, barState.SecondsLeft)) - } - labels[2].UpdateTextFromGoroutine(cr.StateString) - time.Sleep(100 * time.Millisecond) - select { - case _ = <-done: - labels[2].UpdateTextFromGoroutine(cr.StateString) - return - default: - continue - } - } - }(done) - cr.Receive(codePhrase) done <- true isWorking = false }() + for { + if cr.WindowReceivingString != "" { + var question = widgets.QMessageBox_Question(window, "croc", fmt.Sprintf("%s?", cr.WindowReceivingString), widgets.QMessageBox__Yes|widgets.QMessageBox__No, 0) + if question == widgets.QMessageBox__Yes { + cr.WindowRecipientAccept = true + labels[0].UpdateTextFromGoroutine(cr.WindowReceivingString) + } else { + cr.WindowRecipientAccept = false + labels[2].UpdateTextFromGoroutine("canceled") + return + } + cr.WindowRecipientPrompt = false + cr.WindowReceivingString = "" + } + + if cr.Bar != nil { + barState := cr.Bar.State() + labels[1].UpdateTextFromGoroutine(fmt.Sprintf("%2.1f%% [%2.0fs:%2.0fs]", barState.CurrentPercent*100, barState.SecondsSince, barState.SecondsLeft)) + } + labels[2].UpdateTextFromGoroutine(cr.StateString) + time.Sleep(100 * time.Millisecond) + select { + case _ = <-done: + labels[2].UpdateTextFromGoroutine(cr.StateString) + return + default: + continue + } + } + }) widget.Layout().AddWidget(receiveButton)