Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 83 additions & 124 deletions src/app.rs

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod keybind;
mod view;
mod widget;

use std::path::Path;
use std::{path::Path, rc::Rc};

use app::App;
use clap::{Parser, ValueEnum};
Expand Down Expand Up @@ -134,8 +134,8 @@ pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

pub fn run() -> Result<()> {
let args = Args::parse();
let (core_config, ui_config, graph_config, color_theme, key_bind_patch) = config::load()?;
let key_bind = keybind::KeyBind::new(key_bind_patch);
let (core_config, ui_config, graph_config, color_theme, keybind_patch) = config::load()?;
let keybind = keybind::KeyBind::new(keybind_patch);

let max_count = args.max_count;
let image_protocol = args.protocol.or(core_config.option.protocol).into();
Expand Down Expand Up @@ -168,18 +168,21 @@ pub fn run() -> Result<()> {

let (tx, rx) = event::init();

let ctx = Rc::new(app::AppContext {
keybind,
core_config,
ui_config,
color_theme,
image_protocol,
});
let mut app = App::new(
&repository,
graph_image_manager,
&graph,
&key_bind,
&core_config,
&ui_config,
&color_theme,
&graph_color_set,
cell_width_type,
image_protocol,
initial_selection,
ctx,
tx,
);
let ret = app.run(&mut terminal, rx);
Expand Down
33 changes: 11 additions & 22 deletions src/view/detail.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::rc::Rc;

use ratatui::{
crossterm::event::KeyEvent,
layout::{Constraint, Layout, Rect},
Expand All @@ -6,11 +8,9 @@ use ratatui::{
};

use crate::{
color::ColorTheme,
config::UiConfig,
app::AppContext,
event::{AppEvent, Sender, UserEvent, UserEventWithCount},
git::{Commit, FileChange, Ref, Repository},
protocol::ImageProtocol,
widget::{
commit_detail::{CommitDetail, CommitDetailState},
commit_list::{CommitList, CommitListState},
Expand All @@ -26,9 +26,7 @@ pub struct DetailView<'a> {
changes: Vec<FileChange>,
refs: Vec<Ref>,

ui_config: &'a UiConfig,
color_theme: &'a ColorTheme,
image_protocol: ImageProtocol,
ctx: Rc<AppContext>,
tx: Sender,
clear: bool,
}
Expand All @@ -39,9 +37,7 @@ impl<'a> DetailView<'a> {
commit: Commit,
changes: Vec<FileChange>,
refs: Vec<Ref>,
ui_config: &'a UiConfig,
color_theme: &'a ColorTheme,
image_protocol: ImageProtocol,
ctx: Rc<AppContext>,
tx: Sender,
) -> DetailView<'a> {
DetailView {
Expand All @@ -50,9 +46,7 @@ impl<'a> DetailView<'a> {
commit,
changes,
refs,
ui_config,
color_theme,
image_protocol,
ctx,
tx,
clear: false,
}
Expand Down Expand Up @@ -129,30 +123,25 @@ impl<'a> DetailView<'a> {
}

pub fn render(&mut self, f: &mut Frame, area: Rect) {
let detail_height = (area.height - 1).min(self.ui_config.detail.height);
let detail_height = (area.height - 1).min(self.ctx.ui_config.detail.height);
let [list_area, detail_area] =
Layout::vertical([Constraint::Min(0), Constraint::Length(detail_height)]).areas(area);

let commit_list = CommitList::new(&self.ui_config.list, self.color_theme);
let commit_list = CommitList::new(self.ctx.clone());
f.render_stateful_widget(commit_list, list_area, self.as_mut_list_state());

if self.clear {
f.render_widget(Clear, detail_area);
return;
}

let commit_detail = CommitDetail::new(
&self.commit,
&self.changes,
&self.refs,
&self.ui_config.detail,
self.color_theme,
);
let commit_detail =
CommitDetail::new(&self.commit, &self.changes, &self.refs, self.ctx.clone());
f.render_stateful_widget(commit_detail, detail_area, &mut self.commit_detail_state);

// clear the image area if needed
for y in detail_area.top()..detail_area.bottom() {
self.image_protocol.clear_line(y);
self.ctx.image_protocol.clear_line(y);
}
}
}
Expand Down
22 changes: 9 additions & 13 deletions src/view/help.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::rc::Rc;

use ratatui::{
crossterm::event::KeyEvent,
layout::{Constraint, Layout, Rect},
Expand All @@ -8,11 +10,11 @@ use ratatui::{
};

use crate::{
app::AppContext,
color::ColorTheme,
config::CoreConfig,
event::{AppEvent, Sender, UserEvent, UserEventWithCount},
keybind::KeyBind,
protocol::ImageProtocol,
view::View,
};

Expand All @@ -27,21 +29,15 @@ pub struct HelpView<'a> {
offset: usize,
height: usize,

image_protocol: ImageProtocol,
ctx: Rc<AppContext>,
tx: Sender,
clear: bool,
}

impl HelpView<'_> {
pub fn new<'a>(
before: View<'a>,
color_theme: &'a ColorTheme,
image_protocol: ImageProtocol,
tx: Sender,
keybind: &'a KeyBind,
core_config: &'a CoreConfig,
) -> HelpView<'a> {
let (help_key_lines, help_value_lines) = build_lines(color_theme, keybind, core_config);
pub fn new<'a>(before: View<'a>, ctx: Rc<AppContext>, tx: Sender) -> HelpView<'a> {
let (help_key_lines, help_value_lines) =
build_lines(&ctx.color_theme, &ctx.keybind, &ctx.core_config);
let help_key_line_max_width = help_key_lines
.iter()
.map(|line| line.width())
Expand All @@ -54,7 +50,7 @@ impl HelpView<'_> {
help_key_line_max_width,
offset: 0,
height: 0,
image_protocol,
ctx,
tx,
clear: false,
}
Expand Down Expand Up @@ -159,7 +155,7 @@ impl HelpView<'_> {

// clear the image area if needed
for y in area.top()..area.bottom() {
self.image_protocol.clear_line(y);
self.ctx.image_protocol.clear_line(y);
}
}
}
Expand Down
16 changes: 7 additions & 9 deletions src/view/list.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::rc::Rc;

