Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
parasyte committed Dec 13, 2024
0 parents commit d09b62a
Show file tree
Hide file tree
Showing 12 changed files with 780 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github:
- parasyte
patreon: blipjoy
87 changes: 87 additions & 0 deletions .github/workflows/ci.yml
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
.DS_Store
74 changes: 74 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Cargo.toml
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"
18 changes: 18 additions & 0 deletions LICENSE
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.
12 changes: 12 additions & 0 deletions README.md
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 added example.bin
Binary file not shown.
Binary file added img/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions src/grapheme.rs
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));
}
}
}
Loading

0 comments on commit d09b62a

Please sign in to comment.