Skip to content

Commit 33cced6

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

3 files changed

Lines changed: 65 additions & 6 deletions

File tree

src/popups/create_branch.rs

Lines changed: 21 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
}

src/popups/rename_branch.rs

Lines changed: 21 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
}

src/strings.rs

Lines changed: 23 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,
@@ -1934,3 +1941,19 @@ pub mod commands {
19341941
)
19351942
}
19361943
}
1944+
1945+
#[cfg(test)]
1946+
mod tests {
1947+
use super::*;
1948+
1949+
#[test]
1950+
fn test_spaces_are_replaced_by_dashes_in_branch_name() {
1951+
let input = "feature/auto replace spaces in branch name";
1952+
let output: String =
1953+
input.chars().map(normalize_branch_name_char).collect();
1954+
assert_eq!(
1955+
output,
1956+
"feature/auto-replace-spaces-in-branch-name"
1957+
);
1958+
}
1959+
}

0 commit comments

Comments
 (0)