Skip to content

Commit

Permalink
feat(qr): add Reversed option
Browse files Browse the repository at this point in the history
* Add --reverse option (fix #287)

This inverts the string-based QR code output to work on terminals
with light backgrounds

* Change --reverse flag to --reversed

* Add `reversed` configuration option

* fix(config_test): add missing field

---------

Co-authored-by: Claudio d'Angelis <claudiodangelis@gmail.com>
  • Loading branch information
sbliven and claudiodangelis authored Dec 6, 2023
1 parent 6966e7e commit dc7a48d
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ To configure `qrcp` you can create a configuration file inside `$XDG_CONFIG_HOME

> Note: On Linux, the `$XDG_CONFIG_HOME` is `.config` under user home directory.
> So, for example, on Linux the configuration file will be `$HOME/.config/qrcp/config.yml`.
> On MacOS, it defaults to `$HOME/Library/Application Support/qrcp/config.yml``
> Note: Starting from version 0.10.0, qrcp uses a YAML configuration file instead of the old JSON one. You can automatically migrate the legacy JSON format to the new YAML format by running `qrcp config migrate`.
Expand All @@ -214,6 +215,7 @@ To configure `qrcp` you can create a configuration file inside `$XDG_CONFIG_HOME
| `secure` | Bool | Controls whether `qrcp` should use HTTPS instead of HTTP. Defaults to `false` |
| `tls-cert` | String | Path to the TLS certificate. It's only used when `secure: true`. |
| `tls-key` | String | Path to the TLS key. It's only used when `secure: true`. |
| `reversed` | Bool | Reverse QR code (black text on white background)?" true`. |


All the configuration parameters can be controlled via environment variables prefixed with `QRCP_`, for example:
Expand Down
1 change: 1 addition & 0 deletions application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Flags struct {
TlsCert string
TlsKey string
Output string
Reversed bool
}

type App struct {
Expand Down
1 change: 1 addition & 0 deletions cmd/qrcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&app.Flags.Secure, "secure", "s", false, "use https connection")
rootCmd.PersistentFlags().StringVar(&app.Flags.TlsCert, "tls-cert", "", "path to TLS certificate to use with HTTPS")
rootCmd.PersistentFlags().StringVar(&app.Flags.TlsKey, "tls-key", "", "path to TLS private key to use with HTTPS")
rootCmd.PersistentFlags().BoolVarP(&app.Flags.Reversed, "reversed", "r", false, "Reverse QR code (black text on white background)")
// Receive command flags
receiveCmd.PersistentFlags().StringVarP(&app.Flags.Output, "output", "o", "", "output directory for receiving files")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func receiveCmdFunc(command *cobra.Command, args []string) error {
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)
qr.RenderString(srv.ReceiveURL, cfg.Reversed)
if app.Flags.Browser {
srv.DisplayQR(srv.ReceiveURL)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func sendCmdFunc(command *cobra.Command, args []string) error {
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(srv.SendURL)
qr.RenderString(srv.SendURL)
qr.RenderString(srv.SendURL, cfg.Reversed)
if app.Flags.Browser {
srv.DisplayQR(srv.SendURL)
}
Expand Down
15 changes: 15 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Config struct {
TlsCert string `yaml:",omitempty"`
FQDN string `yaml:",omitempty"`
Output string `yaml:",omitempty"`
Reversed bool `yaml:",omitempty"`
}

var interactive bool = false
Expand Down Expand Up @@ -61,6 +62,7 @@ func New(app application.App) Config {
cfg.TlsCert = v.GetString("tls-cert")
cfg.FQDN = v.GetString("fqdn")
cfg.Output = v.GetString("output")
cfg.Reversed = v.GetBool("reversed")

// Override
if app.Flags.Interface != "" {
Expand Down Expand Up @@ -93,6 +95,9 @@ func New(app application.App) Config {
if app.Flags.Output != "" {
cfg.Output = app.Flags.Output
}
if app.Flags.Reversed {
cfg.Reversed = true
}

// Discover interface if it's not been set yet
if !interactive {
Expand Down Expand Up @@ -306,6 +311,16 @@ func Wizard(app application.App) error {
v.Set("output", output)
}
}
promptReversed := promptui.Select{
Items: []string{"No", "Yes"},
Label: "Reverse QR code (black text on white background)?",
}
if _, promptReversedResultString, err := promptReversed.Run(); err == nil {
if promptReversedResultString == "Yes" {
v.Set("reversed", true)
}
cfg.Reversed = v.GetBool("reversed")
}

return v.WriteConfig()
}
2 changes: 2 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func TestNew(t *testing.T) {
TlsCert: "/path/to/cert",
FQDN: "mylan.com",
Output: "/path/to/default/output/dir",
Reversed: true,
},
},
{
Expand All @@ -118,6 +119,7 @@ func TestNew(t *testing.T) {
TlsCert: "/path/to/cert",
FQDN: "mylan.com",
Output: "/path/to/default/output/dir",
Reversed: true,
},
},
}
Expand Down
1 change: 1 addition & 0 deletions config/testdata/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ tls-key: /path/to/key
tls-cert: /path/to/cert
fqdn: mylan.com
output: /path/to/default/output/dir
reversed: true
7 changes: 7 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ qrcp --tls-cert /path/to/cert.pem --tls-key /path/to/cert.key MyDocument

A `--secure` flag is available too, you can use it to override the default value.

### Color scheme

By default, `qrcp`` is configured for terminals with light text on a dark background. Terminals with a light color scheme can invert this with the `--reversed` or `-r` flag.

```sh
qrcp --reversed MyDocument.pdf
```

### Open in browser

Expand Down
4 changes: 2 additions & 2 deletions qr/qr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
)

// RenderString as a QR code
func RenderString(s string) {
func RenderString(s string, inverseColor bool) {
q, err := qrcode.New(s, qrcode.Medium)
if err != nil {
log.Fatal(err)
}
fmt.Println(q.ToSmallString(false))
fmt.Println(q.ToSmallString(inverseColor))
}

// RenderImage returns a QR code as an image.Image
Expand Down

0 comments on commit dc7a48d

Please sign in to comment.