show full diff

This commit is contained in:
Stephan Dilly 2020-03-16 14:42:41 +01:00
parent 580d32972f
commit 27045b64cd

View File

@ -1,11 +1,11 @@
use crossterm::event::{Event, KeyCode};
use git2::Repository;
use git2::{DiffFormat, Repository};
use std::cmp;
use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
layout::{Constraint, Direction, Layout, Rect, Alignment},
style::{Color, Modifier, Style},
widgets::{Block, Borders, SelectableList, Widget},
widgets::{Block, Borders, SelectableList, Widget, Paragraph, Text},
Frame,
};
@ -13,6 +13,8 @@ use tui::{
pub struct App {
status_items: Vec<String>,
status_select: Option<usize>,
diff: String,
offset:u16,
do_quit: bool,
}
@ -50,6 +52,30 @@ impl App {
} else {
None
};
self.diff = self.get_diff();
}
///
pub fn get_diff(&mut self) -> String {
let repo = Repository::init("./").unwrap();
if repo.is_bare() {
panic!("bare repo")
}
let diff = repo.diff_index_to_workdir(None, None).unwrap();
let mut res = String::new();
diff.print(DiffFormat::Patch, |_delta, _hunk, line| {
let content = String::from_utf8_lossy(line.content());
res.push_str(content.chars().as_str());
true
})
.unwrap();
res
}
///
@ -67,10 +93,17 @@ impl App {
self.status_select,
);
Block::default()
.title("Block 2")
.borders(Borders::ALL)
Paragraph::new([Text::raw(self.diff.clone())].iter())
.block(Block::default().title("Diff").borders(Borders::ALL))
.style(Style::default().fg(Color::White).bg(Color::Black))
.alignment(Alignment::Left)
.scroll(self.offset)
.render(f, chunks[1]);
// Block::default()
// .title("Diff")
// .borders(Borders::ALL)
// .render(f, chunks[1]);
}
///
@ -85,8 +118,41 @@ impl App {
if ev == Event::Key(KeyCode::Down.into()) {
self.input(1);
}
if ev == Event::Key(KeyCode::PageDown.into()) {
self.offset+=1;
}
if ev == Event::Key(KeyCode::PageUp.into()) {
if self.offset>0{
self.offset-=1;
}
}
if ev == Event::Key(KeyCode::Enter.into()) {
// self.index_add();
}
}
// fn index_add(&mut self) {
// let repo = Repository::init("./").unwrap();
// let status = repo.statuses(None).unwrap();
// let index = repo.index().unwrap();
// index.add(entry)
// self.status_items = status
// .iter()
// .map(|e| e.path().unwrap().to_string())
// .collect();
// self.status_select = if self.status_items.len() > 0 {
// Some(0)
// } else {
// None
// };
// }
fn input(&mut self, delta: i32) {
let items_len = self.status_items.len();
if items_len > 0 {
@ -114,7 +180,7 @@ fn draw_list<B: Backend, T: AsRef<str>>(
.items(items)
.select(select)
.style(Style::default().fg(Color::White))
.highlight_style(Style::default().modifier(Modifier::ITALIC))
.highlight_style(Style::default().modifier(Modifier::BOLD))
.highlight_symbol(">")
.render(f, r);
}