tui

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package tui provides an interactive terminal user interface for subnet calculations. It uses Bubble Tea for the TUI framework and allows users to interactively split and join subnets, visualize subnet hierarchies, and export results as JSON.

Index

Constants

View Source
const (
	// MaxSplitDepth is the maximum prefix length for subnet splitting (e.g., /30).
	// Splitting beyond this depth is not allowed as /31 and /32 networks have no usable hosts.
	MaxSplitDepth = 30
)

Variables

This section is empty.

Functions

func Run

func Run(cidr string, initialSplit int) error

Run starts the interactive TUI for the given CIDR. The initialSplit parameter specifies the initial split depth (0 means no initial split). When the user quits after pressing 'e' (export), the JSON representation is printed.

Types

type ExportNode

type ExportNode struct {
	CIDR          string        `json:"cidr"`               // Network in CIDR notation
	FirstIP       string        `json:"firstIP"`            // First usable host IP
	LastIP        string        `json:"lastIP"`             // Last usable host IP
	BroadcastAddr string        `json:"broadcastAddr"`      // Broadcast address
	SubnetMask    string        `json:"subnetMask"`         // Subnet mask
	Hosts         uint          `json:"hosts"`              // Number of usable hosts
	Children      []*ExportNode `json:"children,omitempty"` // Child subnets if split
}

ExportNode is a JSON-serializable representation of a subnet node. It mirrors SubnetNode but uses string representations suitable for JSON output.

type Model

type Model struct {
	// contains filtered or unexported fields
}

Model is the Bubble Tea model for the interactive subnet TUI. It maintains the subnet tree state, cursor position, viewport dimensions, and UI components like help and status messages.

func NewModel

func NewModel(cidr string, targetBits int) (Model, error)

NewModel creates a new TUI model from a CIDR string. Optional targetBits parameter specifies initial split depth (0 means no initial split).

func (Model) Init

func (m Model) Init() tea.Cmd

Init implements tea.Model. It returns nil as no initial command is needed.

func (Model) Update

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

Update implements tea.Model. It handles keyboard input, window resize events, and status message clearing.

func (Model) View

func (m Model) View() string

View implements tea.Model. It renders the TUI including the title, subnet table, status messages, and help text.

type SubnetNode

type SubnetNode struct {
	Network  subnet.Network // Network holds the subnet calculation details
	Parent   *SubnetNode    // Parent node, nil for root
	Children []*SubnetNode  // Child subnets when split
	IsSplit  bool           // Whether this node has been split
}

SubnetNode represents a node in the subnet tree hierarchy. Each node embeds network information and can be split into two child subnets or joined back with its sibling into the parent subnet.

func (*SubnetNode) BroadcastAddr

func (n *SubnetNode) BroadcastAddr() netip.Addr

BroadcastAddr returns the broadcast address (last IP in range for IPv6).

func (*SubnetNode) CIDR

func (n *SubnetNode) CIDR() netip.Prefix

CIDR returns the network prefix (e.g., 192.168.1.0/24).

func (*SubnetNode) DeepCopy added in v1.1.0

func (n *SubnetNode) DeepCopy(parent *SubnetNode) *SubnetNode

DeepCopy creates an independent deep copy of the subnet tree. The new tree has the same structure and Network values but separate memory, allowing modifications without affecting the original.

func (*SubnetNode) ExportJSON

func (n *SubnetNode) ExportJSON() (string, error)

ExportJSON returns the subnet tree as a formatted JSON string. The output includes the full tree structure with all split children.

func (*SubnetNode) FirstIP

func (n *SubnetNode) FirstIP() netip.Addr

FirstIP returns the first usable host IP address.

func (*SubnetNode) Hosts

func (n *SubnetNode) Hosts() uint

Hosts returns the number of usable host addresses (capped at max uint for large IPv6 networks).

func (*SubnetNode) Join

func (n *SubnetNode) Join() bool

Join merges all children back into this node, undoing any splits. Returns true if joined, false if the node was not split.

func (*SubnetNode) LastIP

func (n *SubnetNode) LastIP() netip.Addr

LastIP returns the last usable host IP address.

func (*SubnetNode) Split

func (n *SubnetNode) Split() bool

Split divides a subnet node into two child subnets by incrementing the prefix length. Returns true if the split was successful, false if already split or at max depth.

func (*SubnetNode) SplitToDepth

func (n *SubnetNode) SplitToDepth(targetBits int)

SplitToDepth recursively splits the subnet until all leaves reach the target prefix length. For example, SplitToDepth(26) on a /24 creates four /26 subnets.

func (*SubnetNode) SubnetMask

func (n *SubnetNode) SubnetMask() netip.Addr

SubnetMask returns the subnet mask as an address.

Jump to

Keyboard shortcuts

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