init
This commit is contained in:
24 files changed
+3822
No files matched your search
@@ -0,0 +1,343 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
|
||||
"github.com/illustris/i-am-gcroot/internal/filter"
|
||||
"github.com/illustris/i-am-gcroot/internal/roots"
|
||||
)
|
||||
|
||||
var (
|
||||
styleHeader = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("12"))
|
||||
styleColHeader = lipgloss.NewStyle().Bold(true).Underline(true)
|
||||
styleCursor = lipgloss.NewStyle().Reverse(true)
|
||||
styleMarked = lipgloss.NewStyle().Foreground(lipgloss.Color("11")).Bold(true)
|
||||
styleDim = lipgloss.NewStyle().Foreground(lipgloss.Color("8"))
|
||||
styleErr = lipgloss.NewStyle().Foreground(lipgloss.Color("9"))
|
||||
styleOK = lipgloss.NewStyle().Foreground(lipgloss.Color("10"))
|
||||
styleBox = lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).Padding(0, 1)
|
||||
|
||||
kindColor = map[roots.Kind]lipgloss.Color{
|
||||
roots.KindIndirect: "14",
|
||||
roots.KindProfile: "13",
|
||||
roots.KindDirect: "12",
|
||||
roots.KindDangling: "9",
|
||||
roots.KindRuntime: "8",
|
||||
}
|
||||
)
|
||||
|
||||
func (m *Model) View() string {
|
||||
if m.quitting {
|
||||
return ""
|
||||
}
|
||||
if m.width == 0 {
|
||||
return "loading..."
|
||||
}
|
||||
switch m.mode {
|
||||
case modeHelp:
|
||||
return m.overlay(helpText)
|
||||
case modeDetails:
|
||||
return m.overlay(m.details())
|
||||
case modeResults:
|
||||
return m.overlay(m.resultsView())
|
||||
case modeConfirmDelete:
|
||||
return m.overlay(m.confirmView())
|
||||
case modeConfirmGC:
|
||||
return m.overlay("Run nix-store --gc now?\n\nThis deletes every store path that is no longer reachable from a root.\n\n[y] yes [n] no")
|
||||
}
|
||||
return m.listView()
|
||||
}
|
||||
|
||||
func (m *Model) overlay(body string) string {
|
||||
box := styleBox.Width(min(m.width-4, 120)).Render(body)
|
||||
return lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, box)
|
||||
}
|
||||
|
||||
const (
|
||||
wMark = 2
|
||||
wKind = 9
|
||||
wOwner = 10
|
||||
wAge = 6
|
||||
wSize = 8
|
||||
)
|
||||
|
||||
func (m *Model) columns() (link, target int) {
|
||||
rest := m.width - wMark - wKind - wOwner - wAge - wSize - 6
|
||||
if rest < 20 {
|
||||
rest = 20
|
||||
}
|
||||
target = rest * 2 / 5
|
||||
return rest - target, target
|
||||
}
|
||||
|
||||
func (m *Model) listView() string {
|
||||
var b strings.Builder
|
||||
st := m.set.Status()
|
||||
|
||||
title := styleHeader.Render("i-am-gcroot")
|
||||
info := fmt.Sprintf(" %d roots, %d shown, %d marked (up to %s)", st.Total, len(m.view), len(m.marks), filter.HumanSize(m.markedSize()))
|
||||
if m.opts.WebURL != "" {
|
||||
info += styleDim.Render(" web: " + m.opts.WebURL)
|
||||
}
|
||||
b.WriteString(trunc(title+info, m.width+20) + "\n")
|
||||
|
||||
wl, wt := m.columns()
|
||||
hdr := fmt.Sprintf("%-*s %-*s %-*s %*s %*s %-*s %-*s", wMark, "", wKind, "KIND", wOwner, "OWNER", wAge, "AGE", wSize, "CLOSURE", wl, "LINK", wt, "TARGET")
|
||||
b.WriteString(styleColHeader.Render(trunc(hdr, m.width)) + "\n")
|
||||
|
||||
h := m.listHeight()
|
||||
now := time.Now()
|
||||
for i := m.offset; i < m.offset+h; i++ {
|
||||
if i >= len(m.view) {
|
||||
b.WriteString("\n")
|
||||
continue
|
||||
}
|
||||
b.WriteString(m.row(&m.view[i], i == m.cursor, wl, wt, now) + "\n")
|
||||
}
|
||||
|
||||
// footer
|
||||
sortDir := "asc"
|
||||
if m.desc {
|
||||
sortDir = "desc"
|
||||
}
|
||||
hidden := " "
|
||||
if m.hidden {
|
||||
hidden = "x"
|
||||
}
|
||||
line := fmt.Sprintf("%s h[%s]hidden sort:%s %s", kindList(m.kinds), hidden, m.sortKey, sortDir)
|
||||
if st.Sizing {
|
||||
line += fmt.Sprintf(" sizing %d/%d", st.SizesDone, st.SizesTodo)
|
||||
}
|
||||
if m.scanning {
|
||||
line += " scanning..."
|
||||
}
|
||||
b.WriteString(styleDim.Render(trunc(line, m.width)) + "\n")
|
||||
b.WriteString(styleDim.Render(trunc(hints, m.width)) + "\n")
|
||||
if a := m.activity(); a != "" {
|
||||
b.WriteString(a + "\n")
|
||||
}
|
||||
if m.mode == modeFilter || m.input.Value() != "" {
|
||||
b.WriteString(m.input.View())
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
const hints = "space mark a mark all d delete X delete+collect D dry-run g gc / filter s sort h hidden enter details R results ? help q quit"
|
||||
|
||||
// activity is the line under the key hints: a running operation, a filter
|
||||
// error, or the outcome of the last operation.
|
||||
func (m *Model) activity() string {
|
||||
switch {
|
||||
case m.busy != "":
|
||||
return styleMarked.Render(trunc(fmt.Sprintf("%s... %ds", m.busy, int(time.Since(m.since).Seconds())), m.width))
|
||||
case m.queryErr != "":
|
||||
return styleErr.Render(trunc(m.queryErr, m.width))
|
||||
case m.status == "":
|
||||
return ""
|
||||
case m.statusOK:
|
||||
return styleOK.Render(trunc(m.status, m.width))
|
||||
}
|
||||
return styleErr.Render(trunc(m.status, m.width))
|
||||
}
|
||||
|
||||
func (m *Model) row(r *roots.Root, cursor bool, wl, wt int, now time.Time) string {
|
||||
mark := " "
|
||||
if m.marks[r.Link] {
|
||||
mark = "*"
|
||||
} else if r.Protected {
|
||||
mark = "!"
|
||||
}
|
||||
link := r.Link
|
||||
if r.Kind == roots.KindRuntime && r.Command != "" {
|
||||
link += " " + r.Command
|
||||
}
|
||||
target := r.TargetName()
|
||||
switch {
|
||||
case r.Kind == roots.KindRuntime && r.Count > 1:
|
||||
target = fmt.Sprintf("(%d paths)", r.Count)
|
||||
case r.Kind == roots.KindDangling:
|
||||
target = "(missing)"
|
||||
case r.Reason != "" && r.Target == "":
|
||||
target = "(" + r.Reason + ")"
|
||||
}
|
||||
size := filter.HumanSize(r.ClosureSize)
|
||||
if r.Kind == roots.KindDangling {
|
||||
size = "-"
|
||||
}
|
||||
kind := fmt.Sprintf("%-*s", wKind, r.Kind)
|
||||
if !cursor {
|
||||
kind = lipgloss.NewStyle().Foreground(kindColor[r.Kind]).Render(kind)
|
||||
}
|
||||
s := fmt.Sprintf("%-*s %s %-*s %*s %*s %-*s %-*s",
|
||||
wMark, mark, kind, wOwner, trunc(r.Owner, wOwner), wAge, filter.HumanAge(r.ModTime, now),
|
||||
wSize, size, wl, midTrunc(link, wl), wt, trunc(target, wt))
|
||||
switch {
|
||||
case cursor:
|
||||
return styleCursor.Render(s)
|
||||
case m.marks[r.Link]:
|
||||
return styleMarked.Render(s)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (m *Model) markedSize() int64 {
|
||||
var n int64
|
||||
for _, r := range m.all {
|
||||
if m.marks[r.Link] && r.ClosureSize > 0 {
|
||||
n += r.ClosureSize
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (m *Model) details() string {
|
||||
r := m.view[m.cursor]
|
||||
var b strings.Builder
|
||||
field := func(k, v string) {
|
||||
if v != "" {
|
||||
fmt.Fprintf(&b, "%s %s\n", styleHeader.Render(fmt.Sprintf("%-12s", k)), v)
|
||||
}
|
||||
}
|
||||
field("kind", string(r.Kind))
|
||||
field("link", r.Link)
|
||||
field("via", r.Via)
|
||||
field("target", r.Target)
|
||||
field("owner", fmt.Sprintf("%s (uid %d)", r.Owner, r.UID))
|
||||
if !r.ModTime.IsZero() {
|
||||
field("modified", r.ModTime.Format(time.RFC3339)+" ("+filter.HumanAge(r.ModTime, time.Now())+" ago)")
|
||||
}
|
||||
if r.Kind == roots.KindProfile {
|
||||
field("profile", r.Profile)
|
||||
cur := ""
|
||||
if r.Current {
|
||||
cur = " (current)"
|
||||
}
|
||||
field("generation", fmt.Sprintf("%d%s", r.Generation, cur))
|
||||
}
|
||||
if r.Count > 0 {
|
||||
field("store paths", fmt.Sprint(r.Count))
|
||||
}
|
||||
field("command", r.Command)
|
||||
if r.Target != "" {
|
||||
field("closure", filter.HumanSize(r.ClosureSize))
|
||||
field("nar size", filter.HumanSize(r.NarSize))
|
||||
}
|
||||
if r.Protected {
|
||||
field("protected", r.Reason)
|
||||
} else {
|
||||
field("note", r.Reason)
|
||||
}
|
||||
b.WriteString("\n" + styleDim.Render("esc to close"))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *Model) confirmView() string {
|
||||
var b strings.Builder
|
||||
var size int64
|
||||
protected := 0
|
||||
for _, r := range m.pending {
|
||||
if r.ClosureSize > 0 {
|
||||
size += r.ClosureSize
|
||||
}
|
||||
if r.Protected {
|
||||
protected++
|
||||
}
|
||||
}
|
||||
verb := "Delete"
|
||||
if m.collect {
|
||||
verb = "Delete and garbage collect"
|
||||
}
|
||||
fmt.Fprintf(&b, "%s %d root(s)? Closures total up to %s (shared paths may stay alive).\n", verb, len(m.pending), filter.HumanSize(size))
|
||||
if m.collect {
|
||||
b.WriteString(styleDim.Render("Store paths only reachable through these roots are deleted right away.") + "\n")
|
||||
}
|
||||
b.WriteString("\n")
|
||||
show := min(len(m.pending), max(3, m.height-12))
|
||||
for _, r := range m.pending[:show] {
|
||||
fmt.Fprintf(&b, " %-9s %s\n", r.Kind, r.Link)
|
||||
}
|
||||
if len(m.pending) > show {
|
||||
fmt.Fprintf(&b, " ... and %d more\n", len(m.pending)-show)
|
||||
}
|
||||
if protected > 0 && !m.opts.Force {
|
||||
b.WriteString(styleErr.Render(fmt.Sprintf("\n%d protected root(s) will be skipped (use --force to override).\n", protected)))
|
||||
}
|
||||
b.WriteString("\n[y] delete [n] cancel")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *Model) resultsView() string {
|
||||
var b strings.Builder
|
||||
if m.dryRun {
|
||||
b.WriteString(styleHeader.Render("Dry run: these actions would be taken") + "\n\n")
|
||||
} else {
|
||||
b.WriteString(styleHeader.Render("Delete results") + "\n\n")
|
||||
}
|
||||
if m.collected != "" {
|
||||
b.WriteString(m.collected + "\n\n")
|
||||
}
|
||||
show := min(len(m.results), max(3, m.height-10))
|
||||
for _, r := range m.results[:show] {
|
||||
if r.OK() {
|
||||
b.WriteString(styleOK.Render("ok ") + r.Action + "\n")
|
||||
} else {
|
||||
b.WriteString(styleErr.Render("fail ") + r.Link + ": " + r.Err + "\n")
|
||||
}
|
||||
}
|
||||
if len(m.results) > show {
|
||||
fmt.Fprintf(&b, "... and %d more\n", len(m.results)-show)
|
||||
}
|
||||
b.WriteString("\n" + styleDim.Render("esc to close"))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
const helpText = `Keys
|
||||
j/k, up/down, pgup/pgdn, home/end move
|
||||
space / x mark/unmark root under cursor
|
||||
a / A mark all shown (non-protected) / clear marks
|
||||
d delete marked roots (or the one under cursor)
|
||||
X delete, then garbage collect just the paths they held
|
||||
D dry run: show what delete would do
|
||||
g run nix-store --gc
|
||||
/ edit filter, enter/esc to leave, esc again clears it
|
||||
1-5 toggle kinds: indirect profile direct dangling runtime
|
||||
0 reset kind toggles
|
||||
h show/hide nix cache roots (~/.cache/nix/...)
|
||||
s / S cycle sort key / reverse
|
||||
enter details
|
||||
R full results of the last delete / dry run
|
||||
r rescan
|
||||
q quit
|
||||
|
||||
Filter syntax (terms are ANDed, prefix with - to negate)
|
||||
foo /regex/ kind:indirect,dangling owner:me under:/tmp
|
||||
older:30d newer:2h size>1G size<10M is:protected is:current is:hidden`
|
||||
|
||||
func trunc(s string, w int) string {
|
||||
if w <= 0 {
|
||||
return ""
|
||||
}
|
||||
r := []rune(s)
|
||||
if len(r) <= w {
|
||||
return s
|
||||
}
|
||||
if w == 1 {
|
||||
return string(r[:1])
|
||||
}
|
||||
return string(r[:w-1]) + "~"
|
||||
}
|
||||
|
||||
// midTrunc keeps both ends of a path, which carry the most information.
|
||||
func midTrunc(s string, w int) string {
|
||||
r := []rune(s)
|
||||
if len(r) <= w || w < 5 {
|
||||
return trunc(s, w)
|
||||
}
|
||||
head := (w - 3) / 3
|
||||
tail := w - 3 - head
|
||||
return string(r[:head]) + "..." + string(r[len(r)-tail:])
|
||||
}
|
||||
Reference in new issue
Block a user