Skip to content

Commit

Permalink
Merge pull request #290 from atc0005/refactor-and-pull-back-single-ap…
Browse files Browse the repository at this point in the history
…p-functionality

Refactor and pull back single app functionality
  • Loading branch information
atc0005 authored Apr 7, 2024
2 parents cc3093e + d651fd3 commit 2767ae5
Show file tree
Hide file tree
Showing 12 changed files with 716 additions and 333 deletions.
2 changes: 1 addition & 1 deletion cmd/dsl/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func pollInputSource(
// processInput processes given input replacing any Safe Links encoded URL
// with the original value. Other input is returned unmodified.
func processInput(txt string, resultsChan chan<- string, errChan chan<- error) {
log.Println("Calling safelinks.SafeLinkURLs(txt)")
log.Println("Calling safelinks.SafeLinkURLs")
safeLinks, err := safelinks.SafeLinkURLs(txt)

// Failing to find a URL in the input is considered OK. Other errors
Expand Down
7 changes: 4 additions & 3 deletions cmd/dslg/buttons.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/widget"
"github.com/atc0005/safelinks/internal/safelinks"
)

func newCopyButton(w fyne.Window, outputField *widget.Label) *widget.Button {
Expand All @@ -34,15 +35,15 @@ func newDecodeButton(inputField *widget.Entry, copyButton *widget.Button, errOut
log.Println("Decoding requested but no input text provided")

copyButton.Disable()
errOutField.Text = errOutTryAgain
errOutField.Text = errOutTryAgain + "\n"
errOutField.Refresh()

return
}

log.Println("Decoding provided input text")

result, err := decodeInput(inputField.Text)
result, err := safelinks.DecodeInput(inputField.Text)
switch {
case err != nil:
errOutField.Append(err.Error() + "\n")
Expand Down Expand Up @@ -104,7 +105,7 @@ func newAboutButton(_ fyne.Window, inputField *widget.Entry, copyButton *widget.
inputField.Text = ""
inputField.Refresh()

errOutField.Text = ""
errOutField.Text = "..."
errOutField.Refresh()

outputField.Text = "Current version:\n\n" + Version()
Expand Down
34 changes: 0 additions & 34 deletions cmd/dslg/decode.go

This file was deleted.

78 changes: 69 additions & 9 deletions cmd/eslg/buttons.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
"runtime"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"

"github.com/atc0005/safelinks/internal/safelinks"
)

func newCopyButton(w fyne.Window, outputField *widget.Label) *widget.Button {
Expand All @@ -27,21 +30,52 @@ func newCopyButton(w fyne.Window, outputField *widget.Label) *widget.Button {
return copyButton
}

func newEncodeButton(inputField *widget.Entry, copyButton *widget.Button, errOutField *widget.Entry, outputField *widget.Label) *widget.Button {
encodeButton := widget.NewButton("Encode", func() {
func newEncodeButton(randomEncode bool, inputField *widget.Entry, copyButton *widget.Button, errOutField *widget.Entry, outputField *widget.Label) *widget.Button {
buttonLabelText := func() string {
if randomEncode {
return "Encode Randomly"
}
return "Encode All"
}()

encodeButton := newProcessInputButton(
randomEncode,
buttonLabelText,
safelinks.EncodeInput,
inputField,
copyButton,
errOutField,
outputField,
)

return encodeButton
}

func newProcessInputButton(
// TODO: Refactor this to reduce parameters.
randomEscape bool,
buttonLabelText string,
processFunc func(string, bool) (string, error),
inputField *widget.Entry,
copyButton *widget.Button,
errOutField *widget.Entry,
outputField *widget.Label,
) *widget.Button {

button := widget.NewButton(buttonLabelText, func() {
if inputField.Text == "" {
log.Println("Encoding requested but no input text provided")
log.Printf("%s used but no input text provided", buttonLabelText)

copyButton.Disable()
errOutField.Text = errOutTryAgain
errOutField.Text = errOutTryAgain + "\n"
errOutField.Refresh()

return
}

log.Println("Encoding provided input text")
log.Printf("%s input text", buttonLabelText)

result, err := encodeInput(inputField.Text)
result, err := processFunc(inputField.Text, randomEscape)
switch {
case err != nil:
errOutField.Append(err.Error() + "\n")
Expand All @@ -61,9 +95,35 @@ func newEncodeButton(inputField *widget.Entry, copyButton *widget.Button, errOut
}
})

encodeButton.Importance = widget.HighImportance
if randomEscape {
button.Importance = widget.MediumImportance
button.Icon = theme.QuestionIcon()
} else {
button.Importance = widget.HighImportance
}

return encodeButton
return button
}

func newQueryEscapeButton(randomEscape bool, inputField *widget.Entry, copyButton *widget.Button, errOutField *widget.Entry, outputField *widget.Label) *widget.Button {
buttonLabelText := func() string {
if randomEscape {
return "QueryEscape Randomly"
}
return "QueryEscape All"
}()

queryEscapeButton := newProcessInputButton(
randomEscape,
buttonLabelText,
safelinks.QueryEscapeInput,
inputField,
copyButton,
errOutField,
outputField,
)

return queryEscapeButton
}

func newResetButton(w fyne.Window, inputField *widget.Entry, copyButton *widget.Button, errOutField *widget.Entry, outputField *widget.Label) *widget.Button {
Expand Down Expand Up @@ -101,7 +161,7 @@ func newAboutButton(_ fyne.Window, inputField *widget.Entry, copyButton *widget.
inputField.Text = ""
inputField.Refresh()

errOutField.Text = ""
errOutField.Text = "..."
errOutField.Refresh()

outputField.Text = "Current version:\n\n" + Version()
Expand Down
48 changes: 0 additions & 48 deletions cmd/eslg/encode.go

This file was deleted.

21 changes: 15 additions & 6 deletions cmd/eslg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
log.SetOutput(debugLoggingOut)

// Help this tool stand out from the dslg app.
if err := os.Setenv("FYNE_THEME", "light"); err != nil {
log.Println("Failed to set fyne toolkit theme")
}
// Help this tool stand out from the dslg app unless explicitly requested
// otherwise.
// if os.Getenv("FYNE_THEME") == "" {
// if err := os.Setenv("FYNE_THEME", "light"); err != nil {
// log.Println("Failed to set fyne toolkit theme")
// }
// }

// NOTE: This is deprecated and set to be removed in v3.0.
// fyne.CurrentApp().Settings().SetTheme(theme.LightTheme())
Expand All @@ -41,14 +44,20 @@ func main() {
output := NewOutputTextLabel()

copyButton := newCopyButton(w, output)
encodeButton := newEncodeButton(input, copyButton, errorOutput, output)
encodeAllButton := newEncodeButton(false, input, copyButton, errorOutput, output)
encodeRandomButton := newEncodeButton(true, input, copyButton, errorOutput, output)
queryEscapeAllButton := newQueryEscapeButton(false, input, copyButton, errorOutput, output)
queryEscapeRandomButton := newQueryEscapeButton(true, input, copyButton, errorOutput, output)
resetButton := newResetButton(w, input, copyButton, errorOutput, output)
aboutButton := newAboutButton(w, input, copyButton, errorOutput, output)

exitButton := newExitButton(a)

buttonRowContainer := NewButtonRowContainer(
encodeButton,
encodeAllButton,
encodeRandomButton,
queryEscapeAllButton,
queryEscapeRandomButton,
copyButton,
resetButton,
aboutButton,
Expand Down
6 changes: 3 additions & 3 deletions cmd/usl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
os.Exit(1)
}

input, err := safelinks.ReadURLsFromFile(f)
input, err := safelinks.ReadFromFile(f)
if err != nil {
fmt.Printf("Failed to read URLs from %q: %v\n", cfg.Filename, err)
os.Exit(1)
Expand All @@ -44,7 +44,7 @@ func main() {
inputURLs = input

default:
input, err := safelinks.ProcessInputAsURL(cfg.URL)
input, err := ReadURLsFromInput(cfg.URL)
if err != nil {
fmt.Printf("Failed to parse input as URL: %v\n", err)
os.Exit(1)
Expand All @@ -53,7 +53,7 @@ func main() {
inputURLs = input
}

hasErr := safelinks.ProcessInputURLs(inputURLs, os.Stdout, os.Stderr, cfg.Verbose)
hasErr := ProcessInputURLs(inputURLs, os.Stdout, os.Stderr, cfg.Verbose)

// Ensure unsuccessful error code if one encountered.
if hasErr {
Expand Down
2 changes: 1 addition & 1 deletion internal/safelinks/output.go → cmd/usl/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.

package safelinks
package main

import (
"fmt"
Expand Down
Loading

0 comments on commit 2767ae5

Please sign in to comment.