use ratatui::{crossterm::event::KeyEvent, layout::Rect, Frame};

use crate::{
color::ColorTheme,
config::UiConfig,
app::AppContext,
event::{AppEvent, Sender, UserEvent, UserEventWithCount},
widget::commit_list::{CommitList, CommitListState, SearchState},
};
Expand All @@ -11,22 +12,19 @@ use crate::{
pub struct ListView<'a> {
commit_list_state: Option<CommitListState<'a>>,

ui_config: &'a UiConfig,
color_theme: &'a ColorTheme,
ctx: Rc<AppContext>,
tx: Sender,
}

impl<'a> ListView<'a> {
pub fn new(
commit_list_state: CommitListState<'a>,
ui_config: &'a UiConfig,
color_theme: &'a ColorTheme,
ctx: Rc<AppContext>,
tx: Sender,
) -> ListView<'a> {
ListView {
commit_list_state: Some(commit_list_state),
ui_config,
color_theme,
ctx,
tx,
}
}
Expand Down Expand Up @@ -170,7 +168,7 @@ impl<'a> ListView<'a> {
}

pub fn render(&mut self, f: &mut Frame, area: Rect) {
let commit_list = CommitList::new(&self.ui_config.list, self.color_theme);
let commit_list = CommitList::new(self.ctx.clone());
f.render_stateful_widget(commit_list, area, self.as_mut_list_state());
}
}
Expand Down
21 changes: 10 additions & 11 deletions src/view/refs.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::rc::Rc;

use ratatui::{
crossterm::event::KeyEvent,
layout::{Constraint, Layout, Rect},
Frame,
};

use crate::{
color::ColorTheme,
config::UiConfig,
app::AppContext,
event::{AppEvent, Sender, UserEvent, UserEventWithCount},
git::Ref,
widget::{
Expand All @@ -22,25 +23,22 @@ pub struct RefsView<'a> {

refs: Vec<Ref>,

ui_config: &'a UiConfig,
color_theme: &'a ColorTheme,
ctx: Rc<AppContext>,
tx: Sender,
}

impl<'a> RefsView<'a> {
pub fn new(
commit_list_state: CommitListState<'a>,
refs: Vec<Ref>,
ui_config: &'a UiConfig,
color_theme: &'a ColorTheme,
ctx: Rc<AppContext>,
tx: Sender,
) -> RefsView<'a> {
RefsView {
commit_list_state: Some(commit_list_state),
ref_list_state: RefListState::new(),
refs,
ui_config,
color_theme,
ctx,
tx,
}
}
Expand Down Expand Up @@ -96,15 +94,16 @@ impl<'a> RefsView<'a> {

pub fn render(&mut self, f: &mut Frame, area: Rect) {
let graph_width = self.as_list_state().graph_area_cell_width() + 1; // graph area + marker
let refs_width = (area.width.saturating_sub(graph_width)).min(self.ui_config.refs.width);
let refs_width =
(area.width.saturating_sub(graph_width)).min(self.ctx.ui_config.refs.width);

let [list_area, refs_area] =
Layout::horizontal([Constraint::Min(0), Constraint::Length(refs_width)]).areas(area);

let commit_list = CommitList::new(&self.ui_config.list, self.color_theme);
let commit_list = CommitList::new(self.ctx.clone());
f.render_stateful_widget(commit_list, list_area, self.as_mut_list_state());

let ref_list = RefList::new(&self.refs, self.color_theme);
let ref_list = RefList::new(&self.refs, self.ctx.clone());
f.render_stateful_widget(ref_list, refs_area, &mut self.ref_list_state);
}
}
Expand Down
Loading