-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d09b62a
Showing
12 changed files
with
780 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
github: | ||
- parasyte | ||
patreon: blipjoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
name: CI | ||
on: | ||
push: | ||
pull_request: | ||
schedule: | ||
- cron: '0 0 * * 0' | ||
jobs: | ||
checks: | ||
name: Check | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
rust: | ||
- stable | ||
- beta | ||
- 1.73.0 | ||
steps: | ||
- name: Checkout sources | ||
uses: actions/checkout@v3 | ||
- name: Update apt repos | ||
run: sudo apt -y update | ||
- name: Install toolchain | ||
uses: dtolnay/rust-toolchain@master | ||
with: | ||
toolchain: ${{ matrix.rust }} | ||
- name: Rust cache | ||
uses: Swatinem/rust-cache@v2 | ||
with: | ||
shared-key: common | ||
- name: Cargo check | ||
run: cargo check --workspace | ||
|
||
lints: | ||
name: Lints | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout sources | ||
uses: actions/checkout@v3 | ||
- name: Update apt repos | ||
run: sudo apt -y update | ||
- name: Install toolchain | ||
uses: dtolnay/rust-toolchain@master | ||
with: | ||
toolchain: stable | ||
components: clippy, rustfmt | ||
- name: Rust cache | ||
uses: Swatinem/rust-cache@v2 | ||
with: | ||
shared-key: common | ||
- name: Install cargo-machete | ||
uses: baptiste0928/cargo-install@v2 | ||
with: | ||
crate: cargo-machete | ||
- name: Cargo fmt | ||
run: cargo fmt --all -- --check | ||
- name: Cargo doc | ||
run: cargo doc --workspace --no-deps | ||
- name: Cargo clippy | ||
run: cargo clippy --workspace --tests -- -D warnings | ||
- name: Cargo machete | ||
run: cargo machete | ||
|
||
tests: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
needs: [checks, lints] | ||
strategy: | ||
matrix: | ||
rust: | ||
- stable | ||
- beta | ||
- 1.73.0 | ||
steps: | ||
- name: Checkout sources | ||
uses: actions/checkout@v3 | ||
- name: Update apt repos | ||
run: sudo apt -y update | ||
- name: Install toolchain | ||
uses: dtolnay/rust-toolchain@master | ||
with: | ||
toolchain: ${{ matrix.rust }} | ||
- name: Rust cache | ||
uses: Swatinem/rust-cache@v2 | ||
with: | ||
shared-key: common | ||
- name: Cargo test | ||
run: cargo test --workspace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
.DS_Store |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[package] | ||
name = "hd" | ||
description = "Hex Display: A modern `xxd` alternative." | ||
version = "0.1.0" | ||
authors = ["Jay Oster <[email protected]>"] | ||
repository = "https://github.com/parasyte/hd" | ||
edition = "2021" | ||
rust-version = "1.73.0" | ||
keywords = ["bytes", "hex", "pretty", "viewer", "xxd"] | ||
categories = ["command-line-utilities", "development-tools", "value-formatting"] | ||
license = "MIT" | ||
include = [ | ||
"/Cargo.*", | ||
"/LICENSE", | ||
"/README.md", | ||
"/img/screenshot.png", | ||
"/src/**/*", | ||
] | ||
|
||
[dependencies] | ||
colorz = { version = "1.1.4", features = ["std"] } | ||
error-iter = "0.4.1" | ||
onlyargs = "0.2.0" | ||
onlyargs_derive = "0.2.0" | ||
onlyerror = "0.1.4" | ||
unicode-display-width = "0.3.0" | ||
unicode-segmentation = "1.12.0" | ||
|
||
[profile.release] | ||
codegen-units = 1 | ||
lto = "fat" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Copyright 2024 Jay Oster | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[![Crates.io](https://img.shields.io/crates/v/hd)](https://crates.io/crates/hd "Crates.io version") | ||
[![Documentation](https://img.shields.io/docsrs/hd)](https://docs.rs/hd "Documentation") | ||
[![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/) | ||
[![GitHub actions](https://img.shields.io/github/actions/workflow/status/parasyte/hd/ci.yml?branch=main)](https://github.com/parasyte/hd/actions "CI") | ||
[![GitHub activity](https://img.shields.io/github/last-commit/parasyte/hd)](https://github.com/parasyte/hd/commits "Commit activity") | ||
[![GitHub Sponsors](https://img.shields.io/github/sponsors/parasyte)](https://github.com/sponsors/parasyte "Sponsors") | ||
|
||
# `hd` | ||
|
||
Hex Display: A modern `xxd` alternative. | ||
|
||
![Screenshot](./img/screenshot.png) |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use unicode_segmentation::UnicodeSegmentation; | ||
|
||
/// A grapheme cluster. | ||
/// | ||
/// One single-wide or double-wide character, potentially composed of multiple Unicode codepoints. | ||
pub(crate) struct Span<'a> { | ||
pub(crate) bytes: &'a [u8], | ||
pub(crate) parsed: Option<&'a str>, | ||
} | ||
|
||
impl Span<'_> { | ||
/// Create an ASCII span. | ||
pub(crate) fn ascii(bytes: &[u8]) -> Span<'_> { | ||
Span { | ||
bytes, | ||
parsed: None, | ||
} | ||
} | ||
|
||
/// Parse the first available grapheme cluster from a byte slice if possible. | ||
pub(crate) fn parse(bytes: &[u8]) -> Option<Span<'_>> { | ||
let s = std::str::from_utf8(bytes).ok()?; | ||
let mut graphemes = UnicodeSegmentation::graphemes(s, true); | ||
|
||
graphemes.next().map(|parsed| Span { | ||
bytes: &bytes[..parsed.len()], | ||
parsed: Some(parsed), | ||
}) | ||
} | ||
|
||
/// Show a parsed grapheme cluster in the ASCII table. | ||
pub(crate) fn as_ascii(&self, index: usize, column: usize, width: usize) -> Ascii<'_> { | ||
// Correctly handle row wrapping with double-wide characters. | ||
let cluster = self.parsed.unwrap(); | ||
let wide = unicode_display_width::width(cluster) == 2; | ||
if (index == 0 && (!wide || column != width - 1)) || (index == 1 && wide && column == 0) { | ||
Ascii::Cluster(cluster) | ||
} else if wide && ((index == 1 && column != 0) || (index == 2 && column == 1)) { | ||
Ascii::Skip | ||
} else { | ||
Ascii::Space | ||
} | ||
} | ||
} | ||
|
||
/// How to show a span in the ASCII table. | ||
pub(crate) enum Ascii<'a> { | ||
/// Show the grapheme cluster. | ||
Cluster(&'a str), | ||
|
||
/// Show a blank space. | ||
Space, | ||
|
||
/// Skip this column. | ||
Skip, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_as_ascii() { | ||
let astronaut = "👩🏻🚀".as_bytes(); | ||
let span = Span::parse(astronaut).unwrap(); | ||
|
||
// Normal cases: grapheme cluster is shown on first line. | ||
for j in 0..7 { | ||
assert!(matches!(span.as_ascii(0, j, 8), Ascii::Cluster(_))); | ||
assert!(matches!(span.as_ascii(1, (j + 1) % 8, 8), Ascii::Skip)); | ||
for i in 2..astronaut.len() { | ||
assert!(matches!(span.as_ascii(i, (j + i) % 8, 8), Ascii::Space)); | ||
} | ||
} | ||
|
||
// Edge case: grapheme cluster is shown on second line. | ||
assert!(matches!(span.as_ascii(0, 7, 8), Ascii::Space)); | ||
assert!(matches!(span.as_ascii(1, 0, 8), Ascii::Cluster(_))); | ||
assert!(matches!(span.as_ascii(2, 1, 8), Ascii::Skip)); | ||
for i in 3..astronaut.len() { | ||
assert!(matches!(span.as_ascii(i, (i - 2) % 8, 8), Ascii::Space)); | ||
} | ||
} | ||
} |
Oops, something went wrong.