tuile 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/README.md +150 -4
- data/examples/file_commander.rb +4 -3
- data/examples/sampler.rb +1 -0
- data/lib/tuile/ansi.rb +4 -3
- data/lib/tuile/color.rb +122 -0
- data/lib/tuile/component/button.rb +9 -5
- data/lib/tuile/component/label.rb +13 -15
- data/lib/tuile/component/list.rb +29 -19
- data/lib/tuile/component/picker_window.rb +2 -2
- data/lib/tuile/component/popup.rb +11 -1
- data/lib/tuile/component/text_area.rb +1 -2
- data/lib/tuile/component/text_field.rb +1 -2
- data/lib/tuile/component/text_input.rb +10 -15
- data/lib/tuile/component/text_view.rb +9 -10
- data/lib/tuile/component/window.rb +70 -16
- data/lib/tuile/component.rb +74 -5
- data/lib/tuile/event_queue.rb +26 -2
- data/lib/tuile/fake_screen.rb +8 -0
- data/lib/tuile/keys.rb +10 -0
- data/lib/tuile/screen.rb +96 -4
- data/lib/tuile/sizing.rb +59 -0
- data/lib/tuile/terminal_background.rb +137 -0
- data/lib/tuile/theme.rb +202 -0
- data/lib/tuile/theme_def.rb +85 -0
- data/lib/tuile/version.rb +1 -1
- data/lib/tuile.rb +0 -1
- data/sig/tuile.rbs +627 -41
- metadata +5 -15
data/lib/tuile/sizing.rb
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tuile
|
|
4
|
+
# A sizing policy for a slot whose position is managed by a parent
|
|
5
|
+
# component (e.g. {Component::Window#footer}). Resolves one dimension at a
|
|
6
|
+
# time via {#resolve}, so the same value works for widths and heights.
|
|
7
|
+
#
|
|
8
|
+
# Three policies exist:
|
|
9
|
+
#
|
|
10
|
+
# - {FILL} — take everything the slot offers;
|
|
11
|
+
# - {WRAP_CONTENT} — take the component's natural extent (its
|
|
12
|
+
# {Component#content_size}), clamped to the slot;
|
|
13
|
+
# - {.fixed} — take exactly the given number of cells, clamped to the slot.
|
|
14
|
+
#
|
|
15
|
+
# Note that {WRAP_CONTENT} only makes sense for components that report a
|
|
16
|
+
# natural {Component#content_size} ({Component::Label}, {Component::Button},
|
|
17
|
+
# {Component::List}, …). Input components ({Component::TextField} et al.)
|
|
18
|
+
# report {Size::ZERO}, so a wrap-content slot collapses to zero width —
|
|
19
|
+
# i.e. the component becomes invisible. Use {.fixed} or {FILL} for those.
|
|
20
|
+
#
|
|
21
|
+
# @!attribute [r] mode
|
|
22
|
+
# @return [Symbol] `:fill`, `:wrap_content` or `:fixed`.
|
|
23
|
+
# @!attribute [r] amount
|
|
24
|
+
# @return [Integer, nil] the cell count for `:fixed`; `nil` otherwise.
|
|
25
|
+
class Sizing < Data.define(:mode, :amount)
|
|
26
|
+
# @param amount [Integer] the number of cells to occupy; 0 or greater.
|
|
27
|
+
# @return [Sizing] a fixed-size policy.
|
|
28
|
+
def self.fixed(amount)
|
|
29
|
+
raise TypeError, "expected Integer, got #{amount.inspect}" unless amount.is_a?(Integer)
|
|
30
|
+
raise ArgumentError, "amount must not be negative, got #{amount}" if amount.negative?
|
|
31
|
+
|
|
32
|
+
new(mode: :fixed, amount: amount)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Resolves one dimension of a slot.
|
|
36
|
+
# @param available [Integer] cells the slot offers; 0 or greater.
|
|
37
|
+
# @param content [Integer] the component's natural extent on this axis
|
|
38
|
+
# (one dimension of its {Component#content_size}).
|
|
39
|
+
# @return [Integer] the resolved extent, always in `0..available`.
|
|
40
|
+
def resolve(available, content)
|
|
41
|
+
case mode
|
|
42
|
+
when :fill then available
|
|
43
|
+
when :fixed then amount.clamp(0, available)
|
|
44
|
+
when :wrap_content then content.clamp(0, available)
|
|
45
|
+
else raise ArgumentError, "unknown mode #{mode.inspect}"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Occupy everything the slot offers.
|
|
50
|
+
# @return [Sizing]
|
|
51
|
+
FILL = new(mode: :fill, amount: nil)
|
|
52
|
+
|
|
53
|
+
# Occupy the component's natural {Component#content_size}, clamped to the
|
|
54
|
+
# slot. Components reporting {Size::ZERO} collapse to invisibility — see
|
|
55
|
+
# the class doc.
|
|
56
|
+
# @return [Sizing]
|
|
57
|
+
WRAP_CONTENT = new(mode: :wrap_content, amount: nil)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tuile
|
|
4
|
+
# Detects whether the terminal background is light or dark, so {Screen}
|
|
5
|
+
# can pick {Theme::LIGHT} or {Theme::DARK} automatically at startup.
|
|
6
|
+
#
|
|
7
|
+
# Two mechanisms, in order of reliability:
|
|
8
|
+
#
|
|
9
|
+
# 1. **OSC 11 query** — writes `ESC ] 11 ; ? BEL` to the terminal; modern
|
|
10
|
+
# terminals (xterm, kitty, alacritty, wezterm, iTerm2, GNOME Terminal,
|
|
11
|
+
# Windows Terminal) reply on stdin with the background color
|
|
12
|
+
# (`\e]11;rgb:RRRR/GGGG/BBBB` + BEL or ST). The color's relative
|
|
13
|
+
# luminance against a 0.5 threshold decides light vs dark. Terminals
|
|
14
|
+
# that don't support the query simply never reply, so the read is
|
|
15
|
+
# bounded by a short timeout.
|
|
16
|
+
# 2. **`COLORFGBG` env var** — rxvt/konsole export `"fg;bg"` ANSI palette
|
|
17
|
+
# indices. Less reliable (stale across SSH/tmux, often unset); used
|
|
18
|
+
# only when OSC 11 yields nothing.
|
|
19
|
+
#
|
|
20
|
+
# **Timing matters**: the OSC 11 reply arrives on stdin, so the query
|
|
21
|
+
# must complete before {EventQueue#start_key_thread} owns stdin —
|
|
22
|
+
# otherwise the reply bytes get consumed as garbage keystrokes. {Screen}
|
|
23
|
+
# calls {.detect} from its constructor, which apps run before
|
|
24
|
+
# {Screen#run_event_loop}; don't call this after the event loop started.
|
|
25
|
+
module TerminalBackground
|
|
26
|
+
# How long to wait for the OSC 11 reply. Generous for a local
|
|
27
|
+
# terminal; bounded so unsupporting terminals (which never reply)
|
|
28
|
+
# don't stall startup.
|
|
29
|
+
# @return [Float] seconds.
|
|
30
|
+
QUERY_TIMEOUT = 0.1
|
|
31
|
+
|
|
32
|
+
# The OSC 11 background-color query, BEL-terminated.
|
|
33
|
+
# @return [String]
|
|
34
|
+
QUERY = "\e]11;?\a"
|
|
35
|
+
|
|
36
|
+
# Matches the OSC 11 reply. Components are 1–4 hex digits each
|
|
37
|
+
# (terminals vary); `rgba:` (4 components) also matches — the alpha
|
|
38
|
+
# tail is ignored.
|
|
39
|
+
# @return [Regexp]
|
|
40
|
+
REPLY = %r{\e\]11;rgba?:(\h{1,4})/(\h{1,4})/(\h{1,4})}
|
|
41
|
+
|
|
42
|
+
# Enables mode 2031: the terminal pushes a color-scheme report
|
|
43
|
+
# (`\e[?997;1n` dark / `\e[?997;2n` light) whenever the OS appearance
|
|
44
|
+
# flips — see {EventQueue::ColorSchemeEvent}. Terminals without
|
|
45
|
+
# support ignore the sequence. Written by {Screen#run_event_loop}.
|
|
46
|
+
# @return [String]
|
|
47
|
+
NOTIFY_ON = "\e[?2031h"
|
|
48
|
+
|
|
49
|
+
# Disables mode 2031 again; written when the event loop exits.
|
|
50
|
+
# @return [String]
|
|
51
|
+
NOTIFY_OFF = "\e[?2031l"
|
|
52
|
+
|
|
53
|
+
class << self
|
|
54
|
+
# Detects the terminal background. Queries OSC 11 when both `input`
|
|
55
|
+
# and `output` are TTYs, falling back to `COLORFGBG`.
|
|
56
|
+
#
|
|
57
|
+
# @param input [IO] where the OSC 11 reply arrives (the TTY input).
|
|
58
|
+
# @param output [IO] where the query is written (the TTY output).
|
|
59
|
+
# @param env [Hash{String => String}] environment for the `COLORFGBG`
|
|
60
|
+
# fallback; defaults to `ENV` (which duck-types the `[]` lookup).
|
|
61
|
+
# @param timeout [Numeric] max seconds to wait for the OSC 11 reply.
|
|
62
|
+
# @return [Symbol, nil] `:light`, `:dark`, or nil when undetectable.
|
|
63
|
+
def detect(input: $stdin, output: $stdout, env: ENV, timeout: QUERY_TIMEOUT)
|
|
64
|
+
osc = query_osc11(input, output, timeout) if input.tty? && output.tty?
|
|
65
|
+
osc || from_colorfgbg(env["COLORFGBG"])
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
# Writes the OSC 11 query and classifies the reply. The whole
|
|
71
|
+
# exchange runs with `input` in raw mode: the reply has no trailing
|
|
72
|
+
# newline, so a canonical-mode read would block past the timeout,
|
|
73
|
+
# and echo would smear the reply bytes onto the screen.
|
|
74
|
+
# @param input [IO]
|
|
75
|
+
# @param output [IO]
|
|
76
|
+
# @param timeout [Numeric]
|
|
77
|
+
# @return [Symbol, nil]
|
|
78
|
+
def query_osc11(input, output, timeout)
|
|
79
|
+
reply = input.raw do
|
|
80
|
+
output.write(QUERY)
|
|
81
|
+
output.flush
|
|
82
|
+
read_reply(input, timeout)
|
|
83
|
+
end
|
|
84
|
+
match = REPLY.match(reply)
|
|
85
|
+
match && classify(match.captures)
|
|
86
|
+
rescue SystemCallError, IOError
|
|
87
|
+
nil
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Accumulates reply bytes until a BEL/ST terminator or the deadline.
|
|
91
|
+
# Terminals that don't support OSC 11 never reply — returning
|
|
92
|
+
# whatever arrived (usually nothing) lets the caller fail soft.
|
|
93
|
+
# @param input [IO]
|
|
94
|
+
# @param timeout [Numeric]
|
|
95
|
+
# @return [String]
|
|
96
|
+
def read_reply(input, timeout)
|
|
97
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
|
|
98
|
+
buffer = +""
|
|
99
|
+
loop do
|
|
100
|
+
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
101
|
+
return buffer if remaining <= 0 || IO.select([input], nil, nil, remaining).nil?
|
|
102
|
+
|
|
103
|
+
buffer << input.readpartial(256)
|
|
104
|
+
return buffer if buffer.include?("\a") || buffer.include?("\e\\")
|
|
105
|
+
end
|
|
106
|
+
rescue EOFError
|
|
107
|
+
buffer
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Relative luminance of the reported background, scaled per
|
|
111
|
+
# component hex width (xterm replies 4 digits per channel, others 2).
|
|
112
|
+
# @param components [Array<String>] three hex strings.
|
|
113
|
+
# @return [Symbol] `:light` or `:dark`.
|
|
114
|
+
def classify(components)
|
|
115
|
+
r, g, b = components.map { |c| c.to_i(16).fdiv((16**c.length) - 1) }
|
|
116
|
+
luminance = (0.2126 * r) + (0.7152 * g) + (0.0722 * b)
|
|
117
|
+
luminance > 0.5 ? :light : :dark
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# `COLORFGBG` is `"fg;bg"` (rxvt sometimes `"fg;default;bg"`) with
|
|
121
|
+
# ANSI palette indices. White-ish backgrounds — 7 (white) and the
|
|
122
|
+
# bright range 9–15 — read as light; 0–6 and 8 as dark; anything
|
|
123
|
+
# else (missing, `"default"`, out of range) is inconclusive.
|
|
124
|
+
# @param value [String, nil]
|
|
125
|
+
# @return [Symbol, nil]
|
|
126
|
+
def from_colorfgbg(value)
|
|
127
|
+
bg = value&.split(";")&.last
|
|
128
|
+
return nil unless bg&.match?(/\A\d+\z/)
|
|
129
|
+
|
|
130
|
+
case bg.to_i
|
|
131
|
+
when 0..6, 8 then :dark
|
|
132
|
+
when 7, 9..15 then :light
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
data/lib/tuile/theme.rb
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tuile
|
|
4
|
+
# A set of semantic colors the built-in components read when painting.
|
|
5
|
+
# The current theme lives at {Screen#theme}; components must look it up
|
|
6
|
+
# at paint time (inside `repaint`) rather than caching values, so that
|
|
7
|
+
# assigning {Screen#theme=} restyles everything via a single
|
|
8
|
+
# invalidate-everything pass.
|
|
9
|
+
#
|
|
10
|
+
# The primary API is the rendering helpers — {#active_bg},
|
|
11
|
+
# {#active_border}, {#input_bg}, {#hint} — which wrap a plain string in
|
|
12
|
+
# the token's SGR color (on the channel appropriate for the token's
|
|
13
|
+
# role) and reset:
|
|
14
|
+
#
|
|
15
|
+
# screen.theme.active_bg("[ Ok ]") # => "\e[48;5;59m[ Ok ]\e[0m"
|
|
16
|
+
# screen.theme.hint("quit") # => "\e[38;5;109mquit\e[0m"
|
|
17
|
+
#
|
|
18
|
+
# The helpers pass content through verbatim, so input may carry other
|
|
19
|
+
# escape sequences (e.g. {Component::Window} feeds its border string,
|
|
20
|
+
# cursor moves included). For span-aware styling — applying a token to a
|
|
21
|
+
# {StyledString} while preserving per-span colors — use the `*_color`
|
|
22
|
+
# readers instead (e.g. {Component::List} highlights its cursor row via
|
|
23
|
+
# `with_bg(theme.active_bg_color)`). Rule of thumb: plain chrome text →
|
|
24
|
+
# helper; structured text → `*_color` reader + {StyledString}.
|
|
25
|
+
#
|
|
26
|
+
# Two built-in themes are provided: {DARK} (the default; the colors Tuile
|
|
27
|
+
# has always used) and {LIGHT} (counterparts legible on light terminal
|
|
28
|
+
# backgrounds). A custom theme is one `with` away:
|
|
29
|
+
#
|
|
30
|
+
# screen.theme = Theme::DARK.with(active_border_color: Color::CYAN)
|
|
31
|
+
#
|
|
32
|
+
# Tokens deliberately cover only the *accents* Tuile paints. Everything
|
|
33
|
+
# else inherits the terminal's own default foreground/background, which
|
|
34
|
+
# already matches the user's terminal theme perfectly — that's why there
|
|
35
|
+
# is no global `bg`/`fg` token.
|
|
36
|
+
#
|
|
37
|
+
# Every token is a {Color} — and must be passed as one. Unlike the
|
|
38
|
+
# lenient {Color.coerce} call sites elsewhere in the framework, a theme
|
|
39
|
+
# is declared once per app, so it takes only {Color} instances: at a
|
|
40
|
+
# declaration site `Color.palette(130)` documents itself in a way the
|
|
41
|
+
# bare `130` does not (palette index? RGB channel?) — and the named
|
|
42
|
+
# palette constants (`Color::DARK_ORANGE3` *is* 130; see
|
|
43
|
+
# {Color::PALETTE_NAMES}) go one step further.
|
|
44
|
+
#
|
|
45
|
+
# ## App-specific tokens
|
|
46
|
+
#
|
|
47
|
+
# Beyond the built-in tokens, an app can carry its own colors in
|
|
48
|
+
# {#custom} — a frozen `Hash{Symbol => Color}` member. Look them up with
|
|
49
|
+
# {#[]} (fail-fast: a typo raises `KeyError`) and render with the
|
|
50
|
+
# generic {#fg} / {#bg} helpers:
|
|
51
|
+
#
|
|
52
|
+
# theme = Theme::DARK.with(custom: { accent: Color::DARK_ORANGE })
|
|
53
|
+
# theme[:accent] # => Color, e.g. for StyledString#with_fg
|
|
54
|
+
# theme.fg(:accent, "NEW") # => "\e[38;5;208mNEW\e[0m"
|
|
55
|
+
#
|
|
56
|
+
# Apps wanting semantic readers can subclass — `Data#with` preserves the
|
|
57
|
+
# subclass, so an `AppTheme` stays an `AppTheme` through `with`:
|
|
58
|
+
#
|
|
59
|
+
# class AppTheme < Tuile::Theme
|
|
60
|
+
# def accent(text) = fg(:accent, text)
|
|
61
|
+
# end
|
|
62
|
+
#
|
|
63
|
+
# Pair the dark and light variants in a {ThemeDef} and hand it to
|
|
64
|
+
# {Screen#theme_def=} so OS appearance flips pick the right one.
|
|
65
|
+
#
|
|
66
|
+
# @!attribute [r] active_bg_color
|
|
67
|
+
# Background highlight of the component the user is interacting with:
|
|
68
|
+
# the {Component::List} cursor row, the focused {Component::TextField} /
|
|
69
|
+
# {Component::TextArea} well, the focused {Component::Button}. "Active"
|
|
70
|
+
# matches the {Component#active?} focus-chain flag — this is the
|
|
71
|
+
# focus/selection highlight in conventional UI terms.
|
|
72
|
+
# @return [Color]
|
|
73
|
+
# @!attribute [r] active_border_color
|
|
74
|
+
# Foreground of a {Component::Window} border when the window is on the
|
|
75
|
+
# active (focus) chain.
|
|
76
|
+
# @return [Color]
|
|
77
|
+
# @!attribute [r] input_bg_color
|
|
78
|
+
# Resting background "well" of {Component::TextField} /
|
|
79
|
+
# {Component::TextArea} when *not* active — visibly a field, but
|
|
80
|
+
# distinctly subtler than {#active_bg_color}.
|
|
81
|
+
# @return [Color]
|
|
82
|
+
# @!attribute [r] hint_color
|
|
83
|
+
# Foreground of keyboard-shortcut captions in status-bar hints (the
|
|
84
|
+
# "quit" in "q quit") — see {#hint}.
|
|
85
|
+
# @return [Color]
|
|
86
|
+
# @!attribute [r] custom
|
|
87
|
+
# App-specific color tokens; empty in the built-in themes. Frozen —
|
|
88
|
+
# build a changed theme via `with(custom: ...)`. Prefer {#[]} for
|
|
89
|
+
# lookups (it fail-fasts on typos); read this directly to enumerate
|
|
90
|
+
# the tokens.
|
|
91
|
+
# @return [Hash{Symbol => Color}]
|
|
92
|
+
class Theme < Data.define(:active_bg_color, :active_border_color, :input_bg_color, :hint_color, :custom)
|
|
93
|
+
# @param active_bg_color [Color]
|
|
94
|
+
# @param active_border_color [Color]
|
|
95
|
+
# @param input_bg_color [Color]
|
|
96
|
+
# @param hint_color [Color]
|
|
97
|
+
# @param custom [Hash{Symbol => Color}] app-specific tokens, see {#custom}.
|
|
98
|
+
# @raise [TypeError] when a token is not a {Color}, or `custom` is not a
|
|
99
|
+
# `Hash{Symbol => Color}`.
|
|
100
|
+
def initialize(active_bg_color:, active_border_color:, input_bg_color:, hint_color:, custom: {})
|
|
101
|
+
{ active_bg_color:, active_border_color:, input_bg_color:, hint_color: }.each do |name, value|
|
|
102
|
+
raise TypeError, "#{name} must be a Tuile::Color, got #{value.inspect}" unless value.is_a?(Color)
|
|
103
|
+
end
|
|
104
|
+
raise TypeError, "custom must be a Hash, got #{custom.inspect}" unless custom.is_a?(Hash)
|
|
105
|
+
|
|
106
|
+
custom.each do |key, value|
|
|
107
|
+
raise TypeError, "custom key must be a Symbol, got #{key.inspect}" unless key.is_a?(Symbol)
|
|
108
|
+
raise TypeError, "custom[#{key.inspect}] must be a Tuile::Color, got #{value.inspect}" unless value.is_a?(Color)
|
|
109
|
+
end
|
|
110
|
+
super(active_bg_color:, active_border_color:, input_bg_color:, hint_color:, custom: custom.dup.freeze)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Looks up an app-specific token from {#custom}.
|
|
114
|
+
# @param token [Symbol]
|
|
115
|
+
# @return [Color]
|
|
116
|
+
# @raise [KeyError] when the token is not present — a typo should fail
|
|
117
|
+
# loudly, not paint in a default.
|
|
118
|
+
def [](token) = custom.fetch(token)
|
|
119
|
+
|
|
120
|
+
# Renders `text` in the foreground color of the app-specific `token`
|
|
121
|
+
# — the generic counterpart of {#hint} for {#custom} tokens.
|
|
122
|
+
# @param token [Symbol]
|
|
123
|
+
# @param text [String]
|
|
124
|
+
# @return [String] ANSI-rendered text, ending with an SGR reset.
|
|
125
|
+
# @raise [KeyError] when the token is not present.
|
|
126
|
+
def fg(token, text) = wrap(text, self[token], :fg)
|
|
127
|
+
|
|
128
|
+
# Renders `text` on the background color of the app-specific `token`
|
|
129
|
+
# — the generic counterpart of {#active_bg} for {#custom} tokens.
|
|
130
|
+
# @param token [Symbol]
|
|
131
|
+
# @param text [String]
|
|
132
|
+
# @return [String] ANSI-rendered text, ending with an SGR reset.
|
|
133
|
+
# @raise [KeyError] when the token is not present.
|
|
134
|
+
def bg(token, text) = wrap(text, self[token], :bg)
|
|
135
|
+
|
|
136
|
+
# Renders `text` on the {#active_bg_color} background.
|
|
137
|
+
# @param text [String]
|
|
138
|
+
# @return [String] ANSI-rendered text, ending with an SGR reset.
|
|
139
|
+
def active_bg(text) = wrap(text, active_bg_color, :bg)
|
|
140
|
+
|
|
141
|
+
# Renders `text` in the {#active_border_color} foreground. Content
|
|
142
|
+
# passes through verbatim, so it may embed non-SGR escapes (cursor
|
|
143
|
+
# moves in a border string).
|
|
144
|
+
# @param text [String]
|
|
145
|
+
# @return [String] ANSI-rendered text, ending with an SGR reset.
|
|
146
|
+
def active_border(text) = wrap(text, active_border_color, :fg)
|
|
147
|
+
|
|
148
|
+
# Renders `text` on the {#input_bg_color} background.
|
|
149
|
+
# @param text [String]
|
|
150
|
+
# @return [String] ANSI-rendered text, ending with an SGR reset.
|
|
151
|
+
def input_bg(text) = wrap(text, input_bg_color, :bg)
|
|
152
|
+
|
|
153
|
+
# Renders `text` in the {#hint_color} foreground, for status-bar hints,
|
|
154
|
+
# e.g. `"q #{screen.theme.hint("quit")}"`. The color is baked into the
|
|
155
|
+
# returned String, so strings built this way do *not* restyle when the
|
|
156
|
+
# theme changes — rebuild them instead (the framework's own call sites
|
|
157
|
+
# rebuild on every status-bar refresh).
|
|
158
|
+
# @param text [String]
|
|
159
|
+
# @return [String] ANSI-rendered text, ending with an SGR reset.
|
|
160
|
+
def hint(text) = wrap(text, hint_color, :fg)
|
|
161
|
+
|
|
162
|
+
# The colors Tuile used before themes existed, tuned for dark terminal
|
|
163
|
+
# backgrounds. GREY37 (palette 59) is what Rainbow emits for
|
|
164
|
+
# `:darkslategray`, LIGHT_SKY_BLUE3 (109) for `:cadetblue`; GREY27
|
|
165
|
+
# (238, ~#444444) sits in the grayscale ramp, bright enough to stand
|
|
166
|
+
# out against non-pure-black dark terminal themes (Gruvbox/Solarized/
|
|
167
|
+
# OneDark base backgrounds sit in the #1d–#2d range) yet distinctly
|
|
168
|
+
# darker than the active highlight at 59 (~#5f5f5f).
|
|
169
|
+
# @return [Theme]
|
|
170
|
+
DARK = new(active_bg_color: Color::GREY37,
|
|
171
|
+
active_border_color: Color::GREEN,
|
|
172
|
+
input_bg_color: Color::GREY27,
|
|
173
|
+
hint_color: Color::LIGHT_SKY_BLUE3)
|
|
174
|
+
|
|
175
|
+
# Counterparts legible on light terminal backgrounds: grayscale-ramp
|
|
176
|
+
# highlights just below white (GREY82 = 252 ~#d0d0d0, GREY85 = 253
|
|
177
|
+
# ~#dadada — dark enough to read as a "well" against white, one step
|
|
178
|
+
# lighter than the active highlight) and a dark teal (TURQUOISE4 = 30,
|
|
179
|
+
# ~#008787) keeping the hint hue. `active_border_color` stays the
|
|
180
|
+
# named green — named ANSI colors are remapped by the terminal's own
|
|
181
|
+
# palette, so the theme picks a light-appropriate green for us.
|
|
182
|
+
# @return [Theme]
|
|
183
|
+
LIGHT = new(active_bg_color: Color::GREY82,
|
|
184
|
+
active_border_color: Color::GREEN,
|
|
185
|
+
input_bg_color: Color::GREY85,
|
|
186
|
+
hint_color: Color::TURQUOISE4)
|
|
187
|
+
|
|
188
|
+
private
|
|
189
|
+
|
|
190
|
+
# The single sanctioned place for verbatim SGR wrapping: `text` is not
|
|
191
|
+
# parsed or validated, so callers may embed non-SGR escapes. Emits the
|
|
192
|
+
# same bytes `StyledString.styled(text, ...).to_ansi` would for plain
|
|
193
|
+
# text.
|
|
194
|
+
# @param text [String]
|
|
195
|
+
# @param color [Color]
|
|
196
|
+
# @param target [Symbol] `:fg` or `:bg`.
|
|
197
|
+
# @return [String]
|
|
198
|
+
def wrap(text, color, target)
|
|
199
|
+
"#{color.to_ansi(target)}#{text}#{Ansi::RESET}"
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tuile
|
|
4
|
+
# An app's theme definition: the {Theme} pair covering both terminal
|
|
5
|
+
# appearances. {Screen} keeps one at {Screen#theme_def} (defaulting to
|
|
6
|
+
# {DEFAULT}) and picks the member matching the detected background at
|
|
7
|
+
# startup and on every OS appearance flip (mode 2031) — so a custom
|
|
8
|
+
# definition survives the user toggling light/dark, where a bare
|
|
9
|
+
# {Screen#theme=} assignment would be replaced.
|
|
10
|
+
#
|
|
11
|
+
# APP_THEME = Tuile::ThemeDef.new(
|
|
12
|
+
# dark: Tuile::Theme::DARK.with(custom: { accent: Color::DARK_ORANGE }),
|
|
13
|
+
# light: Tuile::Theme::LIGHT.with(custom: { accent: Color::DARK_ORANGE3 })
|
|
14
|
+
# )
|
|
15
|
+
# screen.theme_def = APP_THEME
|
|
16
|
+
#
|
|
17
|
+
# Both members must declare the same {Theme#custom} key set. Without
|
|
18
|
+
# that, a token present only in one member would raise `KeyError` at
|
|
19
|
+
# the unpredictable moment the user flips OS appearance; checking here
|
|
20
|
+
# turns it into an immediate construction-time failure.
|
|
21
|
+
#
|
|
22
|
+
# @!attribute [r] dark
|
|
23
|
+
# The theme applied on dark terminal backgrounds.
|
|
24
|
+
# @return [Theme]
|
|
25
|
+
# @!attribute [r] light
|
|
26
|
+
# The theme applied on light terminal backgrounds.
|
|
27
|
+
# @return [Theme]
|
|
28
|
+
class ThemeDef < Data.define(:dark, :light)
|
|
29
|
+
# @param dark [Theme]
|
|
30
|
+
# @param light [Theme]
|
|
31
|
+
# @raise [TypeError] when a member is not a {Theme}.
|
|
32
|
+
# @raise [ArgumentError] when the members' {Theme#custom} key sets differ.
|
|
33
|
+
def initialize(dark:, light:)
|
|
34
|
+
raise TypeError, "dark must be a Tuile::Theme, got #{dark.inspect}" unless dark.is_a?(Theme)
|
|
35
|
+
raise TypeError, "light must be a Tuile::Theme, got #{light.inspect}" unless light.is_a?(Theme)
|
|
36
|
+
|
|
37
|
+
if dark.custom.keys.sort != light.custom.keys.sort
|
|
38
|
+
raise ArgumentError,
|
|
39
|
+
"dark and light must declare the same custom tokens; " \
|
|
40
|
+
"dark has #{dark.custom.keys.sort.inspect}, light has #{light.custom.keys.sort.inspect}"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
super
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# The member for the given color scheme. Anything other than `:light`
|
|
47
|
+
# selects {#dark}, matching {TerminalBackground.detect}'s
|
|
48
|
+
# inconclusive-means-dark policy.
|
|
49
|
+
# @param scheme [Symbol] `:dark` or `:light`.
|
|
50
|
+
# @return [Theme]
|
|
51
|
+
def for(scheme) = scheme == :light ? light : dark
|
|
52
|
+
|
|
53
|
+
# The built-in pair: {Theme::DARK} / {Theme::LIGHT}.
|
|
54
|
+
# @return [ThemeDef]
|
|
55
|
+
DEFAULT = new(dark: Theme::DARK, light: Theme::LIGHT)
|
|
56
|
+
|
|
57
|
+
class << self
|
|
58
|
+
# The definition newly-constructed {Screen}s start from (see
|
|
59
|
+
# {Screen#theme_def}); initially {DEFAULT}. Reassigning affects
|
|
60
|
+
# future screens only — an already-constructed screen keeps its
|
|
61
|
+
# definition until {Screen#theme_def=}.
|
|
62
|
+
#
|
|
63
|
+
# Intended for test suites: production apps assign
|
|
64
|
+
# {Screen#theme_def=} once at startup, but component specs build a
|
|
65
|
+
# fresh {FakeScreen} per example, and a component reading a custom
|
|
66
|
+
# token (`theme[:accent]`) would `KeyError` against the built-in
|
|
67
|
+
# default. Point this at the app's definition once and every
|
|
68
|
+
# {Screen.fake} carries it:
|
|
69
|
+
#
|
|
70
|
+
# Tuile::ThemeDef.default = APP_THEME # spec_helper.rb, once
|
|
71
|
+
# before { Screen.fake } # theme[:accent] resolves
|
|
72
|
+
# @return [ThemeDef]
|
|
73
|
+
attr_reader :default
|
|
74
|
+
|
|
75
|
+
# @param theme_def [ThemeDef]
|
|
76
|
+
# @raise [TypeError] when not a {ThemeDef}.
|
|
77
|
+
def default=(theme_def)
|
|
78
|
+
raise TypeError, "expected ThemeDef, got #{theme_def.inspect}" unless theme_def.is_a?(ThemeDef)
|
|
79
|
+
|
|
80
|
+
@default = theme_def
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
@default = DEFAULT
|
|
84
|
+
end
|
|
85
|
+
end
|
data/lib/tuile/version.rb
CHANGED