diff options
author | sitkevij <1553398+sitkevij@users.noreply.github.com> | 2024-01-01 16:19:42 -0800 |
---|---|---|
committer | sitkevij <1553398+sitkevij@users.noreply.github.com> | 2024-01-01 16:19:42 -0800 |
commit | bfbf86a1290153dc1f9d91e65542044652abff3b (patch) | |
tree | 0f6856e81989883cf1abede6e7bf1fcdc84ef744 | |
parent | aeac4ed78c5246fb2b247912b1fc4d791deac1a4 (diff) | |
parent | ca0248049f0f81cf187430b381fd509850ac38db (diff) |
ci: resolve stale messagingv0.6.0
-rw-r--r-- | .github/workflows/release.yml | 20 | ||||
-rw-r--r-- | .github/workflows/stale.yml | 14 | ||||
-rw-r--r-- | Cargo.lock | 40 | ||||
-rw-r--r-- | Cargo.toml | 11 | ||||
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | src/lib.rs | 170 | ||||
-rw-r--r-- | src/main.rs | 9 | ||||
-rw-r--r-- | tests/files/alphanumeric-zh.txt | 2 | ||||
-rw-r--r-- | tests/files/emoji.txt | 1 |
9 files changed, 143 insertions, 129 deletions
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..9fa248c --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,20 @@ +name: release +on: + push: + tags: + - "v*" # Push events to matching v*, i.e. v1.0, v20.15.10 + workflow_dispatch: + +jobs: + build: + runs-on: rust-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - run: | # Since v2.0.0 the deb package version will have a "-1" suffix. You can disable this by adding --deb-revision="" flag or revision = "" in Cargo metadata. The default suffix is for compliance with Debian's packaging standard. + cargo install cargo-deb + cargo deb + - name: Release + uses: softprops/action-gh-release@v1 + with: + files: target/debian/*.deb # /hex/target/debian/hx_0.5.0-1_amd64.deb diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml index cc5db07..fd05889 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yml @@ -15,15 +15,19 @@ jobs: steps: - uses: actions/stale@v8 with: - stale-issue-message: "This issue has been marked stale due to no recent activity." - stale-pr-message: "This PR has been marked stale due to no recent activity." - close-issue-message: "This issue was closed due to no activity for an extended period." - close-pr-message: "This PR was closed due to no activity for an extended period." + stale-issue-message: "Issue marked stale due to no recent activity." + stale-issue-label: "stale" + exempt-issue-labels: "enhancement, help wanted, in progress, upstream" + stale-pr-message: "PR marked stale due to no recent activity." + stale-pr-label: "stale" + exempt-pr-labels: "enhancement, help wanted, in progress, upstream" + close-issue-message: "Issue closed due to lack of activity for an extended period." + close-pr-message: "PR closed due to lack of activity for an extended period." days-before-issue-stale: 30 days-before-pr-stale: 45 days-before-issue-close: 5 days-before-pr-close: 10 - uses: dessant/lock-threads@v5 with: - exclude-any-issue-labels: "help wanted, upstream, in progress" + exclude-any-issue-labels: "enhancement, help wanted, in progress, upstream" process-only: "issues" @@ -75,17 +75,6 @@ dependencies = [ ] [[package]] -name = "atty" -version = "0.2.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" -dependencies = [ - "libc", - "termion", - "winapi", -] - -[[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -195,11 +184,10 @@ checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" [[package]] name = "hx" -version = "0.5.0" +version = "0.6.0" dependencies = [ "ansi_term", "assert_cmd", - "atty", "clap", "no_color", "rusty-hook", @@ -295,21 +283,6 @@ dependencies = [ ] [[package]] -name = "redox_syscall" -version = "0.1.50" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52ee9a534dc1301776eff45b4fa92d2c39b1d8c3d3357e6eb593e0d795506fc2" - -[[package]] -name = "redox_termios" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" -dependencies = [ - "redox_syscall", -] - -[[package]] name = "regex-automata" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -365,17 +338,6 @@ dependencies = [ ] [[package]] -name = "termion" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" -dependencies = [ - "libc", - "redox_syscall", - "redox_termios", -] - -[[package]] name = "termtree" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -4,11 +4,11 @@ categories = ["command-line-utilities", "development-tools"] description = "Futuristic take on hexdump, made in Rust." repository = "https://github.com/sitkevij/hex" keywords = ["hexdump", "hexadecimal", "tools", "ascii", "hex"] -include = ["src/**/*", "Cargo.toml", "Cargo.lock", "README.md", "hx.1"] +include = ["src/**/*", "Cargo.toml", "Cargo.lock", "README.md", "hx.1", "tests/**/*"] license = "MIT" name = "hx" readme = "README.md" -version = "0.5.0" +version = "0.6.0" edition = "2021" # see https://doc.rust-lang.org/cargo/reference/manifest.html @@ -26,11 +26,10 @@ panic = 'unwind' # panic strategy (`-C panic=...`), can also be 'abort' opt-level = 3 [dependencies] -clap = "4.4.8" +clap = "4.4" ansi_term = "0.12" -atty = "0.2" no_color = "0.1" [dev-dependencies] -assert_cmd = "2.0.12" -rusty-hook = "^0.11.2" +assert_cmd = "2.0" +rusty-hook = "^0.11" @@ -109,8 +109,11 @@ pacman -S hex ### debian install +Browse https://github.com/sitkevij/hex/releases/latest to choose `VERSION` for use with this debian +installation example: + ```sh -curl -sLO https://github.com/sitkevij/hex/releases/download/v0.4.2/hx_0.4.2_amd64.deb && dpkg -i hx_0.4.2_amd64.deb +VERSION=0.6.0 && curl -sLO "https://github.com/sitkevij/hex/releases/download/v$VERSION/hx_$VERSION-1_amd64.deb" && dpkg -i "hx_$VERSION-1_amd64.deb" ``` ### guix install @@ -15,7 +15,6 @@ extern crate ansi_term; extern crate clap; -use atty::Stream; use clap::ArgMatches; use no_color::is_no_color; use std::env; @@ -23,6 +22,7 @@ use std::error::Error; use std::f64; use std::fs; use std::io::BufReader; +use std::io::IsTerminal; use std::io::{self, BufRead, Read, Write}; /// arg cols @@ -41,9 +41,11 @@ pub const ARG_ARR: &str = "array"; pub const ARG_FNC: &str = "func"; /// arg places pub const ARG_PLC: &str = "places"; +/// arg prefix +pub const ARG_PFX: &str = "prefix"; -const ARGS: [&str; 8] = [ - ARG_COL, ARG_LEN, ARG_FMT, ARG_INP, ARG_CLR, ARG_ARR, ARG_FNC, ARG_PLC, +const ARGS: [&str; 9] = [ + ARG_COL, ARG_LEN, ARG_FMT, ARG_INP, ARG_CLR, ARG_ARR, ARG_FNC, ARG_PLC, ARG_PFX, ]; const DBG: u8 = 0x0; @@ -78,6 +80,36 @@ pub enum Format { Unknown, } +impl Format { + /// Formats a given u8 according to the base Format + /// + /// # Arguments + /// + /// * `data` - The byte to be formatted + /// * `prefix` - whether or not to add a prefix + fn format(&self, data: u8, prefix: bool) -> String { + if prefix { + match &self { + Self::Octal => format!("{:#06o}", data), + Self::LowerHex => format!("{:#04x}", data), + Self::UpperHex => format!("{:#04X}", data), + Self::Binary => format!("{:#010b}", data), + _ => panic!("format is not implemented for this Format"), + } + .to_string() + } else { + match &self { + Self::Octal => format!("{:04o}", data), + Self::LowerHex => format!("{:02x}", data), + Self::UpperHex => format!("{:02X}", data), + Self::Binary => format!("{:08b}", data), + _ => panic!("format is not implemented for this Format"), + } + .to_string() + } + } +} + /// Line structure for hex output #[derive(Clone, Debug, Default)] pub struct Line { @@ -140,70 +172,27 @@ pub fn print_offset(w: &mut impl Write, b: u64) -> io::Result<()> { write!(w, "{}: ", offset(b)) } -/// hex octal, takes u8 -pub fn hex_octal(b: u8) -> String { - format!("{:#06o}", b) -} - -/// hex lower hex, takes u8 -pub fn hex_lower_hex(b: u8) -> String { - format!("{:#04x}", b) -} - -/// hex upper hex, takes u8 -pub fn hex_upper_hex(b: u8) -> String { - format!("{:#04X}", b) -} - -/// hex binary, takes u8 -pub fn hex_binary(b: u8) -> String { - format!("{:#010b}", b) -} - /// print byte to std out -pub fn print_byte(w: &mut impl Write, b: u8, format: Format, colorize: bool) -> io::Result<()> { +pub fn print_byte( + w: &mut impl Write, + b: u8, + format: Format, + colorize: bool, + prefix: bool, +) -> io::Result<()> { + let fmt_string = format.format(b, prefix); if colorize { // note, for color testing: for (( i = 0; i < 256; i++ )); do echo "$(tput setaf $i)This is ($i) $(tput sgr0)"; done let color = byte_to_color(b); - match format { - Format::Octal => write!( - w, - "{} ", - ansi_term::Style::new() - .fg(ansi_term::Color::Fixed(color)) - .paint(hex_octal(b)) - ), - Format::LowerHex => write!( - w, - "{} ", - ansi_term::Style::new() - .fg(ansi_term::Color::Fixed(color)) - .paint(hex_lower_hex(b)) - ), - Format::UpperHex => write!( - w, - "{} ", - ansi_term::Style::new() - .fg(ansi_term::Color::Fixed(color)) - .paint(hex_upper_hex(b)) - ), - Format::Binary => write!( - w, - "{} ", - ansi_term::Style::new() - .fg(ansi_term::Color::Fixed(color)) - .paint(hex_binary(b)) - ), - _ => write!(w, "unk_fmt "), - } + write!( + w, + "{} ", + ansi_term::Style::new() + .fg(ansi_term::Color::Fixed(color)) + .paint(fmt_string) + ) } else { - match format { - Format::Octal => write!(w, "{} ", hex_octal(b)), - Format::LowerHex => write!(w, "{} ", hex_lower_hex(b)), - Format::UpperHex => write!(w, "{} ", hex_upper_hex(b)), - Format::Binary => write!(w, "{} ", hex_binary(b)), - _ => write!(w, "unk_fmt "), - } + write!(w, "{} ", fmt_string) } } @@ -275,6 +264,7 @@ pub fn run(matches: ArgMatches) -> Result<(), Box<dyn Error>> { }; let mut format_out = Format::LowerHex; let mut colorize = true; + let mut prefix = true; if let Some(columns) = matches.get_one::<String>(ARG_COL) { column_width = match columns.parse::<u64>() { @@ -319,7 +309,7 @@ pub fn run(matches: ArgMatches) -> Result<(), Box<dyn Error>> { // prevent term color codes being sent to stdout // test: cat Cargo.toml | target/debug/hx | more // override via ARG_CLR below - if !atty::is(Stream::Stdout) { + if !io::stdout().is_terminal() { colorize = false; } @@ -327,6 +317,10 @@ pub fn run(matches: ArgMatches) -> Result<(), Box<dyn Error>> { colorize = color.parse::<u8>().unwrap() == 1; } + if let Some(prefix_flag) = matches.get_one::<String>(ARG_PFX) { + prefix = prefix_flag.parse::<u8>().unwrap() == 1; + } + // array output mode is mutually exclusive if let Some(array) = matches.get_one::<String>(ARG_ARR) { output_array(array, buf, truncate_len, column_width)?; @@ -351,7 +345,7 @@ pub fn run(matches: ArgMatches) -> Result<(), Box<dyn Error>> { for hex in line.hex_body.iter() { offset_counter += 1; byte_column += 1; - print_byte(&mut locked, *hex, format_out, colorize)?; + print_byte(&mut locked, *hex, format_out, colorize, prefix)?; append_ascii(&mut ascii_line.ascii, *hex, colorize); } @@ -439,14 +433,14 @@ pub fn output_array( i += 1; if i == page.bytes && array_format != "g" { if array_format != "f" { - write!(locked, "{}", hex_lower_hex(*hex))?; + write!(locked, "{}", Format::LowerHex.format(*hex, true))?; } else { - write!(locked, "{}uy", hex_lower_hex(*hex))?; + write!(locked, "{}uy", Format::LowerHex.format(*hex, true))?; } } else if array_format != "f" { - write!(locked, "{}, ", hex_lower_hex(*hex))?; + write!(locked, "{}, ", Format::LowerHex.format(*hex, true))?; } else { - write!(locked, "{}uy; ", hex_lower_hex(*hex))?; + write!(locked, "{}uy; ", Format::LowerHex.format(*hex, true))?; } } writeln!(locked)?; @@ -540,32 +534,56 @@ mod tests { #[test] pub fn test_hex_octal() { let b: u8 = 0x6; - assert_eq!(hex_octal(b), "0o0006"); - assert_eq!(hex_octal(b), format!("{:#06o}", b)); + + //with prefix + assert_eq!(Format::Octal.format(b, true), "0o0006"); + assert_eq!(Format::Octal.format(b, true), format!("{:#06o}", b)); + + //without prefix + assert_eq!(Format::Octal.format(b, false), "0006"); + assert_eq!(Format::Octal.format(b, false), format!("{:04o}", b)); } /// hex lower hex, takes u8 #[test] fn test_hex_lower_hex() { let b: u8 = <u8>::max_value(); // 255 - assert_eq!(hex_lower_hex(b), "0xff"); - assert_eq!(hex_lower_hex(b), format!("{:#04x}", b)); + + //with prefix + assert_eq!(Format::LowerHex.format(b, true), "0xff"); + assert_eq!(Format::LowerHex.format(b, true), format!("{:#04x}", b)); + + //without prefix + assert_eq!(Format::LowerHex.format(b, false), "ff"); + assert_eq!(Format::LowerHex.format(b, false), format!("{:02x}", b)); } /// hex upper hex, takes u8 #[test] fn test_hex_upper_hex() { let b: u8 = <u8>::max_value(); - assert_eq!(hex_upper_hex(b), "0xFF"); - assert_eq!(hex_upper_hex(b), format!("{:#04X}", b)); + + //with prefix + assert_eq!(Format::UpperHex.format(b, true), "0xFF"); + assert_eq!(Format::UpperHex.format(b, true), format!("{:#04X}", b)); + + // without prefix + assert_eq!(Format::UpperHex.format(b, false), "FF"); + assert_eq!(Format::UpperHex.format(b, false), format!("{:02X}", b)); } /// hex binary, takes u8 #[test] fn test_hex_binary() { let b: u8 = <u8>::max_value(); - assert_eq!(hex_binary(b), "0b11111111"); - assert_eq!(hex_binary(b), format!("{:#010b}", b)); + + // with prefix + assert_eq!(Format::Binary.format(b, true), "0b11111111"); + assert_eq!(Format::Binary.format(b, true), format!("{:#010b}", b)); + + // without prefix + assert_eq!(Format::Binary.format(b, false), "11111111"); + assert_eq!(Format::Binary.format(b, false), format!("{:08b}", b)); } #[test] diff --git a/src/main.rs b/src/main.rs index 769fe98..2b57903 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,6 +84,15 @@ fn main() { .value_name("func_places") .help("Set function wave output decimal places") .num_args(1) + ) + .arg( + Arg::new(hx::ARG_PFX) + .action(clap::ArgAction::Set) + .short('r') + .long(hx::ARG_PFX) + .help("Include prefix in output (e.g. 0x/0b/0o). 0 to disable, 1 to enable") + .value_parser(["0", "1"]) + .num_args(1) ); let matches = app.get_matches(); diff --git a/tests/files/alphanumeric-zh.txt b/tests/files/alphanumeric-zh.txt deleted file mode 100644 index 4fb5bea..0000000 --- a/tests/files/alphanumeric-zh.txt +++ /dev/null @@ -1,2 +0,0 @@ -诶 比 西 迪 伊 艾弗 吉 艾尺 艾 杰 开 艾勒 艾马 艾娜 -一 二 三 四 五 六 七 九 十 diff --git a/tests/files/emoji.txt b/tests/files/emoji.txt new file mode 100644 index 0000000..15478f1 --- /dev/null +++ b/tests/files/emoji.txt @@ -0,0 +1 @@ +😀😃😄😁😆😅🤣😂🙂🙃 |