diff --git a/src/commands/preview/mod.rs b/src/commands/preview/mod.rs index e571b78..e20d573 100644 --- a/src/commands/preview/mod.rs +++ b/src/commands/preview/mod.rs @@ -14,7 +14,7 @@ pub struct Input { } fn extract_elements(argstr: &str) -> Result<(&str, &str, &str)> { - let mut parts = argstr.split(deser::terminal::DELIMITER).skip(3); + let mut parts = argstr.split(deser::terminal::DELIMITER).skip(1); let tags = parts.next().context("No `tags` element provided.")?; let comment = parts.next().context("No `comment` element provided.")?; let snippet = parts.next().context("No `snippet` element provided.")?; diff --git a/src/deser/mod.rs b/src/deser/mod.rs index 278687f..85b260e 100644 --- a/src/deser/mod.rs +++ b/src/deser/mod.rs @@ -19,7 +19,7 @@ pub fn with_new_lines(txt: String) -> String { pub fn fix_newlines(txt: &str) -> String { if txt.contains(NEWLINE_ESCAPE_CHAR) { (*NEWLINE_REGEX) - .replace_all(txt.replace(LINE_SEPARATOR, " ").as_str(), "") + .replace_all(txt.replace(LINE_SEPARATOR, "\n").as_str(), "") .to_string() } else { txt.to_string() diff --git a/src/deser/terminal.rs b/src/deser/terminal.rs index a85cb9c..f4b2055 100644 --- a/src/deser/terminal.rs +++ b/src/deser/terminal.rs @@ -33,11 +33,28 @@ lazy_static! { pub fn write(item: &Item) -> String { let (tag_width_percentage, comment_width_percentage, snippet_width_percentage) = *COLUMN_WIDTHS; + + let separator_count = max( + item.snippet.matches(LINE_SEPARATOR).count(), + item.comment.matches(LINE_SEPARATOR).count(), + ); + + let splitted_comment = item.comment.split(LINE_SEPARATOR).collect::>(); + let splitted_snippet = item.snippet.split(LINE_SEPARATOR).collect::>(); + + let printer_item = (0..=separator_count) + .map(|i| {format!("{tags_short}{delimiter}{comment_line_i}{delimiter}{snippet_line_i}", + tags_short = style(limit_str(if i == 0 { &item.tags } else { "" }, tag_width_percentage)).with(CONFIG.tag_color()), + comment_line_i = style(limit_str(splitted_comment.get(i).unwrap_or(&""), comment_width_percentage)).with(CONFIG.comment_color()), + snippet_line_i = style(limit_str(splitted_snippet.get(i).unwrap_or(&""), snippet_width_percentage)).with(CONFIG.snippet_color()), + delimiter = " ", + )}) + .collect::>() + .join("\n"); + format!( - "{tags_short}{delimiter}{comment_short}{delimiter}{snippet_short}{delimiter}{tags}{delimiter}{comment}{delimiter}{snippet}{delimiter}{file_index}{delimiter}\n", - tags_short = style(limit_str(&item.tags, tag_width_percentage)).with(CONFIG.tag_color()), - comment_short = style(limit_str(&fix_newlines(&item.comment), comment_width_percentage)).with(CONFIG.comment_color()), - snippet_short = style(limit_str(&fix_newlines(&item.snippet), snippet_width_percentage)).with(CONFIG.snippet_color()), + "{printer_item}{delimiter}{tags}{delimiter}{comment}{delimiter}{snippet}{delimiter}{file_index}{delimiter}\0", + printer_item = printer_item, tags = item.tags, comment = item.comment, delimiter = DELIMITER, @@ -47,7 +64,7 @@ pub fn write(item: &Item) -> String { } pub fn read(raw_snippet: &str, is_single: bool) -> Result<(&str, Item)> { - let mut lines = raw_snippet.split('\n'); + let mut lines = raw_snippet.split('\0'); let key = if is_single { "enter" } else { @@ -60,7 +77,7 @@ pub fn read(raw_snippet: &str, is_single: bool) -> Result<(&str, Item)> { .next() .context("No more parts in `selections`")? .split(DELIMITER) - .skip(3); + .skip(1); let tags = parts.next().unwrap_or("").into(); let comment = parts.next().unwrap_or("").into(); diff --git a/src/finder/mod.rs b/src/finder/mod.rs index a1edee6..0ff71d9 100644 --- a/src/finder/mod.rs +++ b/src/finder/mod.rs @@ -116,10 +116,12 @@ impl FinderChoice { "--bind", format!("ctrl-j:down,ctrl-k:up{bindings}").as_str(), "--exact", + "--read0", + "--print0", ]); if !opts.show_all_columns { - command.args(["--with-nth", "1,2,3"]); + command.args(["--with-nth", "1"]); } if !opts.prevent_select1 { diff --git a/src/parser.rs b/src/parser.rs index 4a7ba0e..4e5fb80 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -225,9 +225,13 @@ impl<'a> Parser<'a> { let write_fn = self.write_fn; - self.writer - .write_all(write_fn(item).as_bytes()) - .context("Failed to write command to finder's stdin") + let string = write_fn(item); + //eprintln!("string as written by write_fn:\n{}", string); + + return self + .writer + .write_all(string.as_bytes()) + .context("Failed to write command to finder's stdin"); } pub fn read_lines( @@ -257,9 +261,9 @@ impl<'a> Parser<'a> { // blank if line.is_empty() { - if !item.snippet.is_empty() { - item.snippet.push_str(deser::LINE_SEPARATOR); - } + // if !item.snippet.is_empty() { + // item.snippet.push_str(deser::LINE_SEPARATOR); + // } } // tag else if line.starts_with('%') {