Skip to content

Commit

Permalink
Fixed buggy "q to exit" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiodangelis committed Apr 19, 2021
2 parents f07a891 + b33dd93 commit 779c812
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
17 changes: 16 additions & 1 deletion cmd/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/claudiodangelis/qrcp/logger"
"github.com/claudiodangelis/qrcp/qr"
"github.com/claudiodangelis/qrcp/server"
"github.com/eiannone/keyboard"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -36,13 +37,27 @@ func receiveCmdFunc(command *cobra.Command, args []string) error {
return err
}
// Prints the URL to scan to screen
log.Print("Scan the following URL with a QR reader to start the file transfer, press CTRL+C or q to exit:")
log.Print(`Scan the following URL with a QR reader to start the file transfer, press CTRL+C or "q" to exit:`)
log.Print(srv.ReceiveURL)
// Renders the QR
qr.RenderString(srv.ReceiveURL)
if browserFlag {
srv.DisplayQR(srv.ReceiveURL)
}
if err := keyboard.Open(); err != nil {
panic(err)
}
defer func() {
keyboard.Close()
}()
go func() {
for {
char, _, _ := keyboard.GetKey()
if string(char) == "q" {
srv.Shutdown()
}
}
}()
if err := srv.Wait(); err != nil {
return err
}
Expand Down
18 changes: 16 additions & 2 deletions cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/claudiodangelis/qrcp/logger"
"github.com/claudiodangelis/qrcp/payload"
"github.com/claudiodangelis/qrcp/qr"
"github.com/eiannone/keyboard"

"github.com/claudiodangelis/qrcp/server"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -38,13 +39,26 @@ func sendCmdFunc(command *cobra.Command, args []string) error {
}
// Sets the payload
srv.Send(payload)
log.Print("Scan the following URL with a QR reader to start the file transfer, press CTRL+C or q to exit:")
log.Print(`Scan the following URL with a QR reader to start the file transfer, press CTRL+C or "q" to exit:`)
log.Print(srv.SendURL)
qr.RenderString(srv.SendURL)
if browserFlag {
srv.DisplayQR(srv.SendURL)
}

if err := keyboard.Open(); err != nil {
panic(err)
}
defer func() {
keyboard.Close()
}()
go func() {
for {
char, _, _ := keyboard.GetKey()
if string(char) == "q" {
srv.Shutdown()
}
}
}()
if err := srv.Wait(); err != nil {
return err
}
Expand Down
15 changes: 0 additions & 15 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"sync"

"github.com/claudiodangelis/qrcp/qr"
"github.com/eiannone/keyboard"

"github.com/claudiodangelis/qrcp/config"
"github.com/claudiodangelis/qrcp/pages"
Expand Down Expand Up @@ -170,20 +169,6 @@ func New(cfg *config.Config) (*Server, error) {
<-sig
app.stopChannel <- true
}()
if err := keyboard.Open(); err != nil {
panic(err)
}
defer func() {
keyboard.Close()
}()
go func() {
for {
char, _, _ := keyboard.GetKey()
if string(char) == "q" {
app.stopChannel <- true
}
}
}()
// The handler adds and removes from the sync.WaitGroup
// When the group is zero all requests are completed
// and the server is shutdown
Expand Down

0 comments on commit 779c812

Please sign in to comment.