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 crossterm::event::{Event, KeyCode};
use git2::Repository; use git2::{DiffFormat, Repository};
use std::cmp; use std::cmp;
use tui::{ use tui::{
backend::Backend, backend::Backend,
layout::{Constraint, Direction, Layout, Rect}, layout::{Constraint, Direction, Layout, Rect, Alignment},
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
widgets::{Block, Borders, SelectableList, Widget}, widgets::{Block, Borders, SelectableList, Widget, Paragraph, Text},
Frame, Frame,
}; };
@ -13,6 +13,8 @@ use tui::{
pub struct App { pub struct App {
status_items: Vec<String>, status_items: Vec<String>,
status_select: Option<usize>, status_select: Option<usize>,
diff: String,
offset:u16,
do_quit: bool, do_quit: bool,
} }
@ -50,6 +52,30 @@ impl App {
} else { } else {
None 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, self.status_select,
); );
Block::default() Paragraph::new([Text::raw(self.diff.clone())].iter())
.title("Block 2") .block(Block::default().title("Diff").borders(Borders::ALL))
.borders(Borders::ALL) .style(Style::default().fg(Color::White).bg(Color::Black))
.alignment(Alignment::Left)
.scroll(self.offset)
.render(f, chunks[1]); .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()) { if ev == Event::Key(KeyCode::Down.into()) {
self.input(1); 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) { fn input(&mut self, delta: i32) {
let items_len = self.status_items.len(); let items_len = self.status_items.len();
if items_len > 0 { if items_len > 0 {
@ -114,7 +180,7 @@ fn draw_list<B: Backend, T: AsRef<str>>(
.items(items) .items(items)
.select(select) .select(select)
.style(Style::default().fg(Color::White)) .style(Style::default().fg(Color::White))
.highlight_style(Style::default().modifier(Modifier::ITALIC)) .highlight_style(Style::default().modifier(Modifier::BOLD))
.highlight_symbol(">") .highlight_symbol(">")
.render(f, r); .render(f, r);
} }