Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimal display mode #571

Merged
merged 1 commit into from
Nov 3, 2024
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
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};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why I had to qualify this to make it compile, any help/explanation is appreciated.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you add

pub use self::tui_minimal_view::*;

then it won't require you to do this. Up to you on if you add it though, I don't have a huge preference and will probably change it later anyways xd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint!


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
Loading