Skip to content

Commit 83146bf

Browse files
committed
Switch Text Input to TextContent/draw-line-spans
This also clears up the last uses of $.grid.set-span-* outside of where they belong.
1 parent e896e6a commit 83146bf

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

lib/Terminal/Widgets/Input/Text.rakumod

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use Terminal::Widgets::Utils::Color;
88
use Terminal::Widgets::Events;
99
use Terminal::Widgets::Input;
1010
use Terminal::Widgets::Widget;
11+
use Terminal::Widgets::TextContent;
1112

1213

1314
#| Single-line text entry field with history tracking and mappable keys
@@ -56,17 +57,18 @@ class Terminal::Widgets::Input::Text
5657
method full-refresh(Str:D $content = $.input-field.?buffer.contents // '',
5758
Bool:D :$print = True) {
5859
if $.enabled {
60+
my $terminal = self.terminal;
61+
my $locale = $terminal.locale;
62+
my $caps = $terminal.caps;
63+
5964
# Determine new field metrics
60-
my $layout = self.layout.computed;
61-
my $prompt-width = duospace-width($.prompt-string);
62-
my $field-start = $prompt-width
63-
+ $layout.left-correction;
64-
my $display-width = $.w - $prompt-width
65-
- $layout.width-correction;
65+
my ($x, $y, $w, $h) = self.content-rect;
66+
my $prompt-width = $locale.width($.prompt-string);
67+
my $field-start = $x + $prompt-width;
68+
my $display-width = $w - $prompt-width;
6669

6770
# Create a new input field using the new metrics
6871
# XXXX: Should we support masked inputs?
69-
my $caps = self.terminal.caps;
7072
self.set-input-field($.input-class.new(:$display-width, :$field-start, :$caps));
7173

7274
# Insert initial content if any and refresh input field
@@ -84,16 +86,17 @@ class Terminal::Widgets::Input::Text
8486

8587
#| Display input disabled state
8688
method show-disabled(Bool:D :$print = True) {
87-
my $layout = self.layout.computed;
88-
my $x = $layout.left-correction;
89-
my $y = $layout.top-correction;
90-
my $w = $.w - $layout.width-correction;
91-
9289
self.clear-frame;
9390
self.draw-framing;
9491

95-
$.grid.set-span-text($x, $y, $.disabled-string);
96-
$.grid.set-span-color($x, $x + $w - 1, $y, self.current-color);
92+
my ($x, $y, $w, $h) = self.content-rect;
93+
my $locale = $.terminal.locale;
94+
my $pad = pad-span($w - $locale.width($.disabled-string));
95+
my $tree = span-tree(color => self.current-color,
96+
$.disabled-string, $pad);
97+
my @spans = $locale.render($tree);
98+
99+
self.draw-line-spans($x, $y, $w, @spans);
97100
self.composite(:$print);
98101
}
99102

@@ -109,9 +112,7 @@ class Terminal::Widgets::Input::Text
109112

110113
#| Refresh widget in input field mode
111114
method refresh-input-field(Bool:D $edited = True) {
112-
my $layout = self.layout.computed;
113-
my $x = $layout.left-correction;
114-
my $y = $layout.top-correction;
115+
my ($x, $y, $w, $h) = self.content-rect;
115116

116117
my $states = self.current-theme-states;
117118
my $color = self.current-color($states);
@@ -127,9 +128,14 @@ class Terminal::Widgets::Input::Text
127128
self.clear-frame;
128129
self.draw-framing;
129130

130-
$.grid.set-span($x, $y, $.prompt-string, $prompt);
131-
$.grid.set-span($start, $y, $refresh, $color);
132-
$.grid.set-span-color($pos, $pos, $y, $cursor);
131+
my $tree = span-tree(:$color,
132+
string-span($.prompt-string, color => $prompt),
133+
($refresh.substr(0, $pos - 1) if $pos),
134+
string-span($refresh.substr($pos, 1), color => $cursor),
135+
($refresh.substr($pos + 1) if $pos + $start < $w));
136+
137+
my @spans = $.terminal.locale.render($tree);
138+
self.draw-line-spans($x, $y, $w, @spans);
133139
}
134140

135141
#| Fetch completions based on current buffer contents and cursor pos

0 commit comments

Comments
 (0)