printer

package
v1.7.6 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: GPL-3.0 Imports: 25 Imported by: 1

Documentation

Index

Constants

View Source
const (
	HostModeBase  = 0 // 仅 IP/PTR
	HostModeASN   = 1 // ASN + IP/PTR
	HostModeCity  = 2 // ASN + IP/PTR + 城市
	HostModeOwner = 3 // ASN + IP/PTR + owner
	HostModeFull  = 4 // ASN + IP/PTR + full
)
View Source
const (
	HostNamePTRorIP = 0 // 默认:有 PTR 显示 PTR,否则 IP
	HostNameIPOnly  = 1 // 始终显示 IP
)
View Source
const (
	RED_PREFIX    = "\033[1;31m"
	GREEN_PREFIX  = "\033[1;32m"
	YELLOW_PREFIX = "\033[1;33m"
	BLUE_PREFIX   = "\033[1;34m"
	CYAN_PREFIX   = "\033[1;36m"
	RESET_PREFIX  = "\033[0m"
)
View Source
const MTRHistoryWindow = 3 * time.Minute

Variables

This section is empty.

Functions

func ClassicPrinter

func ClassicPrinter(res *trace.Result, ttl int)
func CopyRight()

func DefaultMTRSnapshotFilename added in v1.7.5

func DefaultMTRSnapshotFilename(s MTRSnapshot, format string) string

DefaultMTRSnapshotFilename returns a portable filename in the current directory.

func EasyPrinter

func EasyPrinter(res *trace.Result, ttl int)

func FormatIPGeoData added in v1.6.1

func FormatIPGeoData(ip string, data *ipgeo.IPGeoData) string

func FormatMTRRawLine added in v1.5.2

func FormatMTRRawLine(rec trace.MTRRawRecord) string

FormatMTRRawLine formats one MTR raw stream record with fixed 12 columns: ttl|ip|ptr|rtt|asn|country|prov|city|district|owner|lat|lng

func FormatMTRReplayTime added in v1.7.5

func FormatMTRReplayTime(d time.Duration) string

func FormatTraceStopReason added in v1.7.3

func FormatTraceStopReason(reason *trace.StopReason) string

FormatTraceStopReason returns the canonical plain-text stop line without a trailing newline.

func GetRouter

func GetRouter(r *map[string][]string, node string)

func HopPrinter

func HopPrinter(h trace.Hop, info HopInfo)

func MTRColumnCodes added in v1.7.5

func MTRColumnCodes(columns []MTRColumn) string

func MTRColumnNames added in v1.7.5

func MTRColumnNames(columns []MTRColumn) string

MTRColumnNames serializes the current display selection using CLI column names.

func MTRHelpMaxOffset added in v1.7.5

func MTRHelpMaxOffset(replay bool, width, height int) int

func MTRReportPrint added in v1.5.2

func MTRReportPrint(stats []trace.MTRHopStat, opts MTRReportOptions)

MTRReportPrint 以 mtr -rzw 风格将最终统计一次性输出到 stdout。

输出格式(示例):

Start: 2025-07-14T09:12:00+0800
HOST: myhost                       Loss%   Snt   Last    Avg   Best   Wrst  StDev
  1. AS4134 one.one.one.one         0.0%    10    1.23   1.45   0.98   2.10   0.32
  2. ???                           100.0%    10    0.00   0.00   0.00   0.00   0.00

Wide 模式下使用 HostModeFull(完整地址 + 运营商),host 列宽度取所有行最大值; 非 wide 模式仅显示 PTR/IP,不查询/展示 GEO,也不显示 MPLS,按终端宽度截断:

width < 100  → maxHost = 16
100 ≤ width < 140 → maxHost = 20
width ≥ 140  → maxHost = 24

func MTRTUIPrinter added in v1.5.2

