Skip to content

Commit

Permalink
Ask user before opening editor to edit snippet
Browse files Browse the repository at this point in the history
Previously if a snippet was a single line, there was no way of opening
it in an external editor. This meant (among other things) that is was
not possible to add a line break to a single line snippet.

It also meant for multi line snippets that the editor had to be opened
and closed in order for the edit to be completed even if the user only
wanted to edit tags or description.

This is slightly more involved when making simple edits to single lines
but the consistency across all snippets makes it clearer how things
work.

Fixes out-of-cheese-error#104
  • Loading branch information
tpoliaw committed Sep 17, 2021
1 parent 7bc94fe commit b1e96ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
17 changes: 5 additions & 12 deletions src/the_way/snippet.rs
Expand Up @@ -101,18 +101,11 @@ impl Snippet {
.and_hms(0, 0, 0),
None => Utc::now(),
};
let show_default = old_code
.map(|c| c.split('\n').nth(1).is_none())
.unwrap_or(false);
let mut code = utils::user_input(
"Code snippet (<RET> to edit in external editor)",
if show_default { old_code } else { None },
show_default,
true,
)?;
if code.is_empty() {
code = utils::external_editor_input(old_code.as_deref(), &extension)?;
}
let edit = utils::confirm("Edit snippet? [y/N]", false)?;
let code = match edit {
true => utils::external_editor_input(old_code.as_deref(), &extension)?,
false => old_code.unwrap_or("").to_string(),
};
Ok(Self::new(
index,
description,
Expand Down
12 changes: 11 additions & 1 deletion src/utils.rs
Expand Up @@ -5,7 +5,7 @@ use std::str;
use chrono::{Date, DateTime, Utc, MAX_DATE, MIN_DATE};
use chrono_english::{parse_date_string, Dialect};
use color_eyre::Help;
use dialoguer::{Editor, Input};
use dialoguer::{Confirm, Editor, Input};
use syntect::highlighting::Style;
use syntect::util::as_24_bit_terminal_escaped;

Expand Down Expand Up @@ -167,6 +167,16 @@ pub fn user_input(
}
}

/// Get a yes/no answer from the user
pub fn confirm(prompt: &str, default: bool) -> color_eyre::Result<bool> {
let theme = dialoguer::theme::ColorfulTheme::default();
Ok(Confirm::with_theme(&theme)
.with_prompt(prompt)
.default(default)
.show_default(false)
.interact()?)
}

/// Make an indicatif spinner with given message
pub fn get_spinner(message: &str) -> indicatif::ProgressBar {
let spinner = indicatif::ProgressBar::new_spinner();
Expand Down

0 comments on commit b1e96ce

Please sign in to comment.