show index items separately

This commit is contained in:
Stephan Dilly 2020-03-16 15:34:12 +01:00
parent 27045b64cd
commit 6baf16355c

View file

@ -1,20 +1,21 @@
use crossterm::event::{Event, KeyCode}; use crossterm::event::{Event, KeyCode};
use git2::{DiffFormat, Repository}; use git2::{DiffFormat, Repository, Status};
use std::cmp; use std::cmp;
use tui::{ use tui::{
backend::Backend, backend::Backend,
layout::{Constraint, Direction, Layout, Rect, Alignment}, layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
widgets::{Block, Borders, SelectableList, Widget, Paragraph, Text}, widgets::{Block, Borders, Paragraph, SelectableList, Text, Widget},
Frame, Frame,
}; };
#[derive(Default)] #[derive(Default)]
pub struct App { pub struct App {
status_items: Vec<String>, status_items: Vec<String>,
index_items: Vec<String>,
status_select: Option<usize>, status_select: Option<usize>,
diff: String, diff: String,
offset:u16, offset: u16,
do_quit: bool, do_quit: bool,
} }
@ -33,19 +34,29 @@ impl App {
Err(e) => panic!("failed to init: {}", e), Err(e) => panic!("failed to init: {}", e),
}; };
// println!("state: {:?}",repo.state());
// println!("path: {:?}",repo.path());
if repo.is_bare() { if repo.is_bare() {
panic!("bare repo") panic!("bare repo")
} }
let status = repo.statuses(None).unwrap(); let statuses = repo.statuses(None).unwrap();
self.status_items = status self.status_items = Vec::new();
.iter() self.index_items= Vec::new();
.map(|e| e.path().unwrap().to_string())
.collect(); for e in statuses.iter() {
let status: Status = e.status();
if status.is_ignored() {continue;}
if status.is_index_new() || status.is_index_modified() {
self.index_items
.push(format!("{} ({:?})", e.path().unwrap().to_string(), status))
}
if status.is_wt_new() || status.is_wt_modified() {
self.status_items
.push(format!("{} ({:?})", e.path().unwrap().to_string(), status))
}
}
self.status_select = if self.status_items.len() > 0 { self.status_select = if self.status_items.len() > 0 {
Some(0) Some(0)
@ -85,25 +96,33 @@ impl App {
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(f.size()); .split(f.size());
let left_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(chunks[0]);
draw_list( draw_list(
f, f,
chunks[0], left_chunks[0],
"Status".to_string(), "Status".to_string(),
self.status_items.as_slice(), self.status_items.as_slice(),
self.status_select, self.status_select,
); );
draw_list(
f,
left_chunks[1],
"Index".to_string(),
self.index_items.as_slice(),
None,
);
Paragraph::new([Text::raw(self.diff.clone())].iter()) Paragraph::new([Text::raw(self.diff.clone())].iter())
.block(Block::default().title("Diff").borders(Borders::ALL)) .block(Block::default().title("Diff").borders(Borders::ALL))
.style(Style::default().fg(Color::White).bg(Color::Black)) .style(Style::default().fg(Color::White).bg(Color::Black))
.alignment(Alignment::Left) .alignment(Alignment::Left)
.scroll(self.offset) .scroll(self.offset)
.render(f, chunks[1]); .render(f, chunks[1]);
// Block::default()
// .title("Diff")
// .borders(Borders::ALL)
// .render(f, chunks[1]);
} }
/// ///
@ -120,11 +139,11 @@ impl App {
} }
if ev == Event::Key(KeyCode::PageDown.into()) { if ev == Event::Key(KeyCode::PageDown.into()) {
self.offset+=1; self.offset += 1;
} }
if ev == Event::Key(KeyCode::PageUp.into()) { if ev == Event::Key(KeyCode::PageUp.into()) {
if self.offset>0{ if self.offset > 0 {
self.offset-=1; self.offset -= 1;
} }
} }