Skip to content

Commit 5eca3a5

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

2 files changed

Lines changed: 88 additions & 6 deletions

File tree

src/popups/create_branch.rs

Lines changed: 44 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,
@@ -34,6 +35,13 @@ impl DrawableComponent for CreateBranchPopup {
3435
}
3536
}
3637

38+
const fn normalize_branch_name_char(c: char) -> char {
39+
match c {
40+
' ' => '-',
41+
c => c,
42+
}
43+
}
44+
3745
impl Component for CreateBranchPopup {
3846
fn commands(
3947
&self,
@@ -57,11 +65,28 @@ impl Component for CreateBranchPopup {
5765

5866
fn event(&mut self, ev: &Event) -> Result<EventState> {
5967
if self.is_visible() {
60-
if self.input.event(ev)?.is_consumed() {
68+
let ev = match ev {
69+
Event::Key(KeyEvent {
70+
code: KeyCode::Char(c),
71+
modifiers,
72+
kind,
73+
state,
74+
}) => Cow::Owned(Event::Key(KeyEvent {
75+
code: KeyCode::Char(normalize_branch_name_char(
76+
*c,
77+
)),
78+
modifiers: *modifiers,
79+
kind: *kind,
80+
state: *state,
81+
})),
82+
_ => Cow::Borrowed(ev),
83+
};
84+
85+
if self.input.event(&ev)?.is_consumed() {
6186
return Ok(EventState::Consumed);
6287
}
6388

64-
if let Event::Key(e) = ev {
89+
if let Event::Key(e) = ev.as_ref() {
6590
if key_match(e, self.key_config.keys.enter) {
6691
self.create_branch();
6792
}
@@ -168,3 +193,19 @@ impl CreateBranchPopup {
168193
}
169194
}
170195
}
196+
197+
#[cfg(test)]
198+
mod tests {
199+
use super::*;
200+
201+
#[test]
202+
fn test_spaces_are_replaced_by_dashes_in_branch_name() {
203+
let input = "feature/auto replace spaces in branch name";
204+
let output: String =
205+
input.chars().map(normalize_branch_name_char).collect();
206+
assert_eq!(
207+
output,
208+
"feature/auto-replace-spaces-in-branch-name"
209+
);
210+
}
211+
}

src/popups/rename_branch.rs

Lines changed: 44 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,
@@ -34,6 +35,13 @@ impl DrawableComponent for RenameBranchPopup {
3435
}
3536
}
3637

38+
const fn normalize_branch_name_char(c: char) -> char {
39+
match c {
40+
' ' => '-',
41+
c => c,
42+
}
43+
}
44+
3745
impl Component for RenameBranchPopup {
3846
fn commands(
3947
&self,
@@ -57,11 +65,28 @@ impl Component for RenameBranchPopup {
5765

5866
fn event(&mut self, ev: &Event) -> Result<EventState> {
5967
if self.is_visible() {
60-
if self.input.event(ev)?.is_consumed() {
68+
let ev = match ev {
69+
Event::Key(KeyEvent {
70+
code: KeyCode::Char(c),
71+
modifiers,
72+
kind,
73+
state,
74+
}) => Cow::Owned(Event::Key(KeyEvent {
75+
code: KeyCode::Char(normalize_branch_name_char(
76+
*c,
77+
)),
78+
modifiers: *modifiers,
79+
kind: *kind,
80+
state: *state,
81+
})),
82+
_ => Cow::Borrowed(ev),
83+
};
84+
85+
if self.input.event(&ev)?.is_consumed() {
6186
return Ok(EventState::Consumed);
6287
}
6388

64-
if let Event::Key(e) = ev {
89+
if let Event::Key(e) = ev.as_ref() {
6590
if key_match(e, self.key_config.keys.enter) {
6691
self.rename_branch();
6792
}
@@ -186,3 +211,19 @@ impl RenameBranchPopup {
186211
}
187212
}
188213
}
214+
215+
#[cfg(test)]
216+
mod tests {
217+
use super::*;
218+
219+
#[test]
220+
fn test_spaces_are_replaced_by_dashes_in_branch_name() {
221+
let input = "feature/auto replace spaces in branch name";
222+
let output: String =
223+
input.chars().map(normalize_branch_name_char).collect();
224+
assert_eq!(
225+
output,
226+
"feature/auto-replace-spaces-in-branch-name"
227+
);
228+
}
229+
}

0 commit comments

Comments
 (0)