feat: add branch name validation on renaming (#2081)

Closes #2062 

---------

Co-authored-by: extrawurst <776816+extrawurst@users.noreply.github.com>
This commit is contained in:
Sainath Singineedi 2024-02-21 04:39:53 +05:30 committed by GitHub
parent a50a478c1a
commit fa051eff5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 3 deletions

View File

@ -29,6 +29,7 @@ These defaults require some adoption from existing users but feel more natural t
* support `prepare-commit-msg` hook ([#1873](https://github.com/extrawurst/gitui/issues/1873)) * support `prepare-commit-msg` hook ([#1873](https://github.com/extrawurst/gitui/issues/1873))
* new style `block_title_focused` to allow customizing title text of focused frame/block ([#2052](https://github.com/extrawurst/gitui/issues/2052)). * new style `block_title_focused` to allow customizing title text of focused frame/block ([#2052](https://github.com/extrawurst/gitui/issues/2052)).
* allow `fetch` command in both tabs of branchlist popup ([#2067](https://github.com/extrawurst/gitui/issues/2067)) * allow `fetch` command in both tabs of branchlist popup ([#2067](https://github.com/extrawurst/gitui/issues/2067))
* check branch name validity while typing [[@sainad2222](https://github.com/sainad2222)] ([#2062](https://github.com/extrawurst/gitui/issues/2062))
### Changed ### Changed
* do not allow tagging when `tag.gpgsign` enabled until gpg-signing is [supported](https://github.com/extrawurst/gitui/issues/97) [[@TeFiLeDo](https://github.com/TeFiLeDo)] ([#1915](https://github.com/extrawurst/gitui/pull/1915)) * do not allow tagging when `tag.gpgsign` enabled until gpg-signing is [supported](https://github.com/extrawurst/gitui/issues/97) [[@TeFiLeDo](https://github.com/TeFiLeDo)] ([#1915](https://github.com/extrawurst/gitui/pull/1915))

View File

@ -2,6 +2,7 @@ use crate::components::{
visibility_blocking, CommandBlocking, CommandInfo, Component, visibility_blocking, CommandBlocking, CommandInfo, Component,
DrawableComponent, EventState, InputType, TextInputComponent, DrawableComponent, EventState, InputType, TextInputComponent,
}; };
use crate::ui::style::SharedTheme;
use crate::{ use crate::{
app::Environment, app::Environment,
keys::{key_match, SharedKeyConfig}, keys::{key_match, SharedKeyConfig},
@ -11,7 +12,8 @@ use crate::{
use anyhow::Result; use anyhow::Result;
use asyncgit::sync::{self, RepoPathRef}; use asyncgit::sync::{self, RepoPathRef};
use crossterm::event::Event; use crossterm::event::Event;
use ratatui::{layout::Rect, Frame}; use easy_cast::Cast;
use ratatui::{layout::Rect, widgets::Paragraph, Frame};
pub struct RenameBranchPopup { pub struct RenameBranchPopup {
repo: RepoPathRef, repo: RepoPathRef,
@ -19,12 +21,15 @@ pub struct RenameBranchPopup {
branch_ref: Option<String>, branch_ref: Option<String>,
queue: Queue, queue: Queue,
key_config: SharedKeyConfig, key_config: SharedKeyConfig,
theme: SharedTheme,
} }
impl DrawableComponent for RenameBranchPopup { impl DrawableComponent for RenameBranchPopup {
fn draw(&self, f: &mut Frame, rect: Rect) -> Result<()> { fn draw(&self, f: &mut Frame, rect: Rect) -> Result<()> {
self.input.draw(f, rect)?; if self.is_visible() {
self.input.draw(f, rect)?;
self.draw_warnings(f);
}
Ok(()) Ok(())
} }
} }
@ -97,6 +102,7 @@ impl RenameBranchPopup {
.with_input_type(InputType::Singleline), .with_input_type(InputType::Singleline),
branch_ref: None, branch_ref: None,
key_config: env.key_config.clone(), key_config: env.key_config.clone(),
theme: env.theme.clone(),
} }
} }
@ -148,4 +154,35 @@ impl RenameBranchPopup {
self.input.clear(); self.input.clear();
} }
fn draw_warnings(&self, f: &mut Frame) {
let current_text = self.input.get_text();
if !current_text.is_empty() {
let valid = sync::validate_branch_name(current_text)
.unwrap_or_default();
if !valid {
let msg = strings::branch_name_invalid();
let msg_length: u16 = msg.len().cast();
let w = Paragraph::new(msg)
.style(self.theme.text_danger());
let rect = {
let mut rect = self.input.get_area();
rect.y += rect.height.saturating_sub(1);
rect.height = 1;
let offset =
rect.width.saturating_sub(msg_length + 1);
rect.width =
rect.width.saturating_sub(offset + 1);
rect.x += offset;
rect
};
f.render_widget(w, rect);
}
}
}
} }