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: add ability to change max file size #51

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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_file_size = 30
fioncat marked this conversation as resolved.
Show resolved Hide resolved

[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 file size to open. In MiB
#[clap(long)]
pub max_file_size: Option<usize>,
fioncat marked this conversation as resolved.
Show resolved Hide resolved

/// 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_file_size")]
pub max_file_size: usize,
fioncat marked this conversation as resolved.
Show resolved Hide resolved
}

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_file_size() -> usize {
fioncat marked this conversation as resolved.
Show resolved Hide resolved
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_file_size: Config::default_max_file_size(),
fioncat marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
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_file_size = args.max_file_size.unwrap_or(cfg.data.max_file_size) * 1024 * 1024;
fioncat marked this conversation as resolved.
Show resolved Hide resolved
if data.len() > max_file_size {
fioncat marked this conversation as resolved.
Show resolved Hide resolved
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", humansize::format_size(max_file_size, humansize::BINARY));
fioncat marked this conversation as resolved.
Show resolved Hide resolved
}

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