Skip to content

Commit 8cefa85

Browse files
committed
Automatically convert spaces to dashes in branch names
1 parent 8619c07 commit 8cefa85

3 files changed

Lines changed: 81 additions & 6 deletions

File tree

src/popups/create_branch.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ use crate::{
1111
};
1212
use anyhow::Result;
1313
use asyncgit::sync::{self, RepoPathRef};
14-
use crossterm::event::Event;
14+
use crossterm::event::{Event, KeyCode, KeyEvent};
1515
use easy_cast::Cast;
1616
use ratatui::{layout::Rect, widgets::Paragraph, Frame};
17+
use std::borrow::Cow;
1718

1819
pub struct CreateBranchPopup {
1920
repo: RepoPathRef,
@@ -57,11 +58,28 @@ impl Component for CreateBranchPopup {
5758

5859
fn event(&mut self, ev: &Event) -> Result<EventState> {
5960
if self.is_visible() {
60-
if self.input.event(ev)?.is_consumed() {
61+
let ev = match ev {
62+
Event::Key(KeyEvent {
63+
code: KeyCode::Char(c),
64+
modifiers,
65+
kind,
66+
state,
67+
}) => Cow::Owned(Event::Key(KeyEvent {
68+
code: KeyCode::Char(strings::normalize_branch_name_char(
69+
*c,
70+
)),
71+
modifiers: *modifiers,
72+
kind: *kind,
73+
state: *state,
74+
})),
75+
_ => Cow::Borrowed(ev),
76+
};
77+
78+
if self.input.event(&ev)?.is_consumed() {
6179
return Ok(EventState::Consumed);
6280
}
6381

64-
if let Event::Key(e) = ev {
82+
if let Event::Key(e) = ev.as_ref() {
6583
if key_match(e, self.key_config.keys.enter) {
6684
self.create_branch();
6785
}
@@ -168,3 +186,19 @@ impl CreateBranchPopup {
168186
}
169187
}
170188
}
189+
190+
#[cfg(test)]
191+
mod tests {
192+
use super::*;
193+
194+
#[test]
195+
fn test_spaces_are_replaced_by_dashes_in_branch_name() {
196+
let input = "feature/auto replace spaces in branch name";
197+
let output: String =
198+
input.chars().map(normalize_branch_name_char).collect();
199+
assert_eq!(
200+
output,
201+
"feature/auto-replace-spaces-in-branch-name"
202+
);
203+
}
204+
}

src/popups/rename_branch.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ use crate::{
1111
};
1212
use anyhow::Result;
1313
use asyncgit::sync::{self, RepoPathRef};
14-
use crossterm::event::Event;
14+
use crossterm::event::{Event, KeyCode, KeyEvent};
1515
use easy_cast::Cast;
1616
use ratatui::{layout::Rect, widgets::Paragraph, Frame};
17+
use std::borrow::Cow;
1718

1819
pub struct RenameBranchPopup {
1920
repo: RepoPathRef,
@@ -57,11 +58,28 @@ impl Component for RenameBranchPopup {
5758

5859
fn event(&mut self, ev: &Event) -> Result<EventState> {
5960
if self.is_visible() {
60-
if self.input.event(ev)?.is_consumed() {
61+
let ev = match ev {
62+
Event::Key(KeyEvent {
63+
code: KeyCode::Char(c),
64+
modifiers,
65+
kind,
66+
state,
67+
}) => Cow::Owned(Event::Key(KeyEvent {
68+
code: KeyCode::Char(strings::normalize_branch_name_char(
69+
*c,
70+
)),
71+
modifiers: *modifiers,
72+
kind: *kind,
73+
state: *state,
74+
})),
75+
_ => Cow::Borrowed(ev),
76+
};
77+
78+
if self.input.event(&ev)?.is_consumed() {
6179
return Ok(EventState::Consumed);
6280
}
6381

64-
if let Event::Key(e) = ev {
82+
if let Event::Key(e) = ev.as_ref() {
6583
if key_match(e, self.key_config.keys.enter) {
6684
self.rename_branch();
6785
}
@@ -186,3 +204,19 @@ impl RenameBranchPopup {
186204
}
187205
}
188206
}
207+
208+
#[cfg(test)]
209+
mod tests {
210+
use super::*;
211+
212+
#[test]
213+
fn test_spaces_are_replaced_by_dashes_in_branch_name() {
214+
let input = "feature/auto replace spaces in branch name";
215+
let output: String =
216+
input.chars().map(normalize_branch_name_char).collect();
217+
assert_eq!(
218+
output,
219+
"feature/auto-replace-spaces-in-branch-name"
220+
);
221+
}
222+
}

src/strings.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,13 @@ pub fn ellipsis_trim_start(s: &str, width: usize) -> Cow<'_, str> {
438438
}
439439
}
440440

441+
pub const fn normalize_branch_name_char(c: char) -> char {
442+
match c {
443+
' ' => '-',
444+
c => c,
445+
}
446+
}
447+
441448
#[derive(PartialEq, Eq, Clone, Copy)]
442449
pub enum CheckoutOptions {
443450
KeepLocalChanges,

0 commit comments

Comments
 (0)