hexview

package
v1.0.0-rc.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package hexview shows bytes as a hex dump: an offset, the bytes in hex, in groups, and an ASCII gutter, with a cursor you move over the bytes, a selection you can copy as hex, and the mouse (click a byte, drag to select, wheel to scroll) when it is on.

It holds the bytes it is given and does not copy them again per frame, so a large buffer costs one row of work per visible row.

Stability: experimental. Its API may change in any minor release.

Example

A hex view shows an offset, the bytes in hex and an ASCII gutter.

package main

import (
	"fmt"

	"github.com/ows4444/tui/ansi"
	"github.com/ows4444/tui/hexview"
)

func main() {
	m := hexview.New([]byte("hello, hex view!\nabc"))
	fmt.Println(ansi.StripANSI(m.View()))
}
Output:
0000  68 65 6c 6c 6f 2c 20 68  65 78 20 76 69 65 77 21  |hello, hex view!|
0010  0a 61 62 63                                       |.abc|

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyMap

type KeyMap struct {
	Left, Right      keymap.Binding // one byte back, one byte on
	Up, Down         keymap.Binding // one row up, one row down
	PageUp, PageDown keymap.Binding // a screenful (Height rows, or 16)
	LineStart        keymap.Binding // the first byte of the row
	LineEnd          keymap.Binding // the last byte of the row
	Top, Bottom      keymap.Binding // the first and the last byte
	// Select starts a selection at the cursor, or ends it when one is going;
	// while it is on, moving the cursor extends it.
	Select keymap.Binding
	// Copy returns the selected bytes as hex text in a tui.WriteClipboard Cmd.
	Copy keymap.Binding
}

KeyMap names the keys of each action of a Model.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns arrows, PgUp/PgDn, Home/End, ctrl+Home/End, "v" to select and "y" to copy.

type Model

type Model struct {
	// Data is the bytes shown. Use SetData to replace it, so the cursor and
	// scroll position are kept inside it; assigning Data directly is fine for a
	// first value.
	Data []byte
	// BytesPerRow is how many bytes a row shows; 0 means 16.
	BytesPerRow int
	// GroupSize is how many bytes are drawn together before an extra space; 0
	// means 8 (the classic hexdump look) and a value of BytesPerRow or more
	// draws no groups.
	GroupSize int
	// Height is the number of rows View shows; 0 shows every row. With a Height
	// the view scrolls, by the least distance, to keep the cursor in sight.
	Height int
	// HideOffset and HideASCII leave out the offset column and the ASCII
	// gutter.
	HideOffset, HideASCII bool
	Theme                 theme.Theme

	// KeyMap holds the keys for each action. New fills it with DefaultKeyMap; a
	// Model built as a struct literal with a zero KeyMap behaves as if it held
	// DefaultKeyMap.
	KeyMap KeyMap

	// Mouse, when true, makes Update handle tui.MouseEvent: a left press on a
	// byte (in the hex or the ASCII column) puts the cursor on it and starts a
	// selection, a drag extends it, and the wheel scrolls. Off (the default)
	// ignores the mouse.
	Mouse bool
	// Bounds is the screen rectangle where the app draws the view (its first
	// row is the first row of the dump). The app sets it each frame it moves.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

Model is a hex dump of Data with a cursor. Build one with New.

func New

func New(data []byte) Model

New returns a hex view of data with the cursor on its first byte.

func (Model) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the actions the view honours, with descriptions, for help text.

func (*Model) ClearSelection

func (m *Model) ClearSelection()

ClearSelection ends the selection.

func (Model) Cursor

func (m Model) Cursor() int

Cursor returns the offset of the byte under the cursor (0 when there is no data).

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the view to a layout.Node of its own size. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the view as plain text for accessible output (see tui.Linearizer): how many bytes there are, where the cursor is and its value, and the selection if there is one. No hex dump, colour or cursor marks.

func (Model) SelectedBytes

func (m Model) SelectedBytes() []byte

SelectedBytes returns the selected bytes, or just the byte under the cursor when there is no selection, or nil when there is no data. The slice is a view of Data: do not modify it.

func (Model) Selection

func (m Model) Selection() (from, to int, ok bool)

Selection returns the selected range as offsets from (first byte) and to (last byte, included), and whether a selection is going.

func (*Model) SetCursor

func (m *Model) SetCursor(i int)

SetCursor moves the cursor to offset i, held inside the data, and keeps it in sight. A selection in progress is extended to it.

func (*Model) SetData

func (m *Model) SetData(data []byte)

SetData replaces the bytes, keeping the cursor on the same offset when it is still inside them (else on the last byte) and ending any selection.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).

func (Model) Tokens

func (m Model) Tokens() theme.Tokens

Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentHexView, ...) and then by WithTokens.

func (Model) Update

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

Update moves the cursor and selection on keys, and on the mouse when Mouse is on.

func (Model) View

func (m Model) View() string

View renders the dump: Height rows, or all of them when Height is 0.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL