bin: Cleans up hunk-to-text conversion

This commit is contained in:
Erik Kundt
2024-11-20 16:45:23 +01:00
parent d983b7ccd6
commit 70f89884b5
2 changed files with 17 additions and 24 deletions
+6 -13
View File
@@ -8,17 +8,15 @@ use std::sync::Mutex;
use anyhow::Result;
use radicle::patch::Review;
use ratatui::layout::Position;
use ratatui::layout::{Constraint, Layout};
use ratatui::style::Stylize;
use ratatui::text::Text;
use ratatui::{Frame, Viewport};
use termion::event::Key;
use ratatui::layout::{Constraint, Layout};
use ratatui::{Frame, Viewport};
use radicle::identity::RepoId;
use radicle::storage::git::Repository;
use radicle::patch::Review;
use radicle::storage::ReadStorage;
use radicle::Profile;
@@ -139,8 +137,6 @@ pub struct ReviewItemState {
#[derive(Clone)]
pub struct App<'a> {
repository: Arc<Mutex<Repository>>,
review: Arc<Mutex<Review>>,
queue: Arc<Mutex<(Vec<HunkItem<'a>>, TableState)>>,
items: HashMap<usize, ReviewItemState>,
page: AppPage,
@@ -165,7 +161,7 @@ impl<'a> App<'a> {
pub fn new(
profile: Profile,
rid: RepoId,
review: Review,
_review: Review,
queue: ReviewQueue,
) -> Result<Self, anyhow::Error> {
let repository = profile.storage.repository(rid)?;
@@ -186,13 +182,11 @@ impl<'a> App<'a> {
}
Ok(Self {
repository: Arc::new(Mutex::new(repository)),
page: AppPage::Main,
windows: GroupState::new(2, Some(0)),
help: HelpState {
text: TextViewState::new(help_text(), Position::default()),
},
review: Arc::new(Mutex::new(review)),
queue: Arc::new(Mutex::new((queue, TableState::new(Some(0))))),
items,
})
@@ -220,7 +214,6 @@ impl<'a> App<'a> {
}
fn show_review_item(&self, ui: &mut Ui<Message>, frame: &mut Frame) {
let repo = self.repository.lock().unwrap();
let queue = self.queue.lock().unwrap();
let selected = queue.1.selected();
@@ -229,7 +222,7 @@ impl<'a> App<'a> {
if let Some(item) = item {
let header = item.header();
let hunk = item
.hunk_text(&repo)
.hunk_text()
.unwrap_or(Text::raw("Nothing to show.").dark_gray());
let mut cursor = selected
@@ -243,7 +236,7 @@ impl<'a> App<'a> {
|ui| {
ui.columns(frame, header, Some(Borders::Top));
if let Some(hunk) = item.hunk_text(&repo) {
if let Some(hunk) = item.hunk_text() {
let diff =
ui.text_view(frame, hunk, &mut cursor, Some(Borders::BottomSides));
if diff.changed {
+11 -11
View File
@@ -1443,7 +1443,7 @@ impl<'a> HunkItem<'a> {
}
}
pub fn hunk_text(&'a self, repo: &Repository) -> Option<Text<'a>> {
pub fn hunk_text(&'a self) -> Option<Text<'a>> {
match &self.inner {
(
_,
@@ -1455,7 +1455,7 @@ impl<'a> HunkItem<'a> {
},
) => hunk
.as_ref()
.map(|hunk| Text::from(hunk.to_text(&self.highlighted, repo.raw()))),
.map(|hunk| Text::from(hunk.to_text(&self.highlighted))),
(
_,
crate::cob::HunkItem::FileModified {
@@ -1467,7 +1467,7 @@ impl<'a> HunkItem<'a> {
},
) => hunk
.as_ref()
.map(|hunk| Text::from(hunk.to_text(&self.highlighted, repo.raw()))),
.map(|hunk| Text::from(hunk.to_text(&self.highlighted))),
(
_,
crate::cob::HunkItem::FileDeleted {
@@ -1478,7 +1478,7 @@ impl<'a> HunkItem<'a> {
},
) => hunk
.as_ref()
.map(|hunk| Text::from(hunk.to_text(&self.highlighted, repo.raw()))),
.map(|hunk| Text::from(hunk.to_text(&self.highlighted))),
_ => None,
}
}
@@ -1582,14 +1582,14 @@ pub trait ToText<'a> {
type Context;
/// Render to pretty diff output.
fn to_text<R: Repo>(&'a self, context: &Self::Context, repo: &R) -> Self::Output;
fn to_text(&'a self, context: &Self::Context) -> Self::Output;
}
impl<'a> ToText<'a> for HunkHeader {
type Output = Line<'a>;
type Context = ();
fn to_text<R: Repo>(&self, _context: &Self::Context, _repo: &R) -> Self::Output {
fn to_text(&self, _context: &Self::Context) -> Self::Output {
Line::from(
[
span::default(&format!(
@@ -1609,7 +1609,7 @@ impl<'a> ToText<'a> for Modification {
type Output = Line<'a>;
type Context = Blobs<Vec<Line<'a>>>;
fn to_text<R: Repo>(&'a self, blobs: &Blobs<Vec<Line<'a>>>, _repo: &R) -> Self::Output {
fn to_text(&'a self, blobs: &Blobs<Vec<Line<'a>>>) -> Self::Output {
let line = match self {
Modification::Deletion(diff::Deletion { line, line_no }) => {
if let Some(lines) = &blobs.old.as_ref() {
@@ -1645,7 +1645,7 @@ impl<'a> ToText<'a> for Hunk<Modification> {
type Output = Vec<Line<'a>>;
type Context = Blobs<Vec<Line<'a>>>;
fn to_text<R: Repo>(&'a self, blobs: &Self::Context, repo: &R) -> Self::Output {
fn to_text(&'a self, blobs: &Self::Context) -> Self::Output {
let mut lines: Vec<Line<'a>> = vec![];
let default_dark = Color::Rgb(20, 20, 20);
@@ -1689,7 +1689,7 @@ impl<'a> ToText<'a> for Hunk<Modification> {
span::positive(" + ").bg(positive_dark).dim(),
]
.to_vec(),
line.to_text(blobs, repo)
line.to_text(blobs)
.spans
.into_iter()
.map(|span| span.bg(positive_dark))
@@ -1712,7 +1712,7 @@ impl<'a> ToText<'a> for Hunk<Modification> {
span::negative(" - ").bg(negative_dark).dim(),
]
.to_vec(),
line.to_text(blobs, repo)
line.to_text(blobs)
.spans
.into_iter()
.map(|span| span.bg(negative_dark))
@@ -1741,7 +1741,7 @@ impl<'a> ToText<'a> for Hunk<Modification> {
span::default(&format!("{:<3}", "")),
]
.to_vec(),
line.to_text(blobs, repo).spans,
line.to_text(blobs).spans,
]
.concat(),
));