Skip to content

Commit 375e604

Browse files
committed
fix: properly resize search input for long values
1 parent f26e6fd commit 375e604

3 files changed

Lines changed: 44 additions & 12 deletions

File tree

src/app.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,13 @@ impl App {
365365
.direction(Direction::Vertical)
366366
.constraints(vec![
367367
Constraint::Length(self.rura_widget.height(inner_area.width) + 2), // command
368-
Constraint::Length(if self.searching { 3 } else { 0 }), // search
369-
Constraint::Fill(1), // output
370-
Constraint::Length(1), // status
368+
Constraint::Length(if self.searching {
369+
self.search_widget.height(inner_area.width) + 2
370+
} else {
371+
0
372+
}), // search
373+
Constraint::Fill(1), // output
374+
Constraint::Length(1), // status
371375
])
372376
.split(area);
373377

@@ -377,8 +381,12 @@ impl App {
377381
let layout = Layout::default()
378382
.direction(Direction::Vertical)
379383
.constraints(vec![
380-
Constraint::Fill(1), // output
381-
Constraint::Length(if self.searching { 3 } else { 0 }), // search
384+
Constraint::Fill(1), // output
385+
Constraint::Length(if self.searching {
386+
self.search_widget.height(inner_area.width) + 2
387+
} else {
388+
0
389+
}), // search
382390
Constraint::Length(self.rura_widget.height(inner_area.width) + 2), // command
383391
Constraint::Length(1), // status
384392
])
@@ -412,8 +420,9 @@ impl App {
412420
}
413421

414422
if self.searching {
415-
let x = self.search_widget.input.visual_cursor() as u16;
416-
frame.set_cursor_position((search_input_area.x + 1 + x, search_input_area.y + 1));
423+
let inner_rect = search_input_area.inner(margin);
424+
let (x, y) = self.search_widget.cursor(inner_rect.width);
425+
frame.set_cursor_position((search_input_area.x + 1 + x, search_input_area.y + 1 + y));
417426
} else {
418427
let inner_rect = command_input_area.inner(margin);
419428
let (x, y) = self.rura_widget.cursor(inner_rect.width);

src/rura_widget.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl RuraWidget {
211211
}
212212
}
213213

214-
fn render_line(line: Vec<StyledGrapheme>, area: Rect, buf: &mut Buffer, y: u16) {
214+
pub fn render_line(line: Vec<StyledGrapheme>, area: Rect, buf: &mut Buffer, y: u16) {
215215
let mut x = 0;
216216
for StyledGrapheme { symbol, style } in line {
217217
let width = symbol.width();

src/search_widget.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use crossterm::event::KeyCode::Char;
22
use crossterm::event::{Event, KeyModifiers};
3+
use itertools::Itertools;
34
use ratatui::buffer::Buffer;
4-
use ratatui::layout::Rect;
5-
use ratatui::prelude::Widget;
5+
use ratatui::layout::{Margin, Rect};
6+
use ratatui::prelude::{Style, Widget};
7+
use ratatui::text::Line;
68
use ratatui::widgets::{Block, Paragraph};
79
use tui_input::Input;
810
use tui_input::backend::crossterm::EventHandler;
@@ -21,14 +23,26 @@ impl Widget for &SearchWidget {
2123
where
2224
Self: Sized,
2325
{
24-
let par = Paragraph::new(self.input.value()).block(Block::bordered().title(format!(
26+
let paragraph = Paragraph::new(self.input.value()).block(Block::bordered().title(format!(
2527
" Search: {} / {} | {} {} ",
2628
if self.total == 0 { 0 } else { self.current + 1 },
2729
self.total,
2830
if self.regex { "[.*]" } else { ".*" },
2931
if self.case_sensitive { "[Cc]" } else { "Cc" },
3032
)));
31-
par.render(area, buf);
33+
34+
paragraph.render(area, buf);
35+
36+
let inner_area = area.inner(Margin::new(1, 1));
37+
38+
let line = Line::from(self.input.value());
39+
let graphemes = line.styled_graphemes(Style::default()).collect_vec();
40+
41+
let chunks = graphemes.chunks(inner_area.width as usize);
42+
43+
for (i, c) in chunks.enumerate() {
44+
crate::rura_widget::render_line(c.to_vec(), inner_area, buf, i as u16)
45+
}
3246
}
3347
}
3448

@@ -60,6 +74,15 @@ impl SearchWidget {
6074
}
6175
}
6276

77+
pub fn height(&self, width: u16) -> u16 {
78+
(self.input.value().len() as u16 / width) + 1
79+
}
80+
81+
pub fn cursor(&self, width: u16) -> (u16, u16) {
82+
let cursor = self.input.visual_cursor() as u16;
83+
(cursor % width, cursor / width)
84+
}
85+
6386
pub fn update_highlight_info(&mut self, info: (usize, usize)) {
6487
self.current = info.0;
6588
self.total = info.1;

0 commit comments

Comments
 (0)