Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "readme.md"
license = "MIT"

[dependencies]
termion = "1.0"
termion = "2.0.1"
log-update = "~0.1.0"
default-editor = "~0.1.0"
emoji-commit-type = "~0.1.1"
Expand Down
176 changes: 176 additions & 0 deletions src/input_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
use ansi_term::Colour::White;

pub struct InputString {
value: String,
position: usize,
is_control_input: bool,
}

impl InputString {
pub fn new() -> Self {
Self {
value: String::new(),
position: 0,
is_control_input: false,
}
}

pub fn as_str(&self) -> &str {
self.value.as_str()
}

pub fn trim(&self) -> &str {
self.value.trim()
}
pub fn push(&mut self, c: char) {
if self.is_control_input {
self.handle_control(c);
return;
}
if self.position == self.value.len() {
self.value.push(c);
} else {
let mut new_string = String::new();
new_string.push_str(&self.value.as_str()[0..self.position]);
new_string.push(c);
new_string.push_str(&self.value.as_str()[self.position..]);
self.value = new_string;
}
self.position += 1;
}

pub fn delete(&mut self) {
if self.position == self.value.len() {
return;
}
if self.position == 0 {
self.value.remove(0);
} else {
let mut new_string = String::new();
new_string.push_str(&self.value.as_str()[0..self.position]);
new_string.push_str(&self.value.as_str()[self.position + 1..]);
self.value = new_string;
}
}

pub fn backspace(&mut self) {
if self.position == 0 {
return;
}
if self.position == self.value.len() {
self.value.pop();
} else {
let mut new_string = String::new();
new_string.push_str(&self.value.as_str()[0..self.position - 1]);
new_string.push_str(&self.value.as_str()[self.position..]);
self.value = new_string;
}
self.position -= 1;
}

pub fn go_char_left(&mut self) {
if self.position > 1 {
self.position -= 1;
} else {
self.position = 0;
}
}
pub fn go_char_right(&mut self) {
if self.position < self.value.len() {
self.position += 1;
} else {
self.position = self.value.len();
}
}
pub fn handle_control(&mut self, c: char) {
if !self.is_control_input && c.is_control() {
self.is_control_input = true
}
if self.is_control_input {
match c {
'D' => self.go_word_left(),
'C' => self.go_word_right(),
_ => { return; }
}
self.is_control_input = false
}

match c {
'b' => self.go_word_left(),
'f' => self.go_word_right(),
_ => {}
}
}
pub fn go_word_left(&mut self) {
if self.position == 0 {
return;
}
let mut new_position = self.position;
while new_position > 0
&& self
.value
.as_str()
.chars()
.nth(new_position - 1)
.unwrap()
.is_whitespace()
{
new_position -= 1;
}
while new_position > 0
&& !self
.value
.as_str()
.chars()
.nth(new_position - 1)
.unwrap()
.is_whitespace()
{
new_position -= 1;
}
self.position = new_position;
}
pub fn go_word_right(&mut self) {
if self.position == self.value.len() {
return;
}
let mut new_position = self.position;
while new_position < self.value.len()
&& self
.value
.as_str()
.chars()
.nth(new_position)
.unwrap()
.is_whitespace()
{
new_position += 1;
}
while new_position < self.value.len()
&& !self
.value
.as_str()
.chars()
.nth(new_position)
.unwrap()
.is_whitespace()
{
new_position += 1;
}
self.position = new_position;
}
pub fn format(&self) -> String {
if self.position == self.value.len() {
format!("{}{}", &self.value.as_str(), White.underline().paint(" "))
} else {
format!(
"{}{}{}",
&self.value.as_str()[0..self.position],
White
.underline()
.paint(&self.value.as_str()[self.position..self.position + 1]),
&self.value.as_str()[self.position + 1..]
)
}
}
}
21 changes: 14 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ use termion::raw::IntoRawMode;
use emoji_commit_type::CommitType;
use log_update::LogUpdate;
use structopt::StructOpt;
use ansi_term::Colour::{RGB, Green, Red, White};
use ansi_term::Colour::{RGB, Green, Red};


mod commit_rules;
mod git;
mod input_string;

use crate::input_string::InputString;

impl fmt::Display for commit_rules::CommitRuleValidationResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -80,29 +84,32 @@ fn collect_commit_message(selected_emoji: &'static str, launch_editor: &mut bool
let mut key_stream = stdin().keys();

let mut aborted = false;
let mut input = String::new();
let mut input = InputString::new();

loop {
let rule_text = commit_rules::check_message(&input)
let rule_text = commit_rules::check_message(&input.as_str())
.map(|result| format!("{}", result))
.collect::<Vec<_>>()
.join("\r\n");
let text = format!(
"\r\nRemember the seven rules of a great Git commit message:\r\n\r\n{}\r\n\r\n{}\r\n{} {}{}",
"\r\nRemember the seven rules of a great Git commit message:\r\n\r\n{}\r\n\r\n{}\r\n{} {}",
rule_text,
RGB(105, 105, 105).paint("Enter - finish, Ctrl-C - abort, Ctrl-E - continue editing in $EDITOR"),
selected_emoji,
input,
White.underline().paint(" ")
input.format()
);

log_update.render(&text).unwrap();

match key_stream.next().unwrap().unwrap() {
Key::Ctrl('c') => { aborted = true; break },
Key::Char('\n') => break,
Key::Alt(c) => input.handle_control(c),
Key::Char(c) => input.push(c),
Key::Backspace => { input.pop(); },
Key::Backspace => input.backspace(),
Key::Delete => input.delete(),
Key::Left => input.go_char_left(),
Key::Right => input.go_char_right(),
Key::Ctrl('e') => { *launch_editor = true; break },
_ => {},
}
Expand Down