From 27045b64cd5ff4190ef7a69734183ffb481f2fce Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Mon, 16 Mar 2020 14:42:41 +0100 Subject: [PATCH] show full diff --- src/app.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/src/app.rs b/src/app.rs index d126b361..1f463fa5 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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, status_select: Option, + 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>( .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); }