@@ -23,6 +23,7 @@ use ratatui::{
2323 widgets::{Block, Borders, List, ListItem, ListState, Paragraph, Wrap},
2424};
2525use tui_textarea::{Input, Key, TextArea};
26+use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
2627
2728const ASCII_BANNER: [&str; 7] = [
2829 "░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░▒▓███████▓▒░░▒▓████████▓▒░",
@@ -57,13 +58,12 @@ pub struct AppState {
5758 tick: u64,
5859 spinner: usize,
5960 terminal_scroll: usize,
61+ input_view_width: u16,
6062}
6163
6264impl AppState {
6365 pub fn new(cwd: PathBuf, vm_info: VmInfo) -> Self {
64− let mut input = TextArea::default();
65− input.set_cursor_style(Style::default().fg(Color::Yellow));
66− input.set_block(Block::default().borders(Borders::ALL).title("Input"));
66+ let input = Self::default_input();
6767
6868 Self {
6969 cwd,
@@ -76,21 +76,92 @@ impl AppState {
7676 tick: 0,
7777 spinner: 0,
7878 terminal_scroll: 0,
79+ input_view_width: 0,
7980 }
8081 }
8182
82− pub fn input_line_count(&self) -> u16 {
83− self.input.lines().len().max(1) as u16
83+ fn default_input() -> TextArea<'static> {
84+ let mut input = TextArea::default();
85+ input.set_cursor_style(Style::default().fg(Color::Yellow));
86+ input.set_block(Block::default().borders(Borders::ALL).title("Input"));
87+ input
88+ }
89+
90+ fn reset_input(&mut self) {
91+ self.input = Self::default_input();
8492 }
8593
86− pub fn input_height(&self) -> u16 {
87− let mut height = self.input_line_count();
94+ fn take_input_text(&mut self) -> String {
95+ let text = self.input.lines().join("");
96+ self.reset_input();
97+ text
98+ }
99+
100+ pub fn input_height_for_width(&self, available_width: u16) -> u16 {
101+ let inner_width = self.input_inner_width(available_width).max(1) as usize;
102+ let mut visual_lines = 0usize;
103+ for line in self.input.lines() {
104+ let line_width = UnicodeWidthStr::width(line.as_str());
105+ let wrapped = if line_width == 0 {
106+ 1
107+ } else {
108+ (line_width + inner_width - 1) / inner_width
109+ };
110+ visual_lines += wrapped;
111+ }
112+
113+ let mut height = (visual_lines.max(1) as u16).max(1);
88114 if self.input.block().is_some() {
89115 height = height.saturating_add(2);
90116 }
91117 height.max(1)
92118 }
93119
120+ fn input_inner_width(&self, available_width: u16) -> u16 {
121+ if self.input.block().is_some() {
122+ available_width.saturating_sub(2)
123+ } else {
124+ available_width
125+ }
126+ }
127+
128+ fn insert_char_with_wrap(&mut self, c: char) {
129+ let char_width = UnicodeWidthChar::width(c).unwrap_or(0);
130+ let width = self.input_view_width.max(1) as usize;
131+ let mut should_wrap = false;
132+ if char_width > 0 && width > 0 {
133+ let (row, col) = self.input.cursor();
134+ if let Some(line) = self.input.lines().get(row) {
135+ if col == line.chars().count() {
136+ let line_width = UnicodeWidthStr::width(line.as_str());
137+ should_wrap = line_width + char_width > width;
138+ }
139+ }
140+ }
141+
142+ if should_wrap {
143+ if c == ' ' {
144+ self.input.insert_char(c);
145+ self.input.insert_newline();
146+ } else {
147+ self.input.insert_newline();
148+ self.input.insert_char(c);
149+ }
150+ } else {
151+ self.input.insert_char(c);
152+ }
153+ }
154+
155+ fn insert_str_with_wrap(&mut self, text: &str) {
156+ for c in text.chars() {
157+ match c {
158+ '\n' => self.insert_char_with_wrap(' '),
159+ '\r' => {}
160+ _ => self.insert_char_with_wrap(c),
161+ }
162+ }
163+ }
164+
94165 pub fn push_history(&mut self, line: impl Into<String>) {
95166 self.history.push(line.into());
96167 if self.history.len() > 2000 {
@@ -281,7 +352,7 @@ fn handle_event(event: CrosstermEvent, app: &mut AppState) {
281352 CrosstermEvent::Mouse(event) => handle_mouse_event(event, app),
282353 CrosstermEvent::FocusGained | CrosstermEvent::FocusLost => {}
283354 CrosstermEvent::Paste(text) => {
284− app.input.insert_str(&text);
355+ app.insert_str_with_wrap(&text);
285356 }
286357 }
287358}
@@ -325,7 +396,21 @@ fn handle_key_event(key: KeyEvent, app: &mut AppState) {
325396 KeyCode::Down if key.modifiers.contains(KeyModifiers::CONTROL) => {
326397 app.terminal_scroll = app.terminal_scroll.saturating_sub(1);
327398 }
399+ KeyCode::Enter => {
400+ let message = app.take_input_text();
401+ if !message.trim().is_empty() {
402+ app.push_history(format!("> {}", message));
403+ app.terminal_scroll = 0;
404+ }
405+ }
328406 KeyCode::Tab => app.toggle_completions(default_completions()),
407+ KeyCode::Char(c)
408+ if !key
409+ .modifiers
410+ .contains(KeyModifiers::CONTROL | KeyModifiers::ALT) =>
411+ {
412+ app.insert_char_with_wrap(c);
413+ }
329414 _ => {
330415 app.input.input(input_from_key_event(key));
331416 }
@@ -407,9 +492,10 @@ fn default_completions() -> Vec<String> {
407492
408493fn render(frame: &mut Frame<'_>, app: &mut AppState) {
409494 let area = frame.area();
495+ app.input_view_width = app.input_inner_width(area.width);
410496 let layout = compute_layout(
411497 area,
412− app.input_height(),
498+ app.input_height_for_width(area.width),
413499 app.completions.items().len(),
414500 app.completions.is_active(),
415501 );
@@ -491,14 +577,14 @@ fn render_terminal(frame: &mut Frame<'_>, area: Rect, app: &AppState) {
491577 let scroll_top = max_top.saturating_sub(terminal_scroll);
492578 let paragraph = Paragraph::new(Text::from_iter(lines))
493579 .block(block)
494− .wrap(Wrap { trim: false })
580+ .wrap(Wrap { trim: true })
495581 .scroll((scroll_top.min(u16::MAX as usize) as u16, 0));
496582
497583 frame.render_widget(paragraph, area);
498584}
499585
500586fn render_input(frame: &mut Frame<'_>, area: Rect, app: &mut AppState) {
501− frame.render_widget(app.input.widget(), area);
587+ frame.render_widget(&app.input, area);
502588 if area.height > 0 && area.width > 0 {
503589 let cursor = app.input.cursor();
504590 let inner = match app.input.block() {