Skip to content

Commit

Permalink
feat: add ability to change max file size (#51)
Browse files Browse the repository at this point in the history
* feat: add ability to change max file size

* feat: move setting to config file and cli arg

max file size moved from environment variable.

* chore: rename file -> data

* chore: add data too large hint

---------

Co-authored-by: wenqian <[email protected]>
  • Loading branch information
sineptic and fioncat authored Nov 6, 2024
1 parent 69e8844 commit 4258546
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
14 changes: 7 additions & 7 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 config/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ disable = false

[data]
disable_highlight = false
max_data_size = 30

[keys]
move_up = ["k", "<up>"]
Expand Down
4 changes: 4 additions & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ pub struct CommandArgs {
#[clap(long)]
pub build_info: bool,

/// Limit data size to read. In MiB
#[clap(long)]
pub max_data_size: Option<usize>,

/// Print version.
#[clap(short, long)]
pub version: bool,
Expand Down
9 changes: 8 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub struct Footer {
pub struct Data {
#[serde(default = "Config::disable")]
pub disable_highlight: bool,
#[serde(default = "Config::default_max_data_size")]
pub max_data_size: usize,
}

impl Config {
Expand Down Expand Up @@ -182,10 +184,14 @@ impl Config {
Ok(())
}

fn disable() -> bool {
const fn disable() -> bool {
false
}

const fn default_max_data_size() -> usize {
30
}

fn empty_map() -> HashMap<String, String> {
HashMap::new()
}
Expand Down Expand Up @@ -258,6 +264,7 @@ impl Data {
fn default() -> Self {
Self {
disable_highlight: Config::disable(),
max_data_size: Config::default_max_data_size(),
}
}
}
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// #![warn(clippy::pedantic)]

mod clipboard;
mod cmd;
mod config;
Expand All @@ -20,9 +22,6 @@ use crate::parse::ContentType;
use crate::tree::Tree;
use crate::ui::{App, HeaderContext};

// Forbid large data size to ensure TUI performance
const MAX_DATA_SIZE: usize = 30 * 1024 * 1024;

fn run() -> Result<()> {
let args = match CommandArgs::parse()? {
Some(args) => args,
Expand Down Expand Up @@ -85,8 +84,9 @@ fn run() -> Result<()> {
}
};

if data.len() > MAX_DATA_SIZE {
bail!("the data size is too large, we limit the maximum size to 30 MiB to ensure TUI performance, you should try to reduce the read size");
let max_data_size = args.max_data_size.unwrap_or(cfg.data.max_data_size) * 1024 * 1024;
if data.len() > max_data_size {
bail!("the data size is too large, we limit the maximum size to {} to ensure TUI performance, you should try to reduce the read size. HINT: You can use command line arg `--max-data-size` or config option `data.max_data_size` to modify this limitation", humansize::format_size(max_data_size, humansize::BINARY));
}

// To make sure the data is utf8 encoded.
Expand Down

0 comments on commit 4258546

Please sign in to comment.