initial commit [141.14.140.180,mike]
This commit is contained in:
commit
e34ba75b7b
33 files changed
+6267
No files matched your search
@@ -0,0 +1,607 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
"github.com/mattn/go-runewidth"
|
||||
|
||||
"nscan/internal/scanner"
|
||||
"nscan/internal/store"
|
||||
)
|
||||
|
||||
type column int
|
||||
|
||||
const (
|
||||
colName column = iota
|
||||
colIP
|
||||
colMAC
|
||||
colVendor
|
||||
colLastSeen
|
||||
colRTT
|
||||
colServices
|
||||
numCols
|
||||
)
|
||||
|
||||
var colTitles = [numCols]string{"Name", "IP address", "MAC address", "Vendor", "Last seen", "Ping", "Services"}
|
||||
|
||||
// Fixed-width columns; the others share the remaining space by weight.
|
||||
var (
|
||||
colFixed = map[column]int{colIP: 15, colMAC: 17, colLastSeen: 10, colRTT: 8}
|
||||
colMin = map[column]int{colName: 14, colVendor: 10, colServices: 10}
|
||||
colWeight = map[column]int{colName: 5, colVendor: 3, colServices: 3}
|
||||
)
|
||||
|
||||
var sortOrder = []column{colName, colIP, colMAC, colVendor, colLastSeen, colRTT}
|
||||
|
||||
func nextSortColumn(c column) column {
|
||||
i := slices.Index(sortOrder, c)
|
||||
return sortOrder[(i+1)%len(sortOrder)]
|
||||
}
|
||||
|
||||
const rowPrefix = 6 // "▸ ●⚑✓ "
|
||||
|
||||
func (m *model) layout() ([]column, []int) {
|
||||
var cols []column
|
||||
for c := range numCols {
|
||||
if m.cols[c] {
|
||||
cols = append(cols, c)
|
||||
}
|
||||
}
|
||||
avail := m.w - rowPrefix - 2*max(0, len(cols)-1)
|
||||
fixed, weights := 0, 0
|
||||
for _, c := range cols {
|
||||
if w, ok := colFixed[c]; ok {
|
||||
fixed += w
|
||||
} else {
|
||||
weights += colWeight[c]
|
||||
}
|
||||
}
|
||||
flex := max(0, avail-fixed)
|
||||
widths := make([]int, len(cols))
|
||||
lastFlex, used := -1, 0
|
||||
for i, c := range cols {
|
||||
if w, ok := colFixed[c]; ok {
|
||||
widths[i] = w
|
||||
continue
|
||||
}
|
||||
widths[i] = max(colMin[c], flex*colWeight[c]/weights)
|
||||
used += widths[i]
|
||||
lastFlex = i
|
||||
}
|
||||
if lastFlex >= 0 && used < flex {
|
||||
widths[lastFlex] += flex - used
|
||||
}
|
||||
return cols, widths
|
||||
}
|
||||
|
||||
// fit truncates or pads s to exactly w cells.
|
||||
func fit(s string, w int) string {
|
||||
if w <= 0 {
|
||||
return ""
|
||||
}
|
||||
return runewidth.FillRight(runewidth.Truncate(s, w, "…"), w)
|
||||
}
|
||||
|
||||
// padLines truncates or pads lines to exactly n entries.
|
||||
func padLines(lines []string, n int) []string {
|
||||
n = max(0, n)
|
||||
if len(lines) > n {
|
||||
return lines[:n]
|
||||
}
|
||||
for len(lines) < n {
|
||||
lines = append(lines, "")
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
// screen fills the terminal: body lines on top, the footer on the last line.
|
||||
func (m *model) screen(body []string, footer string) string {
|
||||
return m.clipLines(append(padLines(body, m.h-1), footer))
|
||||
}
|
||||
|
||||
// clipLines cuts every line to the terminal width.
|
||||
func (m *model) clipLines(lines []string) string {
|
||||
for i, l := range lines {
|
||||
lines[i] = ansi.Truncate(l, m.w, "")
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func (m *model) View() string {
|
||||
if m.w == 0 {
|
||||
return ""
|
||||
}
|
||||
switch m.mode {
|
||||
case viewDetail:
|
||||
return m.viewDetail()
|
||||
case viewPorts:
|
||||
return m.viewPorts()
|
||||
case viewPing:
|
||||
return m.viewPing()
|
||||
case viewHistory:
|
||||
return m.viewHistory()
|
||||
case viewHelp:
|
||||
return m.viewHelp()
|
||||
case viewColumns:
|
||||
return m.viewColumns()
|
||||
}
|
||||
return m.viewList()
|
||||
}
|
||||
|
||||
// --- list -----------------------------------------------------------------
|
||||
|
||||
func (m *model) titleBar(right string) string {
|
||||
n := m.cfg.Network
|
||||
left := styleBarName.Render(" nscan ")
|
||||
if m.cfg.Version != "" {
|
||||
left += styleBarDim.Render(m.cfg.Version + " ")
|
||||
}
|
||||
left += styleBar.Render(" ")
|
||||
info := fmt.Sprintf("%s · %s", n.Iface.Name, m.cfg.Range)
|
||||
if n.Gateway.IsValid() {
|
||||
info += " · Gateway " + n.Gateway.String()
|
||||
}
|
||||
info += " · This host " + n.LocalIP.String()
|
||||
if right != "" {
|
||||
info += " " + right
|
||||
}
|
||||
return left + styleBar.Render(fit(info, max(0, m.w-lipgloss.Width(left))))
|
||||
}
|
||||
|
||||
func (m *model) viewList() string {
|
||||
now := time.Now()
|
||||
lines := []string{m.titleBar("")}
|
||||
|
||||
online, offline := 0, 0
|
||||
for _, d := range m.devs {
|
||||
if d.online {
|
||||
online++
|
||||
} else {
|
||||
offline++
|
||||
}
|
||||
}
|
||||
status := styleOnline.Render("●") + fmt.Sprintf(" %d online ", online) +
|
||||
styleMuted.Render("○") + fmt.Sprintf(" %d offline", offline)
|
||||
arrow := "↑"
|
||||
if m.sortDesc {
|
||||
arrow = "↓"
|
||||
}
|
||||
status += styleDim.Render(" Sort: ") + colTitles[m.sortCol] + " " + arrow
|
||||
if m.auto {
|
||||
next := ""
|
||||
if !m.scanning && !m.nextScan.IsZero() {
|
||||
next = fmt.Sprintf(" (in %ds)", max(0, int(time.Until(m.nextScan).Seconds()+0.5)))
|
||||
}
|
||||
status += styleDim.Render(" Auto-scan: ") + "every " + shortDuration(m.cfg.Interval) + next
|
||||
} else {
|
||||
status += styleDim.Render(" Auto-scan: ") + "off"
|
||||
}
|
||||
if m.cumulative {
|
||||
status += " " + styleWarn.Render("[Cumulative]")
|
||||
}
|
||||
if m.hideWhitelisted {
|
||||
status += " " + styleWarn.Render("[Whitelist hidden]")
|
||||
}
|
||||
if m.filter != "" {
|
||||
status += " " + styleMatch.Render("[Filter: "+m.filter+"]")
|
||||
}
|
||||
lines = append(lines, " "+status, " "+m.scanStatus(now), "")
|
||||
|
||||
cols, widths := m.layout()
|
||||
header := strings.Repeat(" ", rowPrefix)
|
||||
for i, c := range cols {
|
||||
title := colTitles[c]
|
||||
if c == m.sortCol {
|
||||
title += " " + arrow
|
||||
}
|
||||
if i > 0 {
|
||||
header += " "
|
||||
}
|
||||
header += fit(title, widths[i])
|
||||
}
|
||||
lines = append(lines, styleHeader.Render(header))
|
||||
|
||||
th := m.tableHeight()
|
||||
switch {
|
||||
case len(m.list) == 0 && m.scanning:
|
||||
lines = append(lines, styleDim.Render(" Looking for devices …"))
|
||||
case len(m.list) == 0 && m.filter != "":
|
||||
lines = append(lines, styleDim.Render(" No device matches the filter (esc clears it)"))
|
||||
case len(m.list) == 0:
|
||||
lines = append(lines, styleDim.Render(" No devices found"))
|
||||
}
|
||||
for i := m.offset; i < len(m.list) && i < m.offset+th; i++ {
|
||||
lines = append(lines, m.renderRow(m.list[i], cols, widths, i == m.cursor, now))
|
||||
}
|
||||
for len(lines) < 5+th {
|
||||
lines = append(lines, "")
|
||||
}
|
||||
|
||||
if len(m.events) > 0 {
|
||||
lines = append(lines, styleRule.Render("─ ")+styleDim.Render("Events ")+styleRule.Render(strings.Repeat("─", max(0, m.w-10))))
|
||||
for _, e := range m.events[max(0, len(m.events)-3):] {
|
||||
lines = append(lines, " "+renderEvent(e, true))
|
||||
}
|
||||
}
|
||||
return m.screen(lines, m.footer("r scan", "enter details", "p ports", "i ping", "o WoL", "n name",
|
||||
"t track", "w whitelist", "c cumulative", "h history", "/ filter", "? help", "q quit"))
|
||||
}
|
||||
|
||||
func (m *model) scanStatus(now time.Time) string {
|
||||
if m.scanning {
|
||||
p := m.progress
|
||||
text := styleDim.Render("Scanning … " + p.Phase)
|
||||
if p.Total > 0 {
|
||||
return fmt.Sprintf("%s %s %s", progressBar(p.Done, p.Total, 24), text, styleDim.Render(fmt.Sprintf("%d/%d", p.Done, p.Total)))
|
||||
}
|
||||
return styleAccent.Render(spinner(now)) + " " + text
|
||||
}
|
||||
status := styleDim.Render("No scan yet")
|
||||
if !m.lastScan.IsZero() {
|
||||
status = styleDim.Render(fmt.Sprintf("Last scan %s (%.1fs, %s)",
|
||||
m.lastScan.Format("15:04:05"), m.lastDur.Seconds(), plural(len(m.cfg.Targets), "address", "addresses")))
|
||||
}
|
||||
if m.cfg.OUI.Len() == 0 {
|
||||
status += styleWarn.Render(" Vendor database missing – run: nscan oui update")
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
func spinner(now time.Time) string {
|
||||
frames := []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
|
||||
return frames[now.UnixMilli()/100%int64(len(frames))]
|
||||
}
|
||||
|
||||
func (m *model) renderRow(d *device, cols []column, widths []int, selected bool, now time.Time) string {
|
||||
meta := m.meta[d.key]
|
||||
dot, dotStyle := "●", styleOnline
|
||||
if !d.online {
|
||||
dot, dotStyle = "○", styleMuted
|
||||
}
|
||||
flag, wl := " ", " "
|
||||
if meta.Tracked {
|
||||
flag = "⚑"
|
||||
}
|
||||
if meta.Whitelisted {
|
||||
wl = "✓"
|
||||
}
|
||||
cells := make([]string, len(cols))
|
||||
for i, c := range cols {
|
||||
cells[i] = fit(m.cellText(d, c, now), widths[i])
|
||||
}
|
||||
if selected {
|
||||
rest := fit(" "+strings.Join(cells, " "), max(0, m.w-5))
|
||||
return styleSelPtr.Render("▸ ") + onSel(dotStyle).Render(dot) + onSel(styleTracked).Render(flag) +
|
||||
onSel(styleMuted).Render(wl) + styleSel.Render(rest)
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString(" " + dotStyle.Render(dot) + styleTracked.Render(flag) + styleMuted.Render(wl) + " ")
|
||||
for i, c := range cols {
|
||||
if i > 0 {
|
||||
b.WriteString(" ")
|
||||
}
|
||||
style := cellStyle[c]
|
||||
if !d.online {
|
||||
style = styleMuted
|
||||
}
|
||||
b.WriteString(style.Render(cells[i]))
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *model) cellText(d *device, c column, now time.Time) string {
|
||||
h := &d.host
|
||||
switch c {
|
||||
case colName:
|
||||
name := m.label(d)
|
||||
if name == "" {
|
||||
name = "–"
|
||||
}
|
||||
switch {
|
||||
case h.Self:
|
||||
name += " (me)"
|
||||
case h.Gateway:
|
||||
name += " (Gateway)"
|
||||
}
|
||||
return name
|
||||
case colIP:
|
||||
return h.IP.String()
|
||||
case colMAC:
|
||||
if len(h.MAC) == 0 {
|
||||
return "–"
|
||||
}
|
||||
return h.MAC.String()
|
||||
case colVendor:
|
||||
return orDash(h.VendorLabel())
|
||||
case colLastSeen:
|
||||
if d.online {
|
||||
return "now"
|
||||
}
|
||||
return ago(d.lastSeen, now)
|
||||
case colRTT:
|
||||
if !d.online || h.RTT <= 0 {
|
||||
return "–"
|
||||
}
|
||||
return formatRTT(h.RTT)
|
||||
case colServices:
|
||||
return serviceSummary(h.Services)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func serviceSummary(svcs []scanner.Service) string {
|
||||
var names []string
|
||||
for _, s := range svcs {
|
||||
if n := s.Short(); !slices.Contains(names, n) {
|
||||
names = append(names, n)
|
||||
}
|
||||
}
|
||||
return strings.Join(names, ", ")
|
||||
}
|
||||
|
||||
func (m *model) footer(hints ...string) string {
|
||||
if m.inputKind != inputNone {
|
||||
return " " + m.input.View() + styleMuted.Render(" enter apply · esc cancel")
|
||||
}
|
||||
if m.flash != "" && time.Since(m.flashAt) < 6*time.Second {
|
||||
if m.flashErr {
|
||||
return " " + styleErr.Render(m.flash)
|
||||
}
|
||||
return " " + styleInfo.Render(m.flash)
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString(" ")
|
||||
for i, h := range hints {
|
||||
key, label, _ := strings.Cut(h, " ")
|
||||
if i > 0 {
|
||||
b.WriteString(styleMuted.Render(" · "))
|
||||
}
|
||||
b.WriteString(styleKey.Render(key) + " " + styleDim.Render(label))
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func renderEvent(e store.Event, withTime bool) string {
|
||||
label := e.Label
|
||||
if label == "" {
|
||||
label = e.IP
|
||||
}
|
||||
var sym, text string
|
||||
var style lipgloss.Style
|
||||
switch e.Kind {
|
||||
case store.EventNew:
|
||||
sym, text, style = "✚", "New device: "+label, styleNew
|
||||
if e.IP != "" && e.IP != label {
|
||||
text += styleDim.Render(" (" + e.IP + ")")
|
||||
}
|
||||
case store.EventJoin:
|
||||
sym, text, style = "→", label+" is back", styleOnline
|
||||
case store.EventLeave:
|
||||
sym, text, style = "←", label+" left the network", styleGone
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
if e.Notify {
|
||||
sym = "⚑"
|
||||
style = styleTracked.Bold(true)
|
||||
}
|
||||
out := style.Render(sym) + " " + text
|
||||
if withTime {
|
||||
out = styleTime.Render(e.Time.Format("15:04")) + " " + out
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// --- detail ---------------------------------------------------------------
|
||||
|
||||
func (m *model) viewDetail() string {
|
||||
d := m.devs[m.detailKey]
|
||||
if d == nil {
|
||||
return ""
|
||||
}
|
||||
now := time.Now()
|
||||
meta := m.meta[d.key]
|
||||
h := &d.host
|
||||
title := m.labelOrIP(d)
|
||||
lines := []string{m.titleBar(""), "", " " + styleBold.Render(title), " " + styleRule.Render(strings.Repeat("─", runewidth.StringWidth(title))), ""}
|
||||
|
||||
kv := func(k, v string) { lines = append(lines, " "+styleDim.Render(fit(k, 18))+v) }
|
||||
if d.online {
|
||||
kv("Status", styleOnline.Render("● online"))
|
||||
} else {
|
||||
kv("Status", styleMuted.Render("○ offline")+" – last seen "+styleTime.Render(ago(d.lastSeen, now)))
|
||||
}
|
||||
ip := styleText.Render(h.IP.String())
|
||||
if h.Self {
|
||||
ip += styleMuted.Render(" (this device)")
|
||||
} else if h.Gateway {
|
||||
ip += styleMuted.Render(" (Gateway)")
|
||||
}
|
||||
kv("IP address", ip)
|
||||
switch {
|
||||
case scanner.PrivateMAC(h.MAC):
|
||||
kv("MAC address", h.MAC.String()+styleMuted.Render(" (private, randomized)"))
|
||||
case len(h.MAC) > 0:
|
||||
kv("MAC address", h.MAC.String())
|
||||
default:
|
||||
kv("MAC address", "–")
|
||||
}
|
||||
vendor := orDash(h.Maker())
|
||||
switch {
|
||||
case h.Maker() != h.Vendor:
|
||||
vendor += styleMuted.Render(" (from the model)")
|
||||
case h.Vendor == "" && m.cfg.OUI.Len() == 0:
|
||||
vendor += styleMuted.Render(" (vendor database missing: nscan oui update)")
|
||||
}
|
||||
kv("Vendor", vendor)
|
||||
if h.Model != "" {
|
||||
kv("Model", h.Model)
|
||||
}
|
||||
kv("Custom name", orDash(meta.Name)+styleMuted.Render(" (n to change)"))
|
||||
kv("mDNS / Bonjour", orDash(h.MDNSName))
|
||||
kv("NetBIOS", orDash(h.NetBIOSName))
|
||||
kv("DNS", orDash(h.DNSName))
|
||||
if d.online && h.RTT > 0 {
|
||||
kv("Response time", formatRTT(h.RTT)+styleMuted.Render(" via "+h.Via))
|
||||
} else if h.Via != "" {
|
||||
kv("Found via", h.Via)
|
||||
}
|
||||
if !meta.FirstSeen.IsZero() {
|
||||
kv("First seen", styleTime.Render(meta.FirstSeen.Format("2006-01-02 15:04")))
|
||||
}
|
||||
if !d.lastSeen.IsZero() {
|
||||
kv("Last seen", styleTime.Render(d.lastSeen.Format("2006-01-02 15:04:05")))
|
||||
}
|
||||
kv("Tracked", flagValue(meta.Tracked, styleTracked)+styleMuted.Render(" (t)"))
|
||||
kv("Whitelist", flagValue(meta.Whitelisted, styleOnline)+styleMuted.Render(" (w)"))
|
||||
|
||||
lines = append(lines, "", " "+styleHeader.Render("Bonjour services"))
|
||||
if len(h.Services) == 0 {
|
||||
lines = append(lines, styleMuted.Render(" none announced"))
|
||||
}
|
||||
for _, s := range h.Services {
|
||||
lines = append(lines, " "+styleService.Render(fit(s.Type, 24))+" "+fit(s.Instance, 36)+" "+styleDim.Render(fmt.Sprintf(":%d", s.Port)))
|
||||
}
|
||||
|
||||
lines = append(lines, "", " "+styleHeader.Render("Open ports"))
|
||||
switch {
|
||||
case d.portsAt.IsZero():
|
||||
lines = append(lines, styleMuted.Render(" not scanned yet – press p for a port scan"))
|
||||
case len(d.ports) == 0:
|
||||
lines = append(lines, styleMuted.Render(" no open ports found (scan "+d.portsAt.Format("15:04")+")"))
|
||||
default:
|
||||
for _, p := range d.ports {
|
||||
lines = append(lines, " "+styleOnline.Render(fit(fmt.Sprintf("%d/tcp", p.Port), 10))+" "+orDash(p.Service))
|
||||
}
|
||||
}
|
||||
|
||||
body := lines[1:]
|
||||
avail := max(1, m.h-2)
|
||||
m.detailOff = min(m.detailOff, max(0, len(body)-avail))
|
||||
body = body[m.detailOff:min(len(body), m.detailOff+avail)]
|
||||
return m.screen(append([]string{lines[0]}, body...),
|
||||
m.footer("esc back", "p ports", "i ping", "o WoL", "b browser", "x ssh", "n name", "t track", "w whitelist"))
|
||||
}
|
||||
|
||||
// --- help & columns -------------------------------------------------------
|
||||
|
||||
var helpKeys = [][2]string{
|
||||
{"↑/↓ j/k", "Select device (PgUp/PgDn, g/G)"},
|
||||
{"enter", "Device details"},
|
||||
{"r", "Scan now"},
|
||||
{"a", "Auto-scan on/off"},
|
||||
{"c", "Cumulative mode: also show devices that are gone"},
|
||||
{"s / S", "Next sort column / reverse order"},
|
||||
{"/", "Filter (name, IP, MAC, vendor, service)"},
|
||||
{"n", "Set a custom name"},
|
||||
{"t", "Track device: notify when it leaves or returns"},
|
||||
{"w / W", "Toggle whitelist / hide whitelisted devices"},
|
||||
{"p", "Port scan"},
|
||||
{"i", "Continuous ping"},
|
||||
{"o", "Wake-on-LAN (magic packet)"},
|
||||
{"b", "Open in browser"},
|
||||
{"x", "Open SSH session"},
|
||||
{"h", "History (network timeline)"},
|
||||
{"v", "Show/hide columns"},
|
||||
{"R", "Scan a different address range"},
|
||||
{"e", "Export as JSON and CSV"},
|
||||
{"q", "Quit"},
|
||||
}
|
||||
|
||||
func (m *model) viewHelp() string {
|
||||
lines := []string{m.titleBar(""), "", " " + styleBold.Render("Keys"), ""}
|
||||
for _, k := range helpKeys {
|
||||
lines = append(lines, " "+styleKey.Render(fit(k[0], 10))+" "+k[1])
|
||||
}
|
||||
lines = append(lines, "",
|
||||
styleDim.Render(" Symbols: ")+styleOnline.Render("●")+" online "+styleMuted.Render("○")+" offline "+
|
||||
styleTracked.Render("⚑")+" tracked "+styleMuted.Render("✓")+" Whitelist",
|
||||
styleDim.Render(" Data: ")+m.cfg.Store.Dir())
|
||||
if m.cfg.Version != "" {
|
||||
lines = append(lines, styleDim.Render(" Version: ")+m.cfg.Version)
|
||||
}
|
||||
body := lines[1:]
|
||||
m.helpOff = min(m.helpOff, max(0, len(body)-(m.h-2)))
|
||||
return m.screen(append([]string{lines[0]}, body[m.helpOff:]...), m.footer("↑/↓ scroll", "esc back"))
|
||||
}
|
||||
|
||||
func (m *model) viewColumns() string {
|
||||
lines := []string{m.titleBar(""), "", " " + styleBold.Render("Columns"), ""}
|
||||
for c := range numCols {
|
||||
box, boxStyle := "[ ]", styleMuted
|
||||
if m.cols[c] {
|
||||
box, boxStyle = "[x]", styleOnline
|
||||
}
|
||||
if int(c) == m.colCursor {
|
||||
lines = append(lines, styleSelPtr.Render("▸ ")+onSel(boxStyle).Render(box)+styleSel.Render(fit(" "+colTitles[c], 26)))
|
||||
continue
|
||||
}
|
||||
lines = append(lines, " "+boxStyle.Render(box)+" "+colTitles[c])
|
||||
}
|
||||
return m.screen(lines, m.footer("space toggle", "esc done"))
|
||||
}
|
||||
|
||||
// --- formatting -----------------------------------------------------------
|
||||
|
||||
// plural formats a count with the matching noun ("1 device", "3 devices").
|
||||
func plural(n int, one, many string) string {
|
||||
if n == 1 {
|
||||
return "1 " + one
|
||||
}
|
||||
return fmt.Sprintf("%d %s", n, many)
|
||||
}
|
||||
|
||||
func orDash(s string) string {
|
||||
if s == "" {
|
||||
return "–"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func flagValue(b bool, on lipgloss.Style) string {
|
||||
if b {
|
||||
return on.Render("yes")
|
||||
}
|
||||
return styleMuted.Render("no")
|
||||
}
|
||||
|
||||
func formatRTT(d time.Duration) string {
|
||||
ms := float64(d.Microseconds()) / 1000
|
||||
switch {
|
||||
case ms < 10:
|
||||
return fmt.Sprintf("%.2f ms", ms)
|
||||
case ms < 100:
|
||||
return fmt.Sprintf("%.1f ms", ms)
|
||||
}
|
||||
return fmt.Sprintf("%.0f ms", ms)
|
||||
}
|
||||
|
||||
func ago(t, now time.Time) string {
|
||||
if t.IsZero() {
|
||||
return "–"
|
||||
}
|
||||
d := now.Sub(t)
|
||||
switch {
|
||||
case d < time.Minute:
|
||||
return "<1 min ago"
|
||||
case d < time.Hour:
|
||||
return fmt.Sprintf("%d min ago", int(d.Minutes()))
|
||||
case d < 24*time.Hour:
|
||||
return fmt.Sprintf("%d h ago", int(d.Hours()))
|
||||
case d < 7*24*time.Hour:
|
||||
return fmt.Sprintf("%d days ago", int(d.Hours()/24))
|
||||
}
|
||||
return t.Format("2006-01-02")
|
||||
}
|
||||
|
||||
func shortDuration(d time.Duration) string {
|
||||
if d%time.Minute == 0 {
|
||||
return fmt.Sprintf("%d min", int(d.Minutes()))
|
||||
}
|
||||
return fmt.Sprintf("%ds", int(d.Seconds()))
|
||||
}
|
||||
Reference in new issue
Block a user