func MTRTUIPrinter(target, domain, targetIP, version string, startTime time.Time,
	srcHost, srcIP, lang string, apiInfo func() string, showIPs bool,
	isPaused func() bool, displayMode func() int, nameMode func() int, isMPLSDisabled func() bool,
	isHistoryMode func() bool, historyChartMode func() int, historySnapshot func(time.Time) []MTRHistoryTTL, columnState ...func() ([]MTRColumn, MTRColumnEditor)) func(iteration int, stats []trace.MTRHopStat)

MTRTUIPrinter 返回一个可直接用作 MTROnSnapshot 的回调函数, 将帧渲染到 os.Stdout。

func MTRTUIPrinterWithDialogs added in v1.7.5

func MTRTUIPrinterWithDialogs(target, domain, targetIP, version string, startTime time.Time,
	srcHost, srcIP, lang string, apiInfo func() string, showIPs bool,
	isPaused func() bool, displayMode func() int, nameMode func() int, isMPLSDisabled func() bool,
	isHistoryMode func() bool, historyChartMode func() int, historySnapshot func(time.Time) []MTRHistoryTTL,
	columnState func() ([]MTRColumn, MTRColumnEditor), dialogState func() (MTRSaveDialog, MTRHelpDialog), probeError ...func() string) func(iteration int, stats []trace.MTRHopStat)

MTRTUIPrinterWithDialogs extends the existing printer without changing its optional column-state API for callers that do not expose interactive dialogs.

func MTRTUIRender added in v1.5.2

func MTRTUIRender(w io.Writer, header MTRTUIHeader, stats []trace.MTRHopStat)

MTRTUIRender writes a standalone full frame. Interactive sessions use MTRTUIRenderer to preserve terminal history and selection across refreshes.

func MTRTUIRenderString added in v1.5.2

func MTRTUIRenderString(header MTRTUIHeader, stats []trace.MTRHopStat) string

MTRTUIRenderString 将 MTR TUI 帧渲染为字符串(方便测试)。

func MTRTablePrinter added in v1.5.2

func MTRTablePrinter(stats []trace.MTRHopStat, iteration int, mode int, nameMode int, lang string, showIPs bool)

MTRTablePrinter 将 MTR 快照渲染为经典 MTR 风格表格。 每次调用都会先清屏再重绘。

func MTRTablePrinterWithColumns added in v1.7.5

func MTRTablePrinterWithColumns(stats []trace.MTRHopStat, iteration, mode, nameMode int, lang string, showIPs bool, columns []MTRColumn)

MTRTablePrinterWithColumns preserves Hop/Host placement and the default wrapper.

func New

func New() table.Table

func PrintTraceRouteNav

func PrintTraceRouteNav(ip net.IP, domain string, dataOrigin string, maxHops int, packetSize int, srcAddr string, mode string)

func PrintTraceStopReason added in v1.7.3

func PrintTraceStopReason(reason *trace.StopReason) error

PrintTraceStopReason writes a styled stop line to the terminal output.

func RealtimePrinter

func RealtimePrinter(res *trace.Result, ttl int)

func RealtimePrinterWithRouter

func RealtimePrinterWithRouter(res *trace.Result, ttl int)

func TracerouteTablePrinter

func TracerouteTablePrinter(res *trace.Result, clearScreen bool)

func ValidateMTRColumns added in v1.7.5

func ValidateMTRColumns(columns []MTRColumn) error

func Version

func Version()

func WriteMTRReplayReport added in v1.7.5

func WriteMTRReplayReport(w io.Writer, stats []trace.MTRHopStat, opts MTRReportOptions) error

WriteMTRReplayReport shares the live report's host and metric formatting, while writing to an explicit sink without terminal escapes.

func WriteMTRSnapshot added in v1.7.5

func WriteMTRSnapshot(w io.Writer, s MTRSnapshot, format string) error

WriteMTRSnapshot writes an immutable report as txt, json or self-contained html. JSON preserves all recorded values; human-readable formats clean display text.

func WriteTraceStopReason added in v1.7.3

func WriteTraceStopReason(w io.Writer, reason *trace.StopReason) error

WriteTraceStopReason writes the canonical plain-text stop line.

Types

type HopInfo

