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 ¶
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) ClearSelection()
- func (m Model) Cursor() int
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) SelectedBytes() []byte
- func (m Model) Selection() (from, to int, ok bool)
- func (m *Model) SetCursor(i int)
- func (m *Model) SetData(data []byte)
- func (m Model) SetTheme(t theme.Theme) Model
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) WithTokens(tok theme.Tokens) Model
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 (Model) Bindings ¶
Bindings returns the actions the view honours, with descriptions, for help text.
func (Model) Cursor ¶
Cursor returns the offset of the byte under the cursor (0 when there is no data).
func (Model) LayoutNode ¶
LayoutNode adapts the view to a layout.Node of its own size. The Model is not changed.
func (Model) Linearize ¶
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 ¶
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 ¶
Selection returns the selected range as offsets from (first byte) and to (last byte, included), and whether a selection is going.
func (*Model) SetCursor ¶
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 ¶
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 ¶
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 ¶
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 ¶
Update moves the cursor and selection on keys, and on the mouse when Mouse is on.