hexhog

repository·main·Indexed 42 days ago

https://github.com/dvdtsb/hexhog

A configurable hex viewer and editor version 0.1.3 that allows users to edit, delete, insert, and copy/paste bytes within files. It features a TUI powered by ratatui and supports custom color themes and charsets via a config.toml file.

Tokens
2.1K
Snippets
11
Records
12
Agent score
55%

What's inside hexhog

  1. Configure hexhog appearance and charsets

    main

    Hexhog is configured via a config.toml file. The configuration allows you to customize the color theme (using color names, ANSI codes, or RGB arrays) and the characters used to represent different byte types in the ASCII view.

    Configuration File Locations

    • Linux: /home/user/.config/hexhog/config.toml
    • Windows: C:\Users\user\AppData\Roaming\hexhog\config.toml
    • macOS: /Users/user/Library/Application Support/hexhog/config.toml
    [theme]
    null = "dark_gray"
    ascii_printable = "blue"
    ascii_whitespace = [67, 205, 128] # rgb
    ascii_other = 162 # ansi
    non_ascii = "red"
    accent = "blue"
    primary = "green"
    background = "black"
    border = "cyan"
    
    [charset]
    null = "."
    ascii_whitespace = "·"
    ascii_other = "°"
    non_ascii = "×"
  2. Configure the hexhog theme and charset via TOML

    main

    Hexhog uses a TOML configuration file to customize the visual appearance (theme) and the character representation (charset) of bytes.

    Theme Configuration

    The [theme] section allows you to define colors for different byte types. Colors can be specified in three formats:

    1. Color Name: A string representing a standard color (e.g., "Blue", "Red").
    2. Indexed Color: An integer between 0 and 255 (e.g., 16).
    3. RGB Array: An array of three integers [r, g, b] where each value is between 0 and 255 (e.g., [255, 0, 0]).

    Available theme keys:

    • null
    • ascii_printable
    • ascii_whitespace
    • ascii_other
    • non_ascii
    • accent
    • primary
    • border
    • select
    • background

    Charset Configuration

    The [charset] section allows you to define which characters are used to represent specific byte types when they are not printable ASCII. Each value must be a single character string.

    Available charset keys:

    • null: Character for null bytes.
    • ascii_whitespace: Character for non-space whitespace.
    • ascii_other: Character for other non-printable ASCII.
    • non_ascii: Character for non-ASCII bytes.
    [theme]
    null = "DarkGray"
    primary = [255, 255, 255]
    accent = 12
    
    [charset]
    null = '.'
    ascii_whitespace = '·'
    ascii_other = '°'
    non_ascii = '×'
  3. Run the application loop with `App::run`

    main

    To start the application's main execution loop, call App::run. This method takes ownership of the App instance and a DefaultTerminal (from the ratatui crate). It enters a loop that handles rendering, event processing, and state updates until the application is no longer running.

    app.run(terminal)?;
  4. Use the Byte struct for data representation

    main

    The Byte struct wraps a u8 value and provides methods to categorize the byte, retrieve its hexadecimal representation, and resolve its character or visual style based on a provided Config object.

    Key methods:

    • new(value: u8) -> Byte: Creates a new Byte instance.
    • value() -> u8: Returns the underlying raw byte value.
    • get_bytetype() -> ByteType: Returns the classification of the byte.
    • get_hex() -> String: Returns the byte as a two-digit uppercase hexadecimal string (e.g., 0A, FF).
    • get_char(&Config) -> char: Resolves the byte to a character using the provided Config's charset.
    • get_style(&Config) -> Style: Resolves the byte to a ratatui::style::Style using the provided Config's colorscheme.
    use crate::byte::{Byte, ByteType};
    use crate::config::Config;
    
    let byte = Byte::new(0x41);
    assert_eq!(byte.value(), 65);
    assert_eq!(byte.get_hex(), "41");
    
    // Categorization
    match byte.get_bytetype() {
        ByteType::AsciiPrintable => println!("It's printable!"),
        _ => println!("Other type"),
    }
    
    // Integration with Config
    // Note: Requires a valid Config instance
    let character = byte.get_char(&config);
    let style = byte.get_style(&config);
  5. Initialize the application with `App::new`

    main

    To programmatically initialize the application state, use App::new. This function requires an Args struct (containing the target file path) and a Config object. It attempts to read the file contents into memory if the file exists.

    Returns a Result<App>.

    let app = App::new(args, config)?;
  6. Load configuration using read_config()

    main

    Use the read_config function to load a configuration from a TOML file path. If the file does not exist, the function returns the Config::default() values instead of an error.

    Returns: Result<Config, String>

    • Ok(Config): The loaded configuration or default configuration.
    • Err(String): An error message if the TOML is malformed or if color/charset values are invalid (e.g., a color index out of range or a charset field containing more than one character).
    let config = read_config("path/to/config.toml").unwrap_or_else(|e| panic!("Failed to load config: {}", e));
  7. Classify bytes using ByteType

    main

    The ByteType enum is used to categorize bytes into one of five types based on their ASCII properties and value:

    • Null: The byte value is 0.
    • AsciiPrintable: The byte is an ASCII graphic character (e.g., letters, numbers, punctuation).
    • AsciiWhitespace: The byte is an ASCII whitespace character (e.g., space, tab, newline).
    • AsciiOther: The byte is an ASCII character that is neither graphic nor whitespace.
    • NonAscii: The byte is not a valid ASCII character.
    pub enum ByteType {
        Null,
        AsciiPrintable,
        AsciiWhitespace,
        AsciiOther,
        NonAscii,
    }
  8. Default configuration values

    main

    If no configuration file is provided or if specific fields are missing, hexhog uses the following default settings:

    Default ColorScheme:

    • null: Color::DarkGray
    • ascii_printable: Color::Blue
    • ascii_whitespace: Color::Cyan
    • ascii_other: Color::Yellow
    • non_ascii: Color::Green
    • accent: Color::Blue
    • select: Color::DarkGray
    • border: Color::White
    • primary: Color::White
    • background: Color::Reset

    Default Charset:

    • null: '.'
    • ascii_whitespace: '·'
    • ascii_other: '°'
    • non_ascii: '×'
  9. Run the hexhog CLI

    main

    The hexhog application is a CLI tool that uses a terminal user interface (TUI) powered by ratatui. When executed, it automatically attempts to load a configuration file from the standard system configuration directory at [config_dir]/hexhog/config.toml. If the configuration file is missing or invalid, it falls back to the default configuration.

    To run the application, use the standard binary execution method. The application expects command-line arguments defined by the Args struct (provided via clap).

    # Example of running the binary (assuming it is compiled)
    ./hexhog