summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go49
-rw-r--r--main_test.go53
2 files changed, 98 insertions, 4 deletions
diff --git a/main.go b/main.go
index 79a9d4c..d913835 100644
--- a/main.go
+++ b/main.go
@@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
+ "strings"
"time"
"fyne.io/fyne/v2"
@@ -25,6 +26,13 @@ const defaultDirectory = "."
var windowSize = fyne.NewSize(400, 100)
+type sharedTextLoadMode int
+
+const (
+ sharedTextLoadPrefill sharedTextLoadMode = iota
+ sharedTextLoadAutoLog
+)
+
// logEntry writes text to a timestamped markdown file in dir.
// Separates persistence logic from the UI so it can be tested independently.
func logEntry(dir, text string) error {
@@ -32,6 +40,18 @@ func logEntry(dir, text string) error {
return os.WriteFile(filename, []byte(text), 0o644)
}
+// prepareSharedTextLoad validates shared text and decides whether to prefill
+// the editor or log the entry immediately.
+func prepareSharedTextLoad(text string, autoLog bool) (sharedTextLoadMode, string, bool) {
+ if strings.TrimSpace(text) == "" {
+ return sharedTextLoadPrefill, "", false
+ }
+ if autoLog {
+ return sharedTextLoadAutoLog, text, true
+ }
+ return sharedTextLoadPrefill, text, true
+}
+
// newInputWidget creates the multi-line text entry with platform-appropriate
// wrapping and row count settings.
func newInputWidget() *widget.Entry {
@@ -84,8 +104,13 @@ func createMainWindow(a fyne.App) fyne.Window {
// Track whether the length warning has been shown so we don't fire a
// modal dialog on every keystroke above the limit.
warnShown := false
+ loadingSharedText := false
input.OnChanged = func(text string) {
charCount.SetText(fmt.Sprintf("%d chars", len(text)))
+ if loadingSharedText {
+ warnShown = false
+ return
+ }
if len(text) > maxTextLength && !warnShown {
warnShown = true
dialog.ShowInformation("Text Limit",
@@ -127,11 +152,27 @@ func createMainWindow(a fyne.App) fyne.Window {
}
return
}
- if txt != "" {
- input.SetText(txt)
- charCount.SetText(fmt.Sprintf("%d chars", len(txt)))
- window.Canvas().Focus(input)
+ mode, sharedText, ok := prepareSharedTextLoad(txt, a.Preferences().BoolWithFallback("AutoLogSharedText", false))
+ if !ok {
+ return
+ }
+ loadingSharedText = true
+ defer func() {
+ loadingSharedText = false
+ }()
+ if mode == sharedTextLoadAutoLog {
+ dir := a.Preferences().StringWithFallback("Directory", defaultDirectory)
+ if err := logEntry(dir, sharedText); err != nil {
+ dialog.ShowError(err, window)
+ return
+ }
+ dialog.ShowInformation("Logged", "Shared text has been logged.", window)
+ resetInput()
+ return
}
+ input.SetText(sharedText)
+ charCount.SetText(fmt.Sprintf("%d chars", len(sharedText)))
+ window.Canvas().Focus(input)
}
if fyne.CurrentDevice().IsMobile() {
diff --git a/main_test.go b/main_test.go
index 085dc24..8d6fac8 100644
--- a/main_test.go
+++ b/main_test.go
@@ -60,3 +60,56 @@ func TestLogEntryEmptyText(t *testing.T) {
t.Errorf("expected empty file, got %d bytes", len(content))
}
}
+
+func TestPrepareSharedTextLoadSkipsWhitespaceOnly(t *testing.T) {
+ mode, text, ok := prepareSharedTextLoad(" \n\t ", false)
+ if ok {
+ t.Fatal("expected whitespace-only text to be skipped")
+ }
+ if mode != sharedTextLoadPrefill {
+ t.Fatalf("expected prefill mode default, got %v", mode)
+ }
+ if text != "" {
+ t.Fatalf("expected empty text, got %q", text)
+ }
+}
+
+func TestPrepareSharedTextLoadPrefillMode(t *testing.T) {
+ mode, text, ok := prepareSharedTextLoad("hello", false)
+ if !ok {
+ t.Fatal("expected shared text to be accepted")
+ }
+ if mode != sharedTextLoadPrefill {
+ t.Fatalf("expected prefill mode, got %v", mode)
+ }
+ if text != "hello" {
+ t.Fatalf("expected original text, got %q", text)
+ }
+}
+
+func TestPrepareSharedTextLoadAutoLogMode(t *testing.T) {
+ mode, text, ok := prepareSharedTextLoad("hello", true)
+ if !ok {
+ t.Fatal("expected shared text to be accepted")
+ }
+ if mode != sharedTextLoadAutoLog {
+ t.Fatalf("expected auto-log mode, got %v", mode)
+ }
+ if text != "hello" {
+ t.Fatalf("expected original text, got %q", text)
+ }
+}
+
+func TestPrepareSharedTextLoadAllowsLongText(t *testing.T) {
+ text := strings.Repeat("x", maxTextLength+1)
+ mode, gotText, ok := prepareSharedTextLoad(text, true)
+ if !ok {
+ t.Fatal("expected long shared text to be accepted")
+ }
+ if mode != sharedTextLoadAutoLog {
+ t.Fatalf("expected auto-log mode, got %v", mode)
+ }
+ if gotText != text {
+ t.Fatalf("expected original text to be preserved, got %d bytes", len(gotText))
+ }
+}