Skip to content

Commit

Permalink
Merge pull request #571 from flipsi/tui-minimal-view
Browse files Browse the repository at this point in the history
Add minimal display mode
  • Loading branch information
kamiyaa authored Nov 3, 2024
2 parents 2c23a69 + 3f449c3 commit d5a25d3
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/joshuto.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ zoxide_update = false
custom_commands = []

[display]
# default, hsplit
# default, minimal, hsplit
mode = "default"

automatically_count_files = false
Expand Down
2 changes: 2 additions & 0 deletions src/types/option/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::line_mode::LineNumberStyle;
#[derive(Clone, Copy, Debug)]
pub enum DisplayMode {
Default,
Minimal,
HSplit,
}

Expand Down Expand Up @@ -38,6 +39,7 @@ impl From<DisplayOptionRaw> for DisplayOption {
fn from(raw: DisplayOptionRaw) -> Self {
let mode = match raw.mode.as_str() {
"hsplit" => DisplayMode::HSplit,
"minimal" => DisplayMode::Minimal,
_ => DisplayMode::Default,
};

Expand Down
1 change: 1 addition & 0 deletions src/ui/views/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod tui_command_menu;
mod tui_folder_view;
mod tui_hsplit_view;
mod tui_minimal_view;
mod tui_textfield;
mod tui_view;
mod tui_worker_view;
Expand Down
108 changes: 108 additions & 0 deletions src/ui/views/tui_minimal_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use ratatui::buffer::Buffer;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Style};
use ratatui::text::Span;
use ratatui::widgets::{Paragraph, Widget, Wrap};

use crate::types::state::AppState;
use crate::ui::widgets::{TuiDirListDetailed, TuiFooter, TuiTopBar};

pub struct TuiMinimalView<'a> {
pub app_state: &'a AppState,
pub show_bottom_status: bool,
}

impl<'a> TuiMinimalView<'a> {
pub fn new(app_state: &'a AppState) -> Self {
Self {
app_state,
show_bottom_status: true,
}
}
}

impl<'a> Widget for TuiMinimalView<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
let tab_state = self.app_state.state.tab_state_ref();

let display_options = &self.app_state.config.display_options;
let constraints = &[Constraint::Ratio(1, 1)];

let layout_rect = {
let area = Rect {
y: area.top() + 1,
height: area.height - 2,
..area
};
calculate_layout(area, constraints)
};

let tab_id = tab_state.curr_tab_id();
if let Some(curr_tab) = tab_state.tab_ref(&tab_id) {
let curr_list = curr_tab.curr_list_ref();

let layout_rect = layout_rect[0];

// render current view
if let Some(list) = curr_list.as_ref() {
TuiDirListDetailed::new(
&self.app_state.config,
list,
display_options,
curr_tab.option_ref(),
true,
)
.render(layout_rect, buf);
let rect = Rect {
x: 0,
y: area.height - 1,
width: area.width,
height: 1,
};

if self.show_bottom_status {
/* draw the bottom status bar */
if let Some(msg) = self.app_state.state.worker_state_ref().get_msg() {
let message_style = Style::default().fg(Color::Yellow);
let text = Span::styled(msg, message_style);
Paragraph::new(text)
.wrap(Wrap { trim: true })
.render(rect, buf);
} else if let Some(msg) =
self.app_state.state.message_queue_ref().current_message()
{
let text = Span::styled(msg.content.as_str(), msg.style);
Paragraph::new(text)
.wrap(Wrap { trim: true })
.render(rect, buf);
} else {
TuiFooter::new(list, curr_tab.option_ref()).render(rect, buf);
}
}
}

let topbar_width = area.width;
let rect = Rect {
x: 0,
y: 0,
width: topbar_width,
height: 1,
};
TuiTopBar::new(self.app_state).render(rect, buf);
}
}
}

fn calculate_layout(area: Rect, constraints: &[Constraint; 1]) -> Vec<Rect> {
let mut layout_rect = Layout::default()
.direction(Direction::Horizontal)
.constraints(constraints.as_ref())
.split(area)
.to_vec();

layout_rect[0] = Rect {
width: layout_rect[0].width - 1,
..layout_rect[0]
};
layout_rect
}
5 changes: 4 additions & 1 deletion src/ui/views/tui_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ratatui::widgets::Widget;

use crate::types::option::display::DisplayMode;
use crate::types::state::AppState;
use crate::ui::views::{TuiFolderView, TuiHSplitView};
use crate::ui::views::{tui_minimal_view::TuiMinimalView, TuiFolderView, TuiHSplitView};

pub struct TuiView<'a> {
pub app_state: &'a AppState,
Expand All @@ -27,6 +27,9 @@ impl<'a> Widget for TuiView<'a> {
DisplayMode::Default => {
TuiFolderView::new(self.app_state).render(area, buf);
}
DisplayMode::Minimal => {
TuiMinimalView::new(self.app_state).render(area, buf);
}
DisplayMode::HSplit => {
TuiHSplitView::new(self.app_state).render(area, buf);
}
Expand Down

0 comments on commit d5a25d3

Please sign in to comment.