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

feat(ui): support header title #5

Merged
merged 1 commit into from
May 22, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ anyhow = "1.0.86"
clap = { version = "4.5.4", features = ["derive"] }
crossterm = "0.27.0"
dirs = "5.0.1"
humansize = "2.1.3"
paste = "1.0.15"
ratatui = "0.26.2"
serde = { version = "1.0.202", features = ["derive"] }
Expand Down
8 changes: 8 additions & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ pub struct CommandArgs {
#[clap(short, long)]
pub content_type: Option<ContentType>,

/// Force to no render the header.
#[clap(long)]
pub disable_header: bool,

/// Force to use the header format.
#[clap(short = 'f', long)]
pub header_format: Option<String>,

/// Force to use vertical layout.
#[clap(short = 'V', long)]
pub vertical: bool,
Expand Down
10 changes: 9 additions & 1 deletion src/config/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ macro_rules! generate_colors_parse {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Colors {
#[serde(default = "Colors::default_header")]
pub header: Color,

#[serde(default = "TreeColors::default")]
pub tree: TreeColors,

Expand All @@ -28,18 +31,23 @@ pub struct Colors {
pub focus_border: Color,
}

generate_colors_parse!(Colors, tree, item, data, focus_border);
generate_colors_parse!(Colors, header, tree, item, data, focus_border);

impl Colors {
pub fn default() -> Self {
Self {
header: Self::default_header(),
tree: TreeColors::default(),
item: ItemColors::default(),
data: DataColors::default(),
focus_border: Self::default_focus_boder(),
}
}

fn default_header() -> Color {
Color::new("", "", true, false)
}

fn default_focus_boder() -> Color {
Color::new("magenta", "", true, false)
}
Expand Down
30 changes: 30 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub struct Config {
#[serde(default = "Layout::default")]
pub layout: Layout,

#[serde(default = "Header::default")]
pub header: Header,

#[serde(default = "Colors::default")]
pub colors: Colors,

Expand Down Expand Up @@ -44,6 +47,15 @@ pub enum LayoutDirection {
Horizontal,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Header {
#[serde(default = "Header::default_disable")]
pub disable: bool,

#[serde(default = "Header::default_format")]
pub format: String,
}

impl Config {
pub const MIN_LAYOUT_TREE_SIZE: u16 = 10;
pub const MAX_LAYOUT_TREE_SIZE: u16 = 80;
Expand Down Expand Up @@ -105,6 +117,7 @@ impl Config {
pub fn default() -> Self {
Self {
layout: Layout::default(),
header: Header::default(),
colors: Colors::default(),
types: Types::default(),
keys: Keys::default(),
Expand Down Expand Up @@ -134,3 +147,20 @@ impl Layout {
40
}
}

impl Header {
fn default() -> Self {
Self {
disable: Self::default_disable(),
format: Self::default_format(),
}
}

fn default_disable() -> bool {
false
}

fn default_format() -> String {
"{version} - {data_source} ({content_type}) - {data_size}".to_string()
}
}
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::cmd::CommandArgs;
use crate::config::Config;
use crate::config::LayoutDirection;
use crate::tree::{ContentType, Tree};
use crate::ui::app::App;
use crate::ui::{App, HeaderContext};

// Forbid large data size to ensure TUI performance
const MAX_DATA_SIZE: usize = 10 * 1024 * 1024;
Expand Down Expand Up @@ -50,8 +50,7 @@ fn run() -> Result<()> {
let mut cfg = if args.ignore_config {
Config::default()
} else {
let config_path = args.config.clone();
Config::load(config_path)?
Config::load(args.config)?
};

if args.vertical && args.horizontal {
Expand All @@ -64,6 +63,14 @@ fn run() -> Result<()> {
cfg.layout.direction = LayoutDirection::Horizontal;
}

if args.disable_header {
cfg.header.disable = true;
}

if let Some(format) = args.header_format {
cfg.header.format = format;
}

if let Some(size) = args.size {
cfg.layout.tree_size = size;
}
Expand Down Expand Up @@ -126,6 +133,12 @@ fn run() -> Result<()> {
let tree = Tree::parse(&cfg, &data, content_type).context("parse file")?;

let mut app = App::new(&cfg, tree);

if !cfg.header.disable {
let header_ctx = HeaderContext::new(args.path, content_type, data.len());
app.set_header(header_ctx);
}

app.show().context("show tui")?;

Ok(())
Expand Down
56 changes: 53 additions & 3 deletions src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::config::keys::Action;
use crate::config::{Config, LayoutDirection};
use crate::tree::Tree;
use crate::ui::data_block::DataBlock;
use crate::ui::header::{Header, HeaderContext};
use crate::ui::tree_overview::TreeOverview;

enum Refresh {
Expand Down Expand Up @@ -48,9 +49,15 @@ pub struct App<'a> {

layout_direction: LayoutDirection,
layout_tree_size: u16,

header: Option<Header<'a>>,
header_area: Rect,
skip_header: bool,
}

impl<'a> App<'a> {
const HEADER_HEIGHT: u16 = 1;

pub fn new(cfg: &'a Config, tree: Tree<'a>) -> Self {
Self {
cfg,
Expand All @@ -62,6 +69,9 @@ impl<'a> App<'a> {
data_block_area: Rect::default(),
layout_direction: cfg.layout.direction,
layout_tree_size: cfg.layout.tree_size,
header: None,
header_area: Rect::default(),
skip_header: false,
}
}

Expand Down Expand Up @@ -114,6 +124,10 @@ impl<'a> App<'a> {
}
}

pub fn set_header(&mut self, ctx: HeaderContext) {
self.header = Some(Header::new(self.cfg, ctx));
}

fn draw(&mut self, frame: &mut Frame) {
self.refresh_area(frame);

Expand All @@ -125,6 +139,12 @@ impl<'a> App<'a> {
// TODO: When we cannot find data, should warn user (maybe message in data block?)
}

if let Some(header) = self.header.as_ref() {
if !self.skip_header {
header.draw(frame, self.header_area);
}
}

let tree_focus = matches!(self.focus, ElementInFocus::TreeOverview);
self.tree_overview
.draw(frame, self.tree_overview_area, tree_focus);
Expand All @@ -136,22 +156,52 @@ impl<'a> App<'a> {

fn refresh_area(&mut self, frame: &Frame) {
let tree_size = self.layout_tree_size;
let data_size = 100 - tree_size;
let data_size = 100_u16.saturating_sub(tree_size);

// These checks should be done in config validation.
debug_assert_ne!(tree_size, 0);
debug_assert_ne!(data_size, 0);

let frame_area = frame.size();
let main_area = match self.header {
Some(_) => {
let Rect { height, .. } = frame_area;
if height <= Self::HEADER_HEIGHT + 1 {
// God knows under what circumstances such a small terminal would appear!
// We will not render the header.
self.skip_header = true;
frame_area
} else {
self.skip_header = false;
self.header_area = Rect {
height: Self::HEADER_HEIGHT,
y: 0,
..frame_area
};
Rect {
height: height.saturating_sub(Self::HEADER_HEIGHT),
y: Self::HEADER_HEIGHT,
..frame_area
}
}
}
None => frame_area,
};

match self.layout_direction {
LayoutDirection::Vertical => {
let vertical = Layout::vertical([
Constraint::Percentage(tree_size),
Constraint::Percentage(data_size),
]);
[self.tree_overview_area, self.data_block_area] = vertical.areas(frame.size());
[self.tree_overview_area, self.data_block_area] = vertical.areas(main_area);
}
LayoutDirection::Horizontal => {
let horizontal = Layout::horizontal([
Constraint::Percentage(tree_size),
Constraint::Percentage(data_size),
]);
[self.tree_overview_area, self.data_block_area] = horizontal.areas(frame.size());
[self.tree_overview_area, self.data_block_area] = horizontal.areas(main_area);
}
}
}
Expand Down