summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/client/tui/tui.go40
-rw-r--r--internal/config/config.go28
-rw-r--r--internal/config/config_test.go23
3 files changed, 66 insertions, 25 deletions
diff --git a/internal/client/tui/tui.go b/internal/client/tui/tui.go
index da6cf44..9851ea9 100644
--- a/internal/client/tui/tui.go
+++ b/internal/client/tui/tui.go
@@ -2,7 +2,6 @@ package tui
import (
"fmt"
- "log"
config "codeberg.org/snonux/gos/internal/config/client"
tea "github.com/charmbracelet/bubbletea"
@@ -17,23 +16,26 @@ var style = lipgloss.NewStyle().
PaddingLeft(4).
Width(40)
-func Run(config config.ClientConfig) {
- p := tea.NewProgram(initModel())
- if _, err := p.Run(); err != nil {
- log.Fatal("error starting TUI:", err)
- }
+func Run(conf config.ClientConfig) error {
+ p := tea.NewProgram(initModel(conf))
+ _, err := p.Run()
+ return err
}
type model struct {
- choices []string
- cursor int
- selected map[int]struct{}
+ choices []string
+ cursor int
+ selected map[int]struct{}
+ conf config.ClientConfig
+ altscreenActive bool
+ err error
}
-func initModel() model {
+func initModel(conf config.ClientConfig) model {
return model{
choices: []string{"Compose post", "Schedule post"},
selected: make(map[int]struct{}),
+ conf: conf,
}
}
@@ -45,8 +47,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
- case "ctrl+c", "q":
- return m, tea.Quit
case "up", "k":
if m.cursor > 0 {
m.cursor--
@@ -62,6 +62,22 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} else {
m.selected[m.cursor] = struct{}{}
}
+ case "a":
+ m.altscreenActive = !m.altscreenActive
+ cmd := tea.EnterAltScreen
+ if !m.altscreenActive {
+ cmd = tea.ExitAltScreen
+ }
+ return m, cmd
+ case "e":
+ return m, openEditor(m.conf.Editor)
+ case "ctrl+c", "q":
+ return m, tea.Quit
+ }
+ case editorFinishedMsg:
+ if msg.err != nil {
+ m.err = msg.err
+ return m, tea.Quit
}
}
diff --git a/internal/config/config.go b/internal/config/config.go
index 784c34b..c05d553 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"io"
"os"
+ "unicode"
)
func FromFile[T any](configFile string) (T, error) {
@@ -25,17 +26,28 @@ func FromFile[T any](configFile string) (T, error) {
}
// Set config from envoronment variable if present, e.g. hansWurst from GOS_HANS_WURST
-func FromENV(envKey string, defaultValue ...string) string {
- if value := os.Getenv(envKey); value != "" {
- return value
- }
-
- // Use first non-empty default value.
- for _, value := range defaultValue {
- if value != "" {
+func FromENV(keys ...string) string {
+ for _, key := range keys{
+ if key == "" {
+ continue
+ }
+ if !isAllUpperCase(key) {
+ return key
+ }
+ if value := os.Getenv(key); value != "" {
return value
}
}
return ""
}
+
+func isAllUpperCase(s string) bool {
+ for _, r := range s {
+ if unicode.IsLetter(r) && !unicode.IsUpper(r) {
+ return false
+ }
+ }
+ return true
+}
+
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
index 2bd8fb6..a0a5228 100644
--- a/internal/config/config_test.go
+++ b/internal/config/config_test.go
@@ -17,7 +17,6 @@ func TestFromENV(t *testing.T) {
if got != expected {
t.Errorf("got '%s' but expected '%s'", got, expected)
- return
}
t.Logf("got '%s' as expected", expected)
@@ -25,13 +24,12 @@ func TestFromENV(t *testing.T) {
got = FromENV("GOS_JAJAJA", expected)
if got != expected {
t.Errorf("got '%s' but expected '%s'", got, expected)
- return
}
t.Logf("got '%s' as expected", expected)
- if got = FromENV("jujuju"); got != "" {
+ os.Unsetenv("JUJUJU_NOT_EXISTANT_ENV")
+ if got = FromENV("JUJUJU_NOT_EXISTANT_ENV"); got != "" {
t.Errorf("got '%s' but expected empty string", got)
- return
}
t.Logf("got empty string as expected")
@@ -39,7 +37,22 @@ func TestFromENV(t *testing.T) {
got = FromENV("GOS_WATCH", "", "", "", expected, "")
if got != expected {
t.Errorf("got '%s' but expected '%s'", got, expected)
- return
}
t.Logf("got '%s' as expected", expected)
}
+
+func TestSecondENV(t *testing.T) {
+ t.Parallel()
+
+ os.Unsetenv("GOS_NONEXISTANT")
+ os.Setenv("EDITOR", "hx")
+
+ var (
+ expected = "hx"
+ got = FromENV("GOS_NONEXISTANT", "EDITOR", "notepad.exe")
+ )
+
+ if expected != got {
+ t.Errorf("got '%s' but expected '%s'", got, expected)
+ }
+}