type HopInfo int
const (
	General HopInfo = 0
	IXP     HopInfo = 1
	Peer    HopInfo = 2
	PoP     HopInfo = 3
	Aboard  HopInfo = 4
)

type MTRColumn added in v1.7.5

type MTRColumn uint8

MTRColumn selects a human-readable statistic; it never changes probe data.

const (
	MTRColumnLoss MTRColumn = iota
	MTRColumnSnt
	MTRColumnReceived
	MTRColumnLast
	MTRColumnAvg
	MTRColumnBest
	MTRColumnWrst
	MTRColumnStDev
	MTRColumnDropped
	MTRColumnGMean
	MTRColumnJitter
	MTRColumnJitterAvg
	MTRColumnJitterMax
	MTRColumnJitterInterarrival
	MTRColumnSpace
)

func DefaultMTRColumns added in v1.7.5

func DefaultMTRColumns() []MTRColumn

func ParseMTRColumns added in v1.7.5

func ParseMTRColumns(input string, codes bool) ([]MTRColumn, error)

ParseMTRColumns accepts comma-separated CLI names or TUI codes; spaces add display spacing.

type MTRColumnEditor added in v1.7.5

type MTRColumnEditor struct {
	Active       bool
	Draft, Error string
}

MTRColumnEditor is an immutable frame snapshot supplied by the TUI controller.

type MTRHelpDialog added in v1.7.5

type MTRHelpDialog struct {
	Active bool
	Offset int
}

type MTRHistoryChartMode added in v1.7.0

type MTRHistoryChartMode int
const (
	MTRHistoryHeatmap MTRHistoryChartMode = iota
	MTRHistoryBars
	MTRHistorySparkline
)

type MTRHistorySample added in v1.7.0

type MTRHistorySample struct {
	At      time.Time
	RTT     time.Duration
	Timeout bool
}

type MTRHistoryStore added in v1.7.0

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

func NewMTRHistoryStore added in v1.7.0

func NewMTRHistoryStore(window time.Duration) *MTRHistoryStore

func (*MTRHistoryStore) AddProbeEvent added in v1.7.0

func (s *MTRHistoryStore) AddProbeEvent(event trace.MTRProbeEvent)

func (*MTRHistoryStore) AddProbeEventAt added in v1.7.5

func (s *MTRHistoryStore) AddProbeEventAt(event trace.MTRProbeEvent, now time.Time)

AddProbeEventAt uses the caller's clock, allowing offline history to retain samples from the recording instead of pruning them against wall time.

func (*MTRHistoryStore) Reset added in v1.7.0

func (s *MTRHistoryStore) Reset()

func (*MTRHistoryStore) Snapshot added in v1.7.0

func (s *MTRHistoryStore) Snapshot(now time.Time) []MTRHistoryTTL

type MTRHistoryTTL added in v1.7.0

type MTRHistoryTTL struct {
	TTL     int
	Samples []MTRHistorySample
}

type MTRReplayEditor added in v1.7.5

type MTRReplayEditor struct {
	Active       bool
	Draft, Error string
}

type MTRReplayStatus added in v1.7.5

type MTRReplayStatus struct {
	Cursor, Duration  time.Duration
	Complete, Seeking bool
	RecordedPaused    bool
}

type MTRReportOptions added in v1.5.2

type MTRReportOptions struct {
	Columns   []MTRColumn
	StartTime time.Time
	SrcHost   string
	Wide      bool
	ShowIPs   bool
	Lang      string
}

MTRReportOptions 控制报告输出细节。

type MTRRow added in v1.5.2

type MTRRow struct {
	Hop   string
	Loss  string
	Snt   string
	Last  string
	Avg   string
	Best  string
	Wrst  string
	StDev string
	Host  string
}

MTRRow 表示表格中一行经过格式化的数据。

func MTRRenderTable added in v1.5.2

func MTRRenderTable(stats []trace.MTRHopStat, mode int, nameMode int, lang string, showIPs bool) []MTRRow

