You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2026/09/03 08:14:20 UTC
(plc4x-extras) 03/09: feat: Add the shared terminal theme and glyph sets for the Charm port
This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x-extras.git
commit 04a4c8e70a96f5b3cf33710e0721133c39858312
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Wed Sep 2 16:45:43 2026 +0200
feat: Add the shared terminal theme and glyph sets for the Charm port
Both plc4go tools are being rebuilt on Bubble Tea v2, and both need the same
presentation vocabulary, so it lives in one place rather than being written
twice.
The previous UIs wrote colour as raw tview markup inline, such as
"[#0000ff]" and a hardcoded "[white]" reset. That palette could not adapt to
the terminal, could not be turned off, and used a pure blue that is barely
legible on a dark background. tools/internal/tui replaces it with named
styles resolved from a light/dark palette via lipgloss v2's LightDark, plus
a NO_COLOR variant that emits no escape sequences at all -- which is also
what makes golden-file tests stable.
Glyphs are a set rather than literals at call sites, with a Unicode set as
the default and a full ASCII fallback selected from the locale or by an
explicit flag. Every ASCII glyph has the same display width as the Unicode
glyph it replaces, so switching sets cannot shift or clip a layout; that
property is enforced by a test rather than assumed. Writing that test caught
a real mistake: lipgloss.NormalBorder() is Unicode box-drawing, not ASCII,
so the fallback now uses lipgloss.ASCIIBorder().
Panes also gain a focused variant. Neither of the old UIs indicated which
pane had the keyboard, and in fact only one widget in each could ever
receive focus at all.
75 test cases cover glyph width parity, the ASCII set really being ASCII,
locale detection, light/dark actually differing, the NO_COLOR contract, and
focus being visually distinct.
---
plc4go/go.mod | 10 ++
plc4go/go.sum | 20 ++++
plc4go/tools/internal/tui/glyphs.go | 147 +++++++++++++++++++++++
plc4go/tools/internal/tui/theme.go | 202 +++++++++++++++++++++++++++++++
plc4go/tools/internal/tui/tui_test.go | 219 ++++++++++++++++++++++++++++++++++
5 files changed, 598 insertions(+)
diff --git a/plc4go/go.mod b/plc4go/go.mod
index a6cc5459..a5831540 100644
--- a/plc4go/go.mod
+++ b/plc4go/go.mod
@@ -22,6 +22,7 @@ module github.com/apache/plc4x-extras/plc4go
go 1.27
require (
+ charm.land/lipgloss/v2 v2.0.6
github.com/apache/plc4x/plc4go v0.0.0-20260902093211-931674134ff2
github.com/fatih/color v1.19.0
github.com/gdamore/tcell/v2 v2.13.10
@@ -38,7 +39,14 @@ require (
require (
github.com/bitfield/gotestdox v0.2.2 // indirect
+ github.com/charmbracelet/colorprofile v0.4.3 // indirect
+ github.com/charmbracelet/ultraviolet v0.0.0-20260811164956-006e29f97886 // indirect
+ github.com/charmbracelet/x/ansi v0.11.8 // indirect
+ github.com/charmbracelet/x/term v0.2.2 // indirect
+ github.com/charmbracelet/x/termios v0.1.1 // indirect
+ github.com/charmbracelet/x/windows v0.2.2 // indirect
github.com/chigopher/pathlib v0.19.1 // indirect
+ github.com/clipperhouse/displaywidth v0.11.0 // indirect
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
github.com/dnephin/pflag v1.0.7 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
@@ -57,6 +65,7 @@ require (
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
+ github.com/muesli/cancelreader v0.2.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
@@ -67,6 +76,7 @@ require (
github.com/stretchr/objx v0.5.3 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/vektra/mockery/v2 v2.53.2 // indirect
+ github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.yaml.in/yaml/v3 v3.0.5 // indirect
golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac // indirect
golang.org/x/mod v0.39.0 // indirect
diff --git a/plc4go/go.sum b/plc4go/go.sum
index 9b2c9317..f4add1c2 100644
--- a/plc4go/go.sum
+++ b/plc4go/go.sum
@@ -1,13 +1,29 @@
+charm.land/lipgloss/v2 v2.0.6 h1:EaGKeuA8FvF+v2BT5VmZd2LoYLaMZJXA5n34th8nCIQ=
+charm.land/lipgloss/v2 v2.0.6/go.mod h1:ipDDJNSGa1hlwDtSfW1s2/xR8Vdhbut4PXh2zEKZd0Q=
github.com/ajankovic/xdiff v0.0.1 h1:V1cj8t5xwYzm6ZGPqPOlAc9AIajXuTEn41D/1MJBWMM=
github.com/ajankovic/xdiff v0.0.1/go.mod h1:SUmEZ67uB97I0zkiuQ+lb+LOms9ipn8X+p+2RdJV710=
github.com/apache/plc4x/plc4go v0.0.0-20260902093211-931674134ff2 h1:AehpEo6hGU1fFryP9ZQtkwhiQom9jqbgB1Lhp1CBfjs=
github.com/apache/plc4x/plc4go v0.0.0-20260902093211-931674134ff2/go.mod h1:eXckuqFuXTmT05hN7BrZUIthBY96xSoXMXJNt//fHXo=
github.com/bitfield/gotestdox v0.2.2 h1:x6RcPAbBbErKLnapz1QeAlf3ospg8efBsedU93CDsnE=
github.com/bitfield/gotestdox v0.2.2/go.mod h1:D+gwtS0urjBrzguAkTM2wodsTQYFHdpx8eqRJ3N+9pY=
+github.com/charmbracelet/colorprofile v0.4.3 h1:QPa1IWkYI+AOB+fE+mg/5/4HRMZcaXex9t5KX76i20Q=
+github.com/charmbracelet/colorprofile v0.4.3/go.mod h1:/zT4BhpD5aGFpqQQqw7a+VtHCzu+zrQtt1zhMt9mR4Q=
+github.com/charmbracelet/ultraviolet v0.0.0-20260811164956-006e29f97886 h1:rdnVWKgJpTVXKuKuJyxDJ+NFJdUaUqGvyGy61OcvlbA=
+github.com/charmbracelet/ultraviolet v0.0.0-20260811164956-006e29f97886/go.mod h1:nAw0d9PhFp1qdzi2xhQU5YOu5sVpDIHWlaW2Uz/bCro=
+github.com/charmbracelet/x/ansi v0.11.8 h1:JMFwp0CgDC2+jcOB162HH5k7I3FVbgFSMMYg7dSPBQQ=
+github.com/charmbracelet/x/ansi v0.11.8/go.mod h1:ZNN+3mXny/516oTQPLMPIBeSINvNJJQ8uQXDgbeJxY0=
+github.com/charmbracelet/x/term v0.2.2 h1:xVRT/S2ZcKdhhOuSP4t5cLi5o+JxklsoEObBSgfgZRk=
+github.com/charmbracelet/x/term v0.2.2/go.mod h1:kF8CY5RddLWrsgVwpw4kAa6TESp6EB5y3uxGLeCqzAI=
+github.com/charmbracelet/x/termios v0.1.1 h1:o3Q2bT8eqzGnGPOYheoYS8eEleT5ZVNYNy8JawjaNZY=
+github.com/charmbracelet/x/termios v0.1.1/go.mod h1:rB7fnv1TgOPOyyKRJ9o+AsTU/vK5WHJ2ivHeut/Pcwo=
+github.com/charmbracelet/x/windows v0.2.2 h1:IofanmuvaxnKHuV04sC0eBy/smG6kIKrWG2/jYn2GuM=
+github.com/charmbracelet/x/windows v0.2.2/go.mod h1:/8XtdKZzedat74NQFn0NGlGL4soHB0YQZrETF96h75k=
github.com/chengxilo/virtualterm v1.0.4 h1:Z6IpERbRVlfB8WkOmtbHiDbBANU7cimRIof7mk9/PwM=
github.com/chengxilo/virtualterm v1.0.4/go.mod h1:DyxxBZz/x1iqJjFxTFcr6/x+jSpqN0iwWCOK1q10rlY=
github.com/chigopher/pathlib v0.19.1 h1:RoLlUJc0CqBGwq239cilyhxPNLXTK+HXoASGyGznx5A=
github.com/chigopher/pathlib v0.19.1/go.mod h1:tzC1dZLW8o33UQpWkNkhvPwL5n4yyFRFm/jL1YGWFvY=
+github.com/clipperhouse/displaywidth v0.11.0 h1:lBc6kY44VFw+TDx4I8opi/EtL9m20WSEFgwIwO+UVM8=
+github.com/clipperhouse/displaywidth v0.11.0/go.mod h1:bkrFNkf81G8HyVqmKGxsPufD3JhNl3dSqnGhOoSD/o0=
github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk=
github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
@@ -63,6 +79,8 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
+github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
+github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
@@ -105,6 +123,8 @@ github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJ
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 h1:gga7acRE695APm9hlsSMoOoE65U4/TcqNj90mc69Rlg=
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
+github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
+github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
diff --git a/plc4go/tools/internal/tui/glyphs.go b/plc4go/tools/internal/tui/glyphs.go
new file mode 100644
index 00000000..f6174325
--- /dev/null
+++ b/plc4go/tools/internal/tui/glyphs.go
@@ -0,0 +1,147 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package tui
+
+import (
+ "os"
+ "strings"
+
+ "charm.land/lipgloss/v2"
+)
+
+// Glyphs is the set of drawing characters a UI uses.
+//
+// It exists so that no call site ever hardcodes a box-drawing character. The tools default to
+// the Unicode set because it looks considerably better, but a terminal, font or console that
+// cannot render it would show mojibake and, worse, miscount cell widths and break the layout.
+// Every glyph therefore has an ASCII counterpart of the SAME display width.
+type Glyphs struct {
+ // Border is the pane border style.
+ Border lipgloss.Border
+
+ // Selected marks the highlighted row of a list or table.
+ Selected string
+ // Unselected occupies the same width as Selected for rows that are not selected, so that
+ // text does not shift when the selection moves.
+ Unselected string
+
+ // Yes and No mark a boolean capability, such as whether a driver supports discovery.
+ Yes string
+ No string
+
+ // Inbound and Outbound mark the direction a message travelled.
+ Inbound string
+ Outbound string
+
+ // Ok, Warn and Err prefix a status.
+ Ok string
+ Warn string
+ Err string
+
+ // Ellipsis truncates overlong text.
+ Ellipsis string
+
+ // Separator joins items on the status line.
+ Separator string
+
+ // ProgressFull and ProgressEmpty draw a progress bar.
+ ProgressFull string
+ ProgressEmpty string
+
+ // Spinner frames are cycled while work is in flight.
+ Spinner []string
+}
+
+// UnicodeGlyphs is the default, better-looking set.
+func UnicodeGlyphs() Glyphs {
+ return Glyphs{
+ Border: lipgloss.RoundedBorder(),
+ Selected: "▸",
+ Unselected: " ",
+ Yes: "●",
+ No: "○",
+ Inbound: "←",
+ Outbound: "→",
+ Ok: "✓",
+ Warn: "!",
+ Err: "✖",
+ Ellipsis: "…",
+ Separator: "·",
+ ProgressFull: "█",
+ ProgressEmpty: "░",
+ Spinner: []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
+ }
+}
+
+// ASCIIGlyphs is the fallback set. Every entry has the same display width as its Unicode
+// counterpart, so switching sets cannot change a layout's arithmetic.
+func ASCIIGlyphs() Glyphs {
+ return Glyphs{
+ Border: lipgloss.ASCIIBorder(),
+ Selected: ">",
+ Unselected: " ",
+ Yes: "*",
+ No: "-",
+ Inbound: "<",
+ Outbound: ">",
+ Ok: "+",
+ Warn: "!",
+ Err: "x",
+ Ellipsis: ".",
+ Separator: "|",
+ ProgressFull: "#",
+ ProgressEmpty: "-",
+ Spinner: []string{"|", "/", "-", "\\"},
+ }
+}
+
+// GlyphsFor returns the Unicode set when unicode is true and the ASCII set otherwise.
+func GlyphsFor(unicode bool) Glyphs {
+ if unicode {
+ return UnicodeGlyphs()
+ }
+ return ASCIIGlyphs()
+}
+
+// SupportsUnicode guesses whether the terminal can render the Unicode glyph set, by looking
+// for a UTF-8 locale in the usual environment variables.
+//
+// lookup is the environment accessor, so this is testable; pass os.Getenv in production.
+// The guess is deliberately conservative in only one direction: a terminal that can render
+// Unicode but advertises no UTF-8 locale merely gets the plainer set, whereas guessing wrong
+// the other way corrupts the display.
+func SupportsUnicode(lookup func(string) string) bool {
+ for _, key := range []string{"LC_ALL", "LC_CTYPE", "LANG"} {
+ value := strings.ToUpper(lookup(key))
+ if value == "" {
+ continue
+ }
+ // The first variable that is set decides, mirroring how locales resolve.
+ return strings.Contains(value, "UTF-8") || strings.Contains(value, "UTF8")
+ }
+ // Windows Terminal and most modern emulators cope, but a bare Windows console historically
+ // did not, and no locale variable is set there.
+ return false
+}
+
+// SupportsUnicodeFromEnv applies SupportsUnicode to the real environment.
+func SupportsUnicodeFromEnv() bool {
+ return SupportsUnicode(os.Getenv)
+}
diff --git a/plc4go/tools/internal/tui/theme.go b/plc4go/tools/internal/tui/theme.go
new file mode 100644
index 00000000..2ae277dd
--- /dev/null
+++ b/plc4go/tools/internal/tui/theme.go
@@ -0,0 +1,202 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+// Package tui holds the presentation vocabulary shared by the plc4go terminal tools: the
+// colour theme, the glyph sets and the pane chrome.
+//
+// It exists so that styling decisions live in exactly one place. The tools it serves
+// previously wrote colours as raw markup literals inline, such as "[#0000ff]" and "[white]",
+// which meant the palette could not adapt to the terminal, could not be switched off, and
+// was pure blue on a dark background in places where that is barely legible.
+package tui
+
+import (
+ "image/color"
+ "os"
+
+ "charm.land/lipgloss/v2"
+)
+
+// Options selects a theme variant.
+type Options struct {
+ // Dark says whether the terminal background is dark. Under Bubble Tea this comes from
+ // tea.BackgroundColorMsg, which arrives during startup and again whenever it changes.
+ Dark bool
+ // NoColor strips all colour, for NO_COLOR, a dumb terminal or a golden-file test.
+ NoColor bool
+ // ASCII selects the ASCII glyph set instead of the Unicode one.
+ ASCII bool
+}
+
+// OptionsFromEnv derives options from the environment, honouring the NO_COLOR convention and
+// the locale-based Unicode guess. An explicit --ascii flag should override the result.
+func OptionsFromEnv(dark bool) Options {
+ _, noColorSet := os.LookupEnv("NO_COLOR")
+ return Options{
+ Dark: dark,
+ NoColor: noColorSet,
+ ASCII: !SupportsUnicodeFromEnv(),
+ }
+}
+
+// Theme is the resolved set of styles and glyphs a view renders with.
+type Theme struct {
+ // Glyphs is the drawing-character set.
+ Glyphs Glyphs
+
+ // Title is the application name in the status bar.
+ Title lipgloss.Style
+ // Badge marks a mode that must be impossible to miss, such as demo mode.
+ Badge lipgloss.Style
+ // Muted is for secondary text: counts, hints, units.
+ Muted lipgloss.Style
+ // Key is for the key part of a key/value pair, and for keybinding names.
+ Key lipgloss.Style
+ // Value is for the value part of a key/value pair.
+ Value lipgloss.Style
+ // Accent draws attention without implying a problem.
+ Accent lipgloss.Style
+ // Ok, Warn and Err carry status meaning.
+ Ok lipgloss.Style
+ Warn lipgloss.Style
+ Err lipgloss.Style
+
+ // Pane is the chrome around an unfocused pane, PaneFocused around the focused one. The
+ // difference has to be visible at a glance: not knowing which pane has the keyboard was
+ // one of the worst parts of the previous UIs.
+ Pane lipgloss.Style
+ PaneFocused lipgloss.Style
+ // PaneTitle and PaneTitleFocused style the name carried in the pane's top border.
+ PaneTitle lipgloss.Style
+ PaneTitleFocused lipgloss.Style
+
+ // SelectedRow highlights the current row of a list or table.
+ SelectedRow lipgloss.Style
+
+ // dark records which variant produced this theme, for tests and for re-resolving.
+ dark bool
+ // noColor records whether colour was stripped.
+ noColor bool
+}
+
+// IsDark reports which background variant this theme was built for.
+func (t Theme) IsDark() bool { return t.dark }
+
+// IsNoColor reports whether the theme was built without colour.
+func (t Theme) IsNoColor() bool { return t.noColor }
+
+// The palette. Each entry is a light/dark pair, chosen so that contrast holds on both. These
+// are the only colour literals in the tools.
+var (
+ paletteAccent = pair{light: "#7D56F4", dark: "#A78BFA"}
+ paletteOk = pair{light: "#03713B", dark: "#4ADE80"}
+ paletteWarn = pair{light: "#8A5A00", dark: "#FBBF24"}
+ paletteErr = pair{light: "#B3261E", dark: "#F87171"}
+ paletteMuted = pair{light: "#5C5C66", dark: "#9CA3AF"}
+ paletteText = pair{light: "#1F2933", dark: "#E5E7EB"}
+ paletteChrome = pair{light: "#9AA0AA", dark: "#4B5563"}
+)
+
+// pair is a light/dark colour pair.
+type pair struct {
+ light string
+ dark string
+}
+
+// NewTheme resolves a theme for the given options.
+func NewTheme(options Options) Theme {
+ // lightDark picks the member of a pair that suits the background. When colour is off,
+ // every style is left unset instead, which renders as the terminal's own default.
+ lightDark := lipgloss.LightDark(options.Dark)
+ pick := func(p pair) color.Color {
+ return lightDark(lipgloss.Color(p.light), lipgloss.Color(p.dark))
+ }
+
+ // foreground builds a style, or a bare style when colour is disabled.
+ foreground := func(p pair) lipgloss.Style {
+ if options.NoColor {
+ return lipgloss.NewStyle()
+ }
+ return lipgloss.NewStyle().Foreground(pick(p))
+ }
+
+ glyphs := GlyphsFor(!options.ASCII)
+
+ theme := Theme{
+ Glyphs: glyphs,
+ dark: options.Dark,
+ noColor: options.NoColor,
+
+ Title: foreground(paletteText).Bold(true),
+ Muted: foreground(paletteMuted),
+ Key: foreground(paletteMuted),
+ Value: foreground(paletteText),
+ Accent: foreground(paletteAccent),
+ Ok: foreground(paletteOk),
+ Warn: foreground(paletteWarn),
+ Err: foreground(paletteErr),
+ }
+
+ // The badge has to read as a badge even with colour off, so it keeps its brackets there.
+ if options.NoColor {
+ theme.Badge = lipgloss.NewStyle().Bold(true)
+ } else {
+ theme.Badge = lipgloss.NewStyle().
+ Foreground(lipgloss.Color("#FFFFFF")).
+ Background(pick(paletteAccent)).
+ Bold(true).
+ Padding(0, 1)
+ }
+
+ theme.Pane = lipgloss.NewStyle().Border(glyphs.Border)
+ theme.PaneFocused = lipgloss.NewStyle().Border(glyphs.Border)
+ if !options.NoColor {
+ theme.Pane = theme.Pane.BorderForeground(pick(paletteChrome))
+ theme.PaneFocused = theme.PaneFocused.BorderForeground(pick(paletteAccent))
+ }
+
+ theme.PaneTitle = foreground(paletteMuted)
+ theme.PaneTitleFocused = foreground(paletteAccent).Bold(true)
+
+ // Without colour, the selected row is marked by the Selected glyph alone, which the views
+ // draw regardless; reverse video would fight the glyph and hurt readability.
+ if options.NoColor {
+ theme.SelectedRow = lipgloss.NewStyle().Bold(true)
+ } else {
+ theme.SelectedRow = lipgloss.NewStyle().Foreground(pick(paletteAccent)).Bold(true)
+ }
+
+ return theme
+}
+
+// PaneStyle returns the chrome for a pane, focused or not.
+func (t Theme) PaneStyle(focused bool) lipgloss.Style {
+ if focused {
+ return t.PaneFocused
+ }
+ return t.Pane
+}
+
+// PaneTitleStyle returns the title style for a pane, focused or not.
+func (t Theme) PaneTitleStyle(focused bool) lipgloss.Style {
+ if focused {
+ return t.PaneTitleFocused
+ }
+ return t.PaneTitle
+}
diff --git a/plc4go/tools/internal/tui/tui_test.go b/plc4go/tools/internal/tui/tui_test.go
new file mode 100644
index 00000000..b094e8d0
--- /dev/null
+++ b/plc4go/tools/internal/tui/tui_test.go
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package tui
+
+import (
+ "strings"
+ "testing"
+
+ "charm.land/lipgloss/v2"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// TestGlyphSetsHaveMatchingWidths is the property the ASCII fallback depends on. Layout
+// arithmetic is done in display cells, so if an ASCII glyph were a different width from the
+// Unicode glyph it replaces, switching sets would shift or clip every pane that uses it.
+func TestGlyphSetsHaveMatchingWidths(t *testing.T) {
+ unicode, ascii := UnicodeGlyphs(), ASCIIGlyphs()
+
+ pairs := map[string][2]string{
+ "Selected": {unicode.Selected, ascii.Selected},
+ "Unselected": {unicode.Unselected, ascii.Unselected},
+ "Yes": {unicode.Yes, ascii.Yes},
+ "No": {unicode.No, ascii.No},
+ "Inbound": {unicode.Inbound, ascii.Inbound},
+ "Outbound": {unicode.Outbound, ascii.Outbound},
+ "Ok": {unicode.Ok, ascii.Ok},
+ "Warn": {unicode.Warn, ascii.Warn},
+ "Err": {unicode.Err, ascii.Err},
+ "Ellipsis": {unicode.Ellipsis, ascii.Ellipsis},
+ "Separator": {unicode.Separator, ascii.Separator},
+ "ProgressFull": {unicode.ProgressFull, ascii.ProgressFull},
+ "ProgressEmpty": {unicode.ProgressEmpty, ascii.ProgressEmpty},
+ }
+ for name, p := range pairs {
+ t.Run(name, func(t *testing.T) {
+ assert.Equal(t, lipgloss.Width(p[0]), lipgloss.Width(p[1]),
+ "%s: unicode %q is %d cells, ascii %q is %d cells",
+ name, p[0], lipgloss.Width(p[0]), p[1], lipgloss.Width(p[1]))
+ })
+ }
+}
+
+// TestEveryGlyphIsExactlyOneCell keeps the sets usable in fixed-width columns.
+func TestEveryGlyphIsExactlyOneCell(t *testing.T) {
+ for setName, set := range map[string]Glyphs{"unicode": UnicodeGlyphs(), "ascii": ASCIIGlyphs()} {
+ for name, glyph := range map[string]string{
+ "Selected": set.Selected, "Unselected": set.Unselected,
+ "Yes": set.Yes, "No": set.No,
+ "Inbound": set.Inbound, "Outbound": set.Outbound,
+ "Ok": set.Ok, "Warn": set.Warn, "Err": set.Err,
+ "Ellipsis": set.Ellipsis, "Separator": set.Separator,
+ "ProgressFull": set.ProgressFull, "ProgressEmpty": set.ProgressEmpty,
+ } {
+ t.Run(setName+"/"+name, func(t *testing.T) {
+ assert.Equal(t, 1, lipgloss.Width(glyph), "%q must occupy one cell", glyph)
+ })
+ }
+ }
+}
+
+func TestSpinnerFramesAreUniformWidth(t *testing.T) {
+ for setName, set := range map[string]Glyphs{"unicode": UnicodeGlyphs(), "ascii": ASCIIGlyphs()} {
+ t.Run(setName, func(t *testing.T) {
+ require.NotEmpty(t, set.Spinner)
+ for _, frame := range set.Spinner {
+ assert.Equal(t, 1, lipgloss.Width(frame),
+ "spinner frame %q must be one cell or the prompt will jitter", frame)
+ }
+ })
+ }
+}
+
+func TestASCIIGlyphsAreActuallyASCII(t *testing.T) {
+ set := ASCIIGlyphs()
+ all := strings.Join(append([]string{
+ set.Selected, set.Unselected, set.Yes, set.No, set.Inbound, set.Outbound,
+ set.Ok, set.Warn, set.Err, set.Ellipsis, set.Separator,
+ set.ProgressFull, set.ProgressEmpty,
+ }, set.Spinner...), "")
+ for _, r := range all {
+ assert.Less(t, r, rune(128), "the ascii set must contain no multi-byte rune, found %q", r)
+ }
+}
+
+// TestASCIIBorderIsASCII guards the pane chrome, which is the most visible Unicode in the UI.
+func TestASCIIBorderIsASCII(t *testing.T) {
+ border := ASCIIGlyphs().Border
+ for _, part := range []string{
+ border.Top, border.Bottom, border.Left, border.Right,
+ border.TopLeft, border.TopRight, border.BottomLeft, border.BottomRight,
+ } {
+ for _, r := range part {
+ assert.Less(t, r, rune(128), "ascii border part %q contains a multi-byte rune %q", part, r)
+ }
+ }
+}
+
+func TestSupportsUnicodeReadsTheLocale(t *testing.T) {
+ tests := map[string]struct {
+ env map[string]string
+ want bool
+ }{
+ "utf-8 in LANG": {map[string]string{"LANG": "en_US.UTF-8"}, true},
+ "utf8 without hyphen": {map[string]string{"LANG": "en_US.utf8"}, true},
+ "LC_ALL wins over LANG": {map[string]string{"LC_ALL": "C", "LANG": "en_US.UTF-8"}, false},
+ "LC_CTYPE utf-8": {map[string]string{"LC_CTYPE": "de_DE.UTF-8"}, true},
+ "plain C locale": {map[string]string{"LANG": "C"}, false},
+ "POSIX locale": {map[string]string{"LANG": "POSIX"}, false},
+ "nothing set at all": {map[string]string{}, false},
+ "empty vars fall through": {map[string]string{"LC_ALL": "", "LANG": "en_GB.UTF-8"}, true},
+ }
+ for name, tc := range tests {
+ t.Run(name, func(t *testing.T) {
+ got := SupportsUnicode(func(key string) string { return tc.env[key] })
+ assert.Equal(t, tc.want, got)
+ })
+ }
+}
+
+func TestGlyphsForSelectsTheSet(t *testing.T) {
+ assert.Equal(t, UnicodeGlyphs().Selected, GlyphsFor(true).Selected)
+ assert.Equal(t, ASCIIGlyphs().Selected, GlyphsFor(false).Selected)
+}
+
+// TestThemeRendersDifferentlyForLightAndDark proves the palette actually adapts rather than
+// being nominally adaptive. The old UIs hardcoded colours such as pure blue, which is close
+// to illegible on a dark background.
+func TestThemeRendersDifferentlyForLightAndDark(t *testing.T) {
+ light := NewTheme(Options{Dark: false})
+ dark := NewTheme(Options{Dark: true})
+
+ assert.NotEqual(t, light.Accent.Render("x"), dark.Accent.Render("x"),
+ "the accent colour must differ between light and dark backgrounds")
+ assert.NotEqual(t, light.Err.Render("x"), dark.Err.Render("x"))
+ assert.False(t, light.IsDark())
+ assert.True(t, dark.IsDark())
+}
+
+// TestNoColorThemeEmitsNoEscapeSequences is the NO_COLOR contract, and it is also what makes
+// golden-file tests stable.
+func TestNoColorThemeEmitsNoEscapeSequences(t *testing.T) {
+ theme := NewTheme(Options{Dark: true, NoColor: true})
+ require.True(t, theme.IsNoColor())
+
+ for name, style := range map[string]lipgloss.Style{
+ "Title": theme.Title, "Muted": theme.Muted, "Key": theme.Key, "Value": theme.Value,
+ "Accent": theme.Accent, "Ok": theme.Ok, "Warn": theme.Warn, "Err": theme.Err,
+ "SelectedRow": theme.SelectedRow, "Badge": theme.Badge,
+ "PaneTitle": theme.PaneTitle, "PaneTitleFocused": theme.PaneTitleFocused,
+ } {
+ t.Run(name, func(t *testing.T) {
+ rendered := style.Render("sample")
+ assert.NotContains(t, rendered, "\x1b[3", "%s must not set a foreground colour", name)
+ assert.NotContains(t, rendered, "\x1b[4", "%s must not set a background colour", name)
+ assert.Contains(t, rendered, "sample", "%s must still render its text", name)
+ })
+ }
+}
+
+func TestNoColorPanesKeepTheirBorders(t *testing.T) {
+ theme := NewTheme(Options{Dark: true, NoColor: true})
+ // Losing colour must not lose structure: the borders are what explain the layout.
+ rendered := theme.Pane.Render("content")
+ assert.Contains(t, rendered, "content")
+ assert.Greater(t, strings.Count(rendered, "\n"), 1, "a bordered pane spans several lines")
+}
+
+// TestFocusIsVisuallyDistinct pins the fix for having had no focus indication at all.
+func TestFocusIsVisuallyDistinct(t *testing.T) {
+ theme := NewTheme(Options{Dark: true})
+ assert.NotEqual(t,
+ theme.PaneStyle(false).Render("x"),
+ theme.PaneStyle(true).Render("x"),
+ "the focused pane must be distinguishable from an unfocused one")
+ assert.NotEqual(t,
+ theme.PaneTitleStyle(false).Render("x"),
+ theme.PaneTitleStyle(true).Render("x"))
+}
+
+func TestASCIIOptionSelectsTheASCIIBorder(t *testing.T) {
+ assert.Equal(t, ASCIIGlyphs().Border, NewTheme(Options{ASCII: true}).Glyphs.Border)
+ assert.Equal(t, UnicodeGlyphs().Border, NewTheme(Options{ASCII: false}).Glyphs.Border)
+}
+
+func TestOptionsFromEnvHonoursNoColor(t *testing.T) {
+ t.Setenv("NO_COLOR", "1")
+ assert.True(t, OptionsFromEnv(true).NoColor)
+
+ // An empty NO_COLOR still counts as set, per the convention: presence is the signal.
+ t.Setenv("NO_COLOR", "")
+ assert.True(t, OptionsFromEnv(true).NoColor)
+}
+
+func TestOptionsFromEnvDerivesASCIIFromLocale(t *testing.T) {
+ t.Setenv("LC_ALL", "en_US.UTF-8")
+ t.Setenv("LANG", "en_US.UTF-8")
+ assert.False(t, OptionsFromEnv(true).ASCII, "a UTF-8 locale should keep the Unicode glyphs")
+
+ t.Setenv("LC_ALL", "C")
+ assert.True(t, OptionsFromEnv(true).ASCII, "a C locale should fall back to ASCII")
+}