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 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
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

## Features

- Added variable panels through the `--panels` and `--terminal-width` flags, see [#13](https://github.com/sharkdp/hexyl/issues/13) and [#164](https://github.com/sharkdp/hexyl/pull/164) (@sharifhsn)

## Bugfixes

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();
}
68 changes: 67 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};

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,31 @@ fn run() -> Result<(), AnyhowError> {
A negative value is valid and calculates an offset relative to the \
end of the file.",
),
)
.arg(
Arg::new("panels")
.long("panels")
.takes_value(true)
.value_name("N")
.help(
"Sets the number of hex data panels to be displayed. \
`--panels=auto` will display the maximum number of hex data panels \
based on the current terminal width",
),
)
.arg(
Arg::new("terminal_width")
.long("terminal-width")
.takes_value(true)
.value_name("N")
.conflicts_with("panels")
.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 panels that can fit in the requested \
width but still leave some space to the right.\nCannot be used with other \
width-setting options.",
),
);

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

let max_panels_fn = |terminal_width: u16| {
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
}
};

let panels = if matches.value_of("panels") == Some("auto") {
max_panels_fn(terminal_size().ok_or_else(|| anyhow!("not a TTY"))?.0 .0)
} else if let Some(panels) = matches
.value_of("panels")
.map(|s| {
s.parse::<NonZeroU16>().map(u16::from).context(anyhow!(
"failed to parse `--panels` arg {:?} as unsigned nonzero integer",
s
))
})
.transpose()?
{
panels
} else if let Some(terminal_width) = matches
.value_of("terminal_width")
.map(|s| {
s.parse::<NonZeroU16>().map(u16::from).context(anyhow!(
"failed to parse `--terminal-width` arg {:?} as unsigned nonzero integer",
s
))
})
.transpose()?
{
max_panels_fn(terminal_width)
} else {
2
};

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

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