MTRRenderTable 仅返回格式化后的行数据(用于测试/非终端场景)。 mode / nameMode / lang 控制 Host 列内容;传 -1 / -1 / "" 等效于 HostModeFull + HostNamePTRorIP + "en"(向后兼容)。

type MTRSaveDialog added in v1.7.5

type MTRSaveDialog struct {
	Active, Busy  bool
	Format, Path  string
	Error, Notice string
	CapturedAt    time.Time
	Field         int // 0 = format, 1 = path
}

Dialog values are immutable frame state supplied by the input controller.

type MTRSnapshot added in v1.7.5

type MTRSnapshot struct {
	SchemaVersion int                 `json:"schema_version"`
	Type          string              `json:"type"`
	Source        string              `json:"source"`
	CapturedAt    time.Time           `json:"captured_at"`
	ElapsedNS     int64               `json:"elapsed_ns"`
	Generation    uint64              `json:"generation"`
	State         string              `json:"state"`
	Session       *mtrsession.Session `json:"session"`
	PathEnd       *trace.StopReason   `json:"path_end"`
	Stats         []trace.MTRHopStat  `json:"stats"`
	Replay        *MTRSnapshotReplay  `json:"replay,omitempty"`
}

MTRSnapshot is a point-in-time report, not an event recording or a completed session. Callers own the snapshot and must not change it while writing.

type MTRSnapshotReplay added in v1.7.5

type MTRSnapshotReplay struct {
	CursorNS          int64 `json:"cursor_ns"`
	DurationNS        int64 `json:"duration_ns"`
	RecordingComplete bool  `json:"recording_complete"`
	RecordedPaused    bool  `json:"recorded_paused"`
}

MTRSnapshotReplay describes the committed replay position in a snapshot.

type MTRTUIHeader added in v1.5.2

type MTRTUIHeader struct {
	Columns      []MTRColumn // nil preserves the original metrics layout
	ColumnEditor MTRColumnEditor
	Target       string
	StartTime    time.Time
	Status       MTRTUIStatus
	Iteration    int
	// 以下为 v2 新增字段
	Domain   string // 用户输入的域名(可为空)
	TargetIP string // 解析后的目标 IP
	Version  string // 软件版本,如 "v1.3.0"
	// 以下为 v3 新增字段
	SrcHost          string // 源主机名
	SrcIP            string // 源 IP
	Lang             string // 语言("en" / "cn")
	DisplayMode      int    // 显示模式 0-4
	NameMode         int    // Host 基础显示 0=PTR/IP, 1=IP only
	ShowIPs          bool   // 是否显示 PTR+IP(nameMode=0 时生效)
	APIInfo          string // preferred API 信息(纯文本,可为空)
	ProbeError       string // latest retry diagnostic, displayed by the rendering worker
	DisableMPLS      bool   // 是否隐藏 MPLS 行(运行时 toggle)
	HistoryMode      bool
	HistoryChartMode int
	History          []MTRHistoryTTL
	HistoryNow       time.Time
	Now              time.Time // zero uses the live clock
	Replay           *MTRReplayStatus
	ReplayEditor     MTRReplayEditor
	SaveDialog       MTRSaveDialog
	HelpDialog       MTRHelpDialog
}

MTRTUIHeader 包含帧顶部显示的元信息。

type MTRTUIRenderer added in v1.7.6

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

MTRTUIRenderer updates an interactive session without erasing the display. Its zero value is ready for use. The session's single rendering worker must call it sequentially with the same terminal writer throughout the session.

func (*MTRTUIRenderer) Render added in v1.7.6

func (r *MTRTUIRenderer) Render(w io.Writer, header MTRTUIHeader, stats []trace.MTRHopStat) error

Render writes only changed rows, using the current stdout terminal size.

type MTRTUIStatus added in v1.5.2

type MTRTUIStatus int

MTRTUIStatus 表示 TUI 当前运行状态。

const (
	MTRTUIRunning MTRTUIStatus = iota
	MTRTUIPaused
)

Jump to

Keyboard shortcuts

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