Skip to content

Commit fa2fd7e

Browse files
committed
feat(input): add wordwrap option
1 parent 01fcbe0 commit fa2fd7e

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

examples/prompt-copas.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ local pr = Prompt {
1111
prompt = "Enter something: ",
1212
value = "Hello, 你-好 World 🚀!",
1313
max_length = 62,
14-
position = 2,
14+
position = 10,
1515
cancellable = true,
16+
text_attr = { brightness = "high" },
17+
wordwrap = true,
1618
}
1719

1820

@@ -34,8 +36,9 @@ local clock = copas.timer.new{
3436

3537

3638
copas.addthread(function()
39+
t.text.stack.push { brightness = "low" }
3740
print("Time is ticking in the top-right corner!")
38-
local result, status = pr:run()
41+
local result, status = pr()
3942
if result then
4043
print("Result (string): '" .. result .. "'")
4144
print("Result (bytes):", (result or ""):byte(1, -1))

examples/prompt.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ local pr = Prompt {
55
prompt = "Enter something: ",
66
value = "Hello, 你-好 World 🚀!",
77
max_length = 62,
8-
-- position = 2,
8+
position = 10,
99
cancellable = true,
1010
text_attr = { brightness = "high" },
11+
wordwrap = true,
1112
}
1213

1314
t.initwrap(function () -- on Windows: wrap for utf8 output
15+
t.text.stack.push { brightness = "low" }
1416
local result, status = pr()
1517
if result then
1618
print("Result (string): '" .. result .. "'")

src/terminal/cli/prompt.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
--
66
-- Features: Prompt, UTF8 support, async input, (to be added: secrets, scrolling and wrapping)
77
--
8-
-- NOTE: you MUST `terminal.initialize` before calling this widget's `:run()` method.
8+
-- NOTE: you MUST call `terminal.initialize` before calling this widget's `:run()` method.
99
--
1010
-- *Usage:*
1111
-- local prompt = Prompt {
@@ -110,6 +110,7 @@ end
110110
-- @tparam[opt=80] number opts.max_length The maximum length of the input.
111111
-- @tparam[opt] string opts.word_delimiters Word delimiters for word operations.
112112
-- @tparam[opt] table opts.text_attr Text attributes for the prompt (input value only).
113+
-- @tparam[opt=false] boolean opts.wordwrap Whether to wordwrap the input value.
113114
-- @treturn Prompt A new Prompt instance.
114115
-- @name cli.Prompt
115116
function Prompt:init(opts)
@@ -131,6 +132,7 @@ function Prompt:init(opts)
131132
self.prompt_width = width.utf8swidth(self.prompt) -- the width of the prompt in characters
132133
self.max_length = opts.max_length or 80 -- the maximum length of the input
133134
self.text_attr = opts.text_attr or {} -- text attributes for the input value
135+
self.wordwrap = not not opts.wordwrap -- whether to wordwrap the input value
134136

135137
if self.value:len_char() > self.max_length then
136138
-- truncate the value if it is too long, keep cursor position
@@ -169,7 +171,7 @@ function Prompt:renew_cached_data()
169171
self.current_lines, self.cursor_row, self.cursor_col = self.value:format {
170172
width = self.screen_cols,
171173
first_width = self.screen_cols - self.prompt_width,
172-
wordwrap = false,
174+
wordwrap = self.wordwrap,
173175
pad = true,
174176
pad_last = false,
175177
no_new_cursor_line = false,

0 commit comments

Comments
 (0)