Add branch name in Status and ChangesComponent cleanup (#375)

This commit is contained in:
remique 2020-10-26 16:21:26 +01:00 committed by GitHub
parent 7596422da4
commit 35f3a25cba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 43 deletions

View File

@ -11,18 +11,16 @@ use crate::{
ui::style::SharedTheme,
};
use anyhow::Result;
use asyncgit::{cached, sync, StatusItem, StatusItemType, CWD};
use asyncgit::{sync, StatusItem, StatusItemType, CWD};
use crossterm::event::Event;
use std::path::Path;
use tui::{backend::Backend, layout::Rect, Frame};
///
pub struct ChangesComponent {
title: String,
files: FileTreeComponent,
is_working_dir: bool,
queue: Queue,
branch_name: cached::BranchName,
key_config: SharedKeyConfig,
}
@ -37,7 +35,6 @@ impl ChangesComponent {
key_config: SharedKeyConfig,
) -> Self {
Self {
title: title.into(),
files: FileTreeComponent::new(
title,
focus,
@ -47,28 +44,10 @@ impl ChangesComponent {
),
is_working_dir,
queue,
branch_name: cached::BranchName::new(CWD),
key_config,
}
}
pub fn update(&mut self) -> Result<()> {
if self.is_working_dir {
if let Ok(branch_name) = self.branch_name.lookup() {
self.files.set_title(format!(
"{} - {{{}}}",
&self.title, branch_name,
))
}
}
Ok(())
}
///
pub fn branch_name(&self) -> Option<String> {
self.branch_name.last()
}
///
pub fn set_items(&mut self, list: &[StatusItem]) -> Result<()> {
self.files.update(list)?;

View File

@ -12,6 +12,7 @@ use crate::{
};
use anyhow::Result;
use asyncgit::{
cached,
sync::BranchCompare,
sync::{self, status::StatusType},
AsyncDiff, AsyncNotification, AsyncStatus, DiffParams, DiffType,
@ -50,6 +51,7 @@ pub struct Status {
git_status_workdir: AsyncStatus,
git_status_stage: AsyncStatus,
git_branch_state: BranchCompare,
git_branch_name: cached::BranchName,
queue: Queue,
git_action_executed: bool,
key_config: SharedKeyConfig,
@ -148,6 +150,7 @@ impl Status {
git_status_stage: AsyncStatus::new(sender.clone()),
git_action_executed: false,
git_branch_state: BranchCompare::default(),
git_branch_name: cached::BranchName::new(CWD),
key_config,
}
}
@ -157,26 +160,31 @@ impl Status {
f: &mut tui::Frame<B>,
chunks: &[tui::layout::Rect],
) {
let w = Paragraph::new(format!(
"\u{2191}{} \u{2193}{}",
self.git_branch_state.ahead, self.git_branch_state.behind
))
.alignment(Alignment::Right);
if let Some(branch_name) = self.git_branch_name.last() {
let w = Paragraph::new(format!(
"\u{2191}{} \u{2193}{} {{{}}}",
self.git_branch_state.ahead,
self.git_branch_state.behind,
branch_name
))
.alignment(Alignment::Right);
let mut rect = if self.index_wd.focused() {
let mut rect = chunks[0];
rect.y += rect.height.saturating_sub(1);
rect
} else {
chunks[1]
};
let mut rect = if self.index_wd.focused() {
let mut rect = chunks[0];
rect.y += rect.height.saturating_sub(1);
rect
} else {
chunks[1]
};
rect.x += 1;
rect.width = rect.width.saturating_sub(2);
rect.height =
rect.height.saturating_sub(rect.height.saturating_sub(1));
rect.x += 1;
rect.width = rect.width.saturating_sub(2);
rect.height = rect
.height
.saturating_sub(rect.height.saturating_sub(1));
f.render_widget(w, rect);
f.render_widget(w, rect);
}
}
fn can_focus_diff(&self) -> bool {
@ -240,6 +248,8 @@ impl Status {
///
pub fn update(&mut self) -> Result<()> {
self.git_branch_name.lookup().map(Some).unwrap_or(None);
if self.is_visible() {
self.git_diff.refresh()?;
self.git_status_workdir.fetch(StatusParams::new(
@ -249,7 +259,6 @@ impl Status {
self.git_status_stage
.fetch(StatusParams::new(StatusType::Stage, true))?;
self.index_wd.update()?;
self.check_branch_state();
}
@ -360,7 +369,7 @@ impl Status {
}
fn push(&self) {
if let Some(branch) = self.index_wd.branch_name() {
if let Some(branch) = self.git_branch_name.last() {
let branch = format!("refs/heads/{}", branch);
self.queue
@ -370,7 +379,7 @@ impl Status {
}
fn fetch(&self) {
if let Some(branch) = self.index_wd.branch_name() {
if let Some(branch) = self.git_branch_name.last() {
match sync::fetch_origin(CWD, branch.as_str()) {
Err(e) => {
self.queue.borrow_mut().push_back(
@ -393,7 +402,7 @@ impl Status {
}
fn check_branch_state(&mut self) {
self.git_branch_state = self.index_wd.branch_name().map_or(
self.git_branch_state = self.git_branch_name.last().map_or(
BranchCompare::default(),
|branch| {
sync::branch_compare_upstream(CWD, branch.as_str())