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

Adds --columns for Print using custom width #13 #164

Merged
merged 16 commits into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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 .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: CICD

env:
MIN_SUPPORTED_RUST_VERSION: "1.56.0"
MIN_SUPPORTED_RUST_VERSION: "1.56.1"
CICD_INTERMEDIATES_DIR: "_cicd-intermediates"

on:
Expand Down
127 changes: 120 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ atty = "0.2"
const_format = "0.2"
libc = "0.2"
thiserror = "1.0"
terminal_size = "0.2"

[dependencies.clap]
version = "3"
Expand Down
2 changes: 2 additions & 0 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fn main() {
let show_position_panel = true;
let use_squeezing = false;
let border_style = BorderStyle::Unicode;
let columns = 2;

let mut printer = Printer::new(
&mut handle,
Expand All @@ -24,6 +25,7 @@ fn main() {
show_position_panel,
border_style,
use_squeezing,
columns,
);
printer.print_all(&input[..]).unwrap();
}
85 changes: 84 additions & 1 deletion src/bin/hexyl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate clap;
use std::convert::TryFrom;
use std::fs::File;
use std::io::{self, prelude::*, SeekFrom};
use std::num::NonZeroI64;
use std::num::{NonZeroI64, NonZeroU16, NonZeroU8};

use clap::{crate_name, crate_version, AppSettings, Arg, ColorChoice, Command};

Expand All @@ -16,6 +16,8 @@ use const_format::formatcp;

use thiserror::Error as ThisError;

use terminal_size::terminal_size;

use hexyl::{BorderStyle, Input, Printer};

const DEFAULT_BLOCK_SIZE: i64 = 512;
Expand Down Expand Up @@ -149,6 +151,42 @@ fn run() -> Result<(), AnyhowError> {
A negative value is valid and calculates an offset relative to the \
end of the file.",
),
)
.arg(
Arg::new("columns")
.short('w')
.long("columns")
.takes_value(true)
.value_name("N")
.help(
"Sets the number of hex data columns to be displayed. \
Cannot be used with other width-setting options.",
sharifhsn marked this conversation as resolved.
Show resolved Hide resolved
),
)
.arg(
Arg::new("terminal_width")
.short('t')
sharifhsn marked this conversation as resolved.
Show resolved Hide resolved
.long("terminal-width")
.takes_value(true)
.value_name("N")
.conflicts_with("columns")
.help(
"Sets the number of terminal columns to be displayed.\nSince the terminal \
width may not be an evenly divisible by the width per hex data column, this \
will use the greatest number of hex data columns that can fit in the requested \
width but still leave some space to the right.\nCannot be used with other \
width-setting options.",
),
)
.arg(
Arg::new("auto_width")
.short('a')
.long("auto-width")
.conflicts_with_all(&["columns", "terminal_width"])
.help(
"Sets the number of hex data columns to be adjusted according to the \
detected terminal width.\nCannot be used with other width-setting options.",
),
);

let matches = command.get_matches();
Expand Down Expand Up @@ -267,6 +305,50 @@ fn run() -> Result<(), AnyhowError> {
.transpose()?
.unwrap_or(0);

let columns = if let Some(columns) = matches
.value_of("columns")
.map(|s| {
s.parse::<NonZeroU8>().map(u8::from).context(anyhow!(
"failed to parse `--columns` arg {:?} as unsigned nonzero integer",
s
))
})
.transpose()?
{
columns
} else {
let terminal_width = if let Some(terminal_width) = matches
.value_of("terminal_width")
.map(|s| {
s.parse::<NonZeroU16>().map(u16::from).context(anyhow!(
"failed to parse `--columns` arg {:?} as unsigned nonzero integer",
s
))
})
.transpose()?
{
Some(terminal_width)
} else if matches.is_present("auto_width") {
Some(terminal_size().expect("not a tty").0 .0)
sharifhsn marked this conversation as resolved.
Show resolved Hide resolved
} else {
None
};

if let Some(terminal_width) = terminal_width {
let offset = if show_position_panel { 10 } else { 1 };
let col_width = if show_char_panel { 35 } else { 26 };
if (terminal_width - offset) / col_width < 1 {
1
} else {
((terminal_width - offset) / col_width)
.try_into()
.expect("there is a maximum of 255 columns")
}
} else {
2
}
};

let stdout = io::stdout();
let mut stdout_lock = stdout.lock();

Expand All @@ -277,6 +359,7 @@ fn run() -> Result<(), AnyhowError> {
show_position_panel,
border_style,
squeeze,
columns,
);
printer.display_offset(skip_offset + display_offset);
printer.print_all(&mut reader).map_err(|e| anyhow!(e))?;
Expand Down
Loading