Skip to content

Commit

Permalink
make palette multiline
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed Apr 18, 2024
1 parent ae65fb9 commit cd19295
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
8 changes: 8 additions & 0 deletions crates/ferrite-core/src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ impl CommandPalette {
None
}
}

pub fn height(&self) -> usize {
match &self.state {
PaletteState::Message(string) => string.lines().count(),
PaletteState::Error(string) => string.lines().count(),
_ => 1,
}
}
}

impl CommandPalette {
Expand Down
16 changes: 14 additions & 2 deletions crates/ferrite-tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,13 @@ impl TuiApp {
let theme = &self.engine.themes[&self.engine.config.theme];
f.render_widget(BackgroundWidget::new(theme), f.size());
let size = f.size();
let editor_size = Rect::new(size.x, size.y, size.width, size.height - 1);
let editor_size = Rect::new(
size.x,
size.y,
size.width,
size.height
.saturating_sub(self.engine.palette.height() as u16),
);

self.buffer_area = editor_size;
let current_pane = self.engine.workspace.panes.get_current_pane();
Expand Down Expand Up @@ -212,7 +218,13 @@ impl TuiApp {
);
}

let palette_size = Rect::new(size.left(), size.bottom() - 1, size.width, 1);
let palette_size = Rect::new(
size.left(),
size.bottom()
.saturating_sub(self.engine.palette.height() as u16),
size.width,
(self.engine.palette.height() as u16).min(size.height),
);
f.render_stateful_widget(
CmdPaletteWidget::new(theme, self.engine.palette.has_focus(), size),
palette_size,
Expand Down
38 changes: 24 additions & 14 deletions crates/ferrite-tui/src/widgets/palette_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,32 @@ impl StatefulWidget for CmdPaletteWidget<'_> {
}
}
PaletteState::Message(msg) => {
buf.set_stringn(
area.x,
area.y,
msg,
area.width.into(),
convert_style(&self.theme.text),
);
for (i, line) in msg.lines().enumerate() {
if i >= area.height.into() {
break;
}
buf.set_stringn(
area.x,
area.y + i as u16,
line,
area.width.into(),
convert_style(&self.theme.text),
);
}
}
PaletteState::Error(msg) => {
buf.set_stringn(
area.x,
area.y,
msg,
area.width.into(),
convert_style(&self.theme.error_text),
);
for (i, line) in msg.lines().enumerate() {
if i >= area.height.into() {
break;
}
buf.set_stringn(
area.x,
area.y + i as u16,
line,
area.width.into(),
convert_style(&self.theme.error_text),
);
}
}
PaletteState::Nothing => (),
PaletteState::Prompt {
Expand Down

0 comments on commit cd19295

Please sign in to comment.