Skip to content

Allow multiline commit messgages #1333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* customizable `cmdbar_bg` theme color & screen spanning selected line bg [[@gigitsu](https://github.com/gigitsu)] ([#1299](https://github.com/extrawurst/gitui/pull/1299))
* word motions to text input [[@Rodrigodd](https://github.com/Rodrigodd)] ([#1256](https://github.com/extrawurst/gitui/issues/1256))
* file blame at right revision from commit-details [[@heiskane](https://github.com/heiskane)] ([#1122](https://github.com/extrawurst/gitui/issues/1122))
* Multiline commit messages [[@heiskane](https://github.com/heiskane)] ([#1171](https://github.com/extrawurst/gitui/issues/1171))
* dedicated selection foreground theme color `selection_fg` ([#1365](https://github.com/extrawurst/gitui/issues/1365))
* add `regex-fancy` and `regex-onig` features to allow building Syntect with Onigumara regex engine instead of the default engine based on fancy-regex [[@jirutka](https://github.com/jirutka)]
* add `vendor-openssl` feature to allow building without vendored openssl [[@jirutka](https://github.com/jirutka)]
Expand Down
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 src/components/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl Component for CommitComponent {
}

if let Event::Key(e) = ev {
if key_match(e, self.key_config.keys.enter)
if key_match(e, self.key_config.keys.confirm_commit)
&& self.can_commit()
{
try_or_popup!(
Expand Down
3 changes: 2 additions & 1 deletion src/components/create_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ impl CreateBranchComponent {
&strings::create_branch_popup_title(&key_config),
&strings::create_branch_popup_msg(&key_config),
true,
),
)
.with_input_type(super::InputType::Singleline),
theme,
key_config,
repo,
Expand Down
3 changes: 2 additions & 1 deletion src/components/file_find_popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ impl FileFindPopup {
"",
"start typing..",
false,
);
)
.with_input_type(super::InputType::Singleline);
find_text.embed();

Self {
Expand Down
3 changes: 2 additions & 1 deletion src/components/rename_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ impl RenameBranchComponent {
&strings::rename_branch_popup_title(&key_config),
&strings::rename_branch_popup_msg(&key_config),
true,
),
)
.with_input_type(super::InputType::Singleline),
branch_ref: None,
key_config,
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/stashmsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ impl StashMsgComponent {
&strings::stash_popup_title(&key_config),
&strings::stash_popup_msg(&key_config),
true,
),
)
.with_input_type(super::InputType::Singleline),
key_config,
repo,
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/tag_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ impl TagCommitComponent {
&strings::tag_popup_name_title(),
&strings::tag_popup_name_msg(),
true,
),
)
.with_input_type(super::InputType::Singleline),
commit_id: None,
key_config,
repo,
Expand Down
224 changes: 222 additions & 2 deletions src/components/textinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,161 @@ impl TextInputComponent {
Some(0)
}

fn previous_line_start(&self) -> Option<usize> {
let mut index = self.cursor_position;
let mut newline_count = 0;
while index > 0 {
index -= 1;
let is_newline =
matches!(self.msg.as_bytes()[index] as char, '\n');

if is_newline {
newline_count += 1;
}
if is_newline && newline_count == 2 {
return Some(index);
}
}
None
}

fn line_start(&self) -> Option<usize> {
let mut index = self.cursor_position;
while index > 0 {
index -= 1;
if self.msg.as_bytes()[index] as char == '\n' {
return Some(index);
}
}
None
}

fn cursor_up(&mut self) {
if self.cursor_position == 0 {
return;
}

let prev_line_start = self.previous_line_start().unwrap_or(0);
let line_start = self.line_start().unwrap_or(0);

if line_start == prev_line_start {
self.cursor_position = line_start;
return;
}

let mut dist =
self.get_real_distance(line_start, self.cursor_position);
self.cursor_position = prev_line_start;

if prev_line_start == 0 && dist > 1 {
dist -= 1;
}

self.cursor_forward(dist);
}

fn line_end(&self) -> Option<usize> {
let mut index = self.cursor_position;
while index < self.msg.len() - 1 {
if self.msg.as_bytes()[index] as char == '\n' {
return Some(index);
}
index += 1;
}
None
}

fn next_line_end(&self) -> Option<usize> {
let mut index = self.cursor_position;
let mut newline_count = 0;
while index < self.msg.len() - 1 {
if !self.msg.is_char_boundary(index) {
index += 1;
continue;
}
let is_newline =
matches!(self.msg.as_bytes()[index] as char, '\n');

if is_newline {
newline_count += 1;
}
if is_newline && newline_count == 2 {
return Some(index);
}
index += 1;
}
None
}

fn cursor_down(&mut self) {
let line_start = self.line_start().unwrap_or(0);
let line_end = self.line_end().unwrap_or(self.msg.len());
let next_end = self.next_line_end().unwrap_or(self.msg.len());

if line_end == next_end {
self.cursor_position = next_end;
return;
}

if self.cursor_position - line_start > next_end {
self.cursor_position = next_end;
return;
}

let mut dist =
self.get_real_distance(line_start, self.cursor_position);

self.cursor_position = line_end;

if line_start != 0 {
dist -= 1;
}

self.cursor_forward(dist + 1);
}

fn get_real_distance(&self, start: usize, end: usize) -> usize {
let mut index = start;
let mut dist = 0;
while index <= end {
if self.msg.is_char_boundary(index) {
dist += 1;
}
index += 1;
}
dist
}

/// Move forward `distance` amount of characters stopping at a newline
fn cursor_forward(&mut self, distance: usize) {
let mut travelled = 0;
let mut index = self.cursor_position;
while index < self.msg.len() + 1 {
if self.msg.is_char_boundary(index) {
travelled += 1;
}

if travelled == distance {
self.cursor_position = index;
break;
}

if index == self.msg.len() - 1 {
self.cursor_position = index + 1;
break;
}

index += 1;

if index != self.msg.len() - 1
&& self.msg.as_bytes()[index] as char == '\n'
{
self.cursor_position = index;
break;
}
}
}

fn backspace(&mut self) {
if self.cursor_position > 0 {
self.decr_cursor();
Expand Down Expand Up @@ -303,7 +458,7 @@ impl TextInputComponent {
}

fn draw_char_count<B: Backend>(&self, f: &mut Frame<B>, r: Rect) {
let count = self.msg.len();
let count = self.msg.chars().count();
if count > 0 {
let w = Paragraph::new(format!("[{count} chars]"))
.alignment(Alignment::Right);
Expand Down Expand Up @@ -374,7 +529,7 @@ impl DrawableComponent for TextInputComponent {
area,
)
}
_ => ui::centered_rect_absolute(32, 3, f.size()),
_ => ui::centered_rect_absolute(64, 3, f.size()),
}
};

Expand Down Expand Up @@ -430,6 +585,14 @@ impl Component for TextInputComponent {
e.modifiers.contains(KeyModifiers::CONTROL);

match e.code {
KeyCode::Enter
if self.input_type
== InputType::Multiline && !is_ctrl =>
{
self.msg.insert(self.cursor_position, '\n');
self.incr_cursor();
return Ok(EventState::Consumed);
}
KeyCode::Char(c) if !is_ctrl => {
self.msg.insert(self.cursor_position, c);
self.incr_cursor();
Expand Down Expand Up @@ -490,6 +653,14 @@ impl Component for TextInputComponent {
self.incr_cursor();
return Ok(EventState::Consumed);
}
KeyCode::Up => {
self.cursor_up();
return Ok(EventState::Consumed);
}
KeyCode::Down => {
self.cursor_down();
return Ok(EventState::Consumed);
}
KeyCode::Home => {
self.cursor_position = 0;
return Ok(EventState::Consumed);
Expand Down Expand Up @@ -717,6 +888,55 @@ mod tests {
assert_eq!(comp.previous_word_position(), None);
}

#[test]
fn test_line_change() {
let mut comp = TextInputComponent::new(
SharedTheme::default(),
SharedKeyConfig::default(),
"",
"",
false,
);

comp.set_text(String::from("aaaaa\näaa\naaa\naaa"));

comp.cursor_position = 0;
comp.cursor_down();
assert_eq!(comp.cursor_position, 6);

comp.cursor_position = 2;
comp.cursor_down();
assert_eq!(comp.cursor_position, 9);

comp.cursor_position = 10;
comp.cursor_down();
assert_eq!(comp.cursor_position, 14);

comp.cursor_position = 8;
comp.cursor_down();
assert_eq!(comp.cursor_position, 12);

comp.cursor_position = 13;
comp.cursor_down();
assert_eq!(comp.cursor_position, 17);

comp.cursor_position = 6;
comp.cursor_up();
assert_eq!(comp.cursor_position, 0);

comp.cursor_position = 9;
comp.cursor_up();
assert_eq!(comp.cursor_position, 2);

comp.cursor_position = 0;
comp.cursor_up();
assert_eq!(comp.cursor_position, 0);

comp.cursor_position = 4;
comp.cursor_up();
assert_eq!(comp.cursor_position, 0);
}

#[test]
fn test_next_word_multibyte() {
let mut comp = TextInputComponent::new(
Expand Down
2 changes: 2 additions & 0 deletions src/keys/key_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct KeysList {
pub quit: GituiKeyEvent,
pub exit_popup: GituiKeyEvent,
pub open_commit: GituiKeyEvent,
pub confirm_commit: GituiKeyEvent,
pub open_commit_editor: GituiKeyEvent,
pub open_help: GituiKeyEvent,
pub open_options: GituiKeyEvent,
Expand Down Expand Up @@ -134,6 +135,7 @@ impl Default for KeysList {
quit: GituiKeyEvent::new(KeyCode::Char('q'), KeyModifiers::empty()),
exit_popup: GituiKeyEvent::new(KeyCode::Esc, KeyModifiers::empty()),
open_commit: GituiKeyEvent::new(KeyCode::Char('c'), KeyModifiers::empty()),
confirm_commit: GituiKeyEvent::new(KeyCode::Char('s'), KeyModifiers::CONTROL),
open_commit_editor: GituiKeyEvent::new(KeyCode::Char('e'), KeyModifiers::CONTROL),
open_help: GituiKeyEvent::new(KeyCode::Char('h'), KeyModifiers::empty()),
open_options: GituiKeyEvent::new(KeyCode::Char('o'), KeyModifiers::empty()),
Expand Down
2 changes: 2 additions & 0 deletions src/keys/key_list_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct KeysListFile {
pub quit: Option<GituiKeyEvent>,
pub exit_popup: Option<GituiKeyEvent>,
pub open_commit: Option<GituiKeyEvent>,
pub confirm_commit: Option<GituiKeyEvent>,
pub open_commit_editor: Option<GituiKeyEvent>,
pub open_help: Option<GituiKeyEvent>,
pub open_options: Option<GituiKeyEvent>,
Expand Down Expand Up @@ -114,6 +115,7 @@ impl KeysListFile {
quit: self.quit.unwrap_or(default.quit),
exit_popup: self.exit_popup.unwrap_or(default.exit_popup),
open_commit: self.open_commit.unwrap_or(default.open_commit),
confirm_commit: self.commit_amend.unwrap_or(default.confirm_commit),
open_commit_editor: self.open_commit_editor.unwrap_or(default.open_commit_editor),
open_help: self.open_help.unwrap_or(default.open_help),
open_options: self.open_options.unwrap_or(default.open_options),
Expand Down
2 changes: 1 addition & 1 deletion src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ pub mod commands {
CommandText::new(
format!(
"Commit [{}]",
key_config.get_hint(key_config.keys.enter),
key_config.get_hint(key_config.keys.confirm_commit),
),
"commit (available when commit message is non-empty)",
CMD_GROUP_COMMIT,
Expand Down