Skip to content

Commit

Permalink
Merge pull request #106 from tpoliaw/confirm_edit
Browse files Browse the repository at this point in the history
Ask user before opening editor to edit snippet
  • Loading branch information
Ninjani authored Sep 24, 2021
2 parents 88b9008 + 27992fd commit 8d15450
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.15.0] - ???
### Changed

* Prompt user to open snippet in editor when editing existing snippet. #104

## [0.14.4] - 2021-09-05
### Fixed
* .deb package extended description
Expand Down
34 changes: 22 additions & 12 deletions src/the_way/snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,28 @@ 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 code = match old_code {
Some(old) => {
if utils::confirm("Edit snippet? [y/N]", false)? {
utils::external_editor_input(old_code.as_deref(), &extension)?
} else {
old.to_string()
}
}
None => {
let mut input = utils::user_input(
"Code snippet (leave empty to open external editor)",
None, // default
false, // show default
true, // allow empty
)?;
if input.is_empty() {
input = utils::external_editor_input(None, &extension)?;
}
input
}
};
Ok(Self::new(
index,
description,
Expand Down
12 changes: 11 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
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
4 changes: 2 additions & 2 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ fn change_snippet_rexpect(config_file: PathBuf) -> rexpect::errors::Result<()> {
p.send_line("")?;
p.exp_regex("Date")?;
p.send_line("")?;
p.exp_regex("Code snippet")?;
p.send_line("code 2")?;
p.exp_regex("Edit snippet")?;
p.send_line("")?;
p.exp_regex("Snippet #1 changed")?;
p.wait_for_prompt()?;
p.send_line(&format!("{} view 1", executable))?;
Expand Down

0 comments on commit 8d15450

Please sign in to comment.