Skip to content

Commit 0bc7538

Browse files
cstructLinusU
authored andcommitted
🎉 Add option to continue editing in external editor
This fixes #7
1 parent 3bc2c15 commit 0bc7538

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

‎Cargo.lock

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

‎Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ default-editor = "~0.1.0"
1919
emoji-commit-type = "~0.1.1"
2020
git2 = "~0.13.20"
2121
structopt = "~0.3"
22+
ansi_term = "~0.12"

‎src/main.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ use default_editor;
1515
use emoji_commit_type::CommitType;
1616
use log_update::LogUpdate;
1717
use structopt::StructOpt;
18+
use ansi_term::Colour::{RGB, Green, Red, White};
1819

1920
mod commit_rules;
2021
mod git;
2122

22-
static PASS: &'static str = "\u{001b}[32m✔\u{001b}[39m";
23-
static FAIL: &'static str = "\u{001b}[31m✖\u{001b}[39m";
24-
static CURSOR: &'static str = "\u{001b}[4m \u{001b}[24m";
25-
2623
impl fmt::Display for commit_rules::CommitRuleValidationResult {
2724
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28-
write!(f, "{} {}", if self.pass { PASS } else { FAIL }, self.description)
25+
write!(f, "{} {}", if self.pass {
26+
Green.paint("✔")
27+
} else {
28+
Red.paint("✖")
29+
}, self.description)
2930
}
3031
}
3132

@@ -73,7 +74,7 @@ fn select_emoji() -> Option<&'static str> {
7374
if aborted { None } else { Some(selected.emoji()) }
7475
}
7576

76-
fn collect_commit_message(selected_emoji: &'static str) -> Option<String> {
77+
fn collect_commit_message(selected_emoji: &'static str, launch_editor: &mut bool) -> Option<String> {
7778
let mut log_update = LogUpdate::new(stderr()).unwrap();
7879
let mut raw_output = stderr().into_raw_mode().unwrap();
7980

@@ -88,11 +89,12 @@ fn collect_commit_message(selected_emoji: &'static str) -> Option<String> {
8889
.collect::<Vec<_>>()
8990
.join("\r\n");
9091
let text = format!(
91-
"\r\nRemember the seven rules of a great Git commit message:\r\n\r\n{}\r\n\r\n{} {}{}",
92+
"\r\nRemember the seven rules of a great Git commit message:\r\n\r\n{}\r\n\r\n{}\r\n{} {}{}",
9293
rule_text,
94+
RGB(105, 105, 105).paint("Enter - finish, Ctrl-C - abort, Ctrl-E - continue editing in $EDITOR"),
9395
selected_emoji,
9496
input,
95-
CURSOR,
97+
White.underline().paint(" ")
9698
);
9799

98100
log_update.render(&text).unwrap();
@@ -102,6 +104,7 @@ fn collect_commit_message(selected_emoji: &'static str) -> Option<String> {
102104
Key::Char('\n') => break,
103105
Key::Char(c) => input.push(c),
104106
Key::Backspace => { input.pop(); },
107+
Key::Ctrl('e') => { *launch_editor = true; break },
105108
_ => {},
106109
}
107110
}
@@ -149,7 +152,8 @@ fn collect_information_and_write_to_file(out_path: PathBuf) {
149152
}
150153

151154
if let Some(emoji) = maybe_emoji {
152-
let maybe_message = collect_commit_message(emoji);
155+
let mut launch_editor = false;
156+
let maybe_message = collect_commit_message(emoji, &mut launch_editor);
153157

154158
if maybe_message == None {
155159
abort();
@@ -158,8 +162,13 @@ fn collect_information_and_write_to_file(out_path: PathBuf) {
158162
if let Some(message) = maybe_message {
159163
let result = format!("{} {}\n", emoji, message);
160164

161-
let mut f = File::create(out_path).unwrap();
165+
let mut f = File::create(out_path.clone()).unwrap();
162166
f.write_all(result.as_bytes()).unwrap();
167+
drop(f);
168+
169+
if launch_editor {
170+
launch_default_editor(out_path);
171+
}
163172
}
164173
}
165174
}

0 commit comments

Comments
 (0)