Skip to content

Commit f6d6673

Browse files
authored
Merge pull request #3 from elkowar/master
v0.0.14 Fix issues with multiline input
2 parents dc827d8 + 3d7c6d6 commit f6d6673

File tree

3 files changed

+32
-42
lines changed

3 files changed

+32
-42
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pipr"
3-
version = "0.0.13"
3+
version = "0.0.14"
44
authors = ["Leon Kowarschick"]
55
edition = "2018"
66
license = "MIT"
@@ -16,9 +16,6 @@ exclude = [
1616
"showcase.gif",
1717
]
1818

19-
[badges]
20-
github = { repository = "Elkowar/pipr", branch = "master" }
21-
2219
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
2320

2421
[dependencies]

src/ui.rs

+30-37
Original file line numberDiff line numberDiff line change
@@ -191,52 +191,37 @@ fn draw_input_field<B: Backend>(f: &mut Frame<B>, rect: Rect, app: &mut App) {
191191

192192
let mut highlighter = HighlightLines::new(*SH_SYNTAX, &THEME);
193193

194-
let lines = if app.cached_command_part.is_none() {
195-
app.input_state
196-
.content_lines()
197-
.iter()
194+
// cut off lines at the input field width, adding ...
195+
let lines: Vec<String> = app
196+
.input_state
197+
.content_lines()
198+
.iter()
199+
.map(|line| truncate_with_ellipsis(line.clone(), rect.width as usize))
200+
.collect_vec();
201+
202+
let joined_lines = lines.join("\n");
203+
let styled_lines = if app.config.highlighting_enabled {
204+
LinesWithEndings::from(joined_lines.as_ref())
198205
.map(|line| {
199-
let mut line = line.clone();
200-
if line.len() > rect.width as usize - 5 {
201-
line.truncate(rect.width as usize - 5);
202-
line.push_str("...");
203-
}
204-
line
206+
highlighter
207+
.highlight(line, &SYNTAX_SET)
208+
.iter()
209+
.map(|(style, part)| Span::styled(*part, highlight_style_to_tui_style(&style)))
210+
.collect_vec()
205211
})
206-
.collect::<Vec<String>>()
207-
} else {
208-
app.input_state.content_lines().clone()
209-
};
210-
211-
let (cached_part, non_cached_part) = match app.cached_command_part {
212-
Some(CachedCommandPart { end_line, end_col, .. }) => lines.split_strings_at_offset(end_line, end_col),
213-
_ => (Vec::new(), lines),
214-
};
215-
let (cached_part, non_cached_part) = (cached_part.join("\n"), non_cached_part.join("\n"));
216-
217-
let cached_part_styled = vec![Span::styled(
218-
Cow::Borrowed(cached_part.as_ref()),
219-
Style::default().bg(Color::DarkGray).fg(Color::White),
220-
)];
221-
222-
let mut non_cached_part_styled = if app.config.highlighting_enabled {
223-
LinesWithEndings::from(&non_cached_part)
224-
.flat_map(|line| highlighter.highlight(line, &SYNTAX_SET))
225-
.map(|(style, part)| Span::styled(Cow::Borrowed(part), highlight_style_to_tui_style(&style)))
226-
.collect::<Vec<Span>>()
212+
.map(Spans::from)
213+
.collect_vec()
227214
} else {
228-
vec![Span::raw(non_cached_part)]
215+
lines.iter().map(Span::raw).map(Spans::from).collect_vec()
229216
};
230217

231-
let mut full_styled = cached_part_styled;
232-
full_styled.append(&mut non_cached_part_styled);
233-
234218
let is_bookmarked = app.bookmarks.entries().contains(&app.input_state.content_to_commandentry());
235219

236220
let input_block_title = format!(
237-
"Command{}{}{}",
221+
"Command{}{}{}{}",
238222
if is_bookmarked { " [Bookmarked]" } else { "" },
239223
if app.autoeval_mode { " [Autoeval]" } else { "" },
224+
if app.cached_command_part.is_some() { " [Caching]" } else { "" },
240225
if app.autoeval_mode && app.paranoid_history_mode {
241226
" [Paranoid]"
242227
} else {
@@ -245,11 +230,19 @@ fn draw_input_field<B: Backend>(f: &mut Frame<B>, rect: Rect, app: &mut App) {
245230
);
246231

247232
f.render_widget(
248-
Paragraph::new(Spans::from(full_styled)).block(make_default_block(&input_block_title, true)),
233+
Paragraph::new(Text::from(styled_lines)).block(make_default_block(&input_block_title, true)),
249234
rect,
250235
);
251236
}
252237

238+
fn truncate_with_ellipsis(mut line: String, length: usize) -> String {
239+
if line.len() > length - 5 {
240+
line.truncate(length - 5);
241+
line.push_str("...");
242+
}
243+
line
244+
}
245+
253246
fn apply_graphics_mode_to_style(style: &mut Style, modes: &[u32]) {
254247
fn ansi_to_color(bright: bool, n: u32) -> Color {
255248
match (bright, n) {

0 commit comments

Comments
 (0)