Files
2026-10-28 08:00:16 +05:30

308 lines
8.0 KiB
Go

// Package filter parses the query language shared by the TUI and web UI.
//
// A query is a whitespace separated list of terms, all of which must match:
//
// foo substring of link, target, via or command (case-insensitive)
// /re/ regular expression over the same fields (no spaces; use \s)
// -term negates any term
// kind:K indirect, profile, direct, dangling, runtime (comma separated)
// owner:U owner name or uid; "me" is the current user
// under:/path link starts with /path
// older:30d modified more than 30 days ago (s, m, h, d, w, y)
// newer:2h modified less than 2 hours ago
// size>500M closure size bigger than 500 MiB (K, M, G, T; also <)
// is:protected protected roots; is:current for current generations
// is:hidden nix cache roots (~/.cache/nix/...), shown only when asked for
package filter
import (
"fmt"
"os/user"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/illustris/i-am-gcroot/internal/roots"
)
type pred func(r *roots.Root, now time.Time) bool
type Query struct {
Raw string
preds []pred
// ShowHidden is set when the query mentions is:hidden, which would
// otherwise never match because hidden roots are dropped first.
ShowHidden bool
}
func Parse(q string) (*Query, error) {
out := &Query{Raw: q}
for _, tok := range strings.Fields(q) {
neg := false
if strings.HasPrefix(tok, "-") && len(tok) > 1 {
neg, tok = true, tok[1:]
}
p, err := parseTerm(tok)
if err != nil {
return nil, err
}
if tok == "is:hidden" {
out.ShowHidden = true
}
if neg {
inner := p
p = func(r *roots.Root, now time.Time) bool { return !inner(r, now) }
}
out.preds = append(out.preds, p)
}
return out, nil
}
func haystack(r *roots.Root) string {
return r.Link + "\x00" + r.Target + "\x00" + r.Via + "\x00" + r.Command
}
func parseTerm(tok string) (pred, error) {
if isRegex(tok) {
re, err := regexp.Compile("(?i)" + tok[1:len(tok)-1])
if err != nil {
return nil, fmt.Errorf("bad regex %s: %w", tok, err)
}
return func(r *roots.Root, _ time.Time) bool { return re.MatchString(haystack(r)) }, nil
}
if strings.HasPrefix(tok, "size>") || strings.HasPrefix(tok, "size<") {
n, err := ParseSize(tok[5:])
if err != nil {
return nil, err
}
if tok[4] == '>' {
return func(r *roots.Root, _ time.Time) bool { return r.ClosureSize > n }, nil
}
return func(r *roots.Root, _ time.Time) bool { return r.ClosureSize >= 0 && r.ClosureSize < n }, nil
}
key, val, ok := strings.Cut(tok, ":")
if ok && val != "" {
switch key {
case "kind", "k":
kinds := map[roots.Kind]bool{}
for _, k := range strings.Split(val, ",") {
kk, err := ParseKind(k)
if err != nil {
return nil, err
}
kinds[kk] = true
}
return func(r *roots.Root, _ time.Time) bool { return kinds[r.Kind] }, nil
case "owner", "user":
if val == "me" {
if u, err := user.Current(); err == nil {
val = u.Username
}
}
return func(r *roots.Root, _ time.Time) bool {
return r.Owner == val || strconv.Itoa(r.UID) == val
}, nil
case "under", "in":
return func(r *roots.Root, _ time.Time) bool { return strings.HasPrefix(r.Link, val) }, nil
case "older", "newer":
d, err := ParseDuration(val)
if err != nil {
return nil, err
}
if key == "older" {
return func(r *roots.Root, now time.Time) bool { return !r.ModTime.IsZero() && now.Sub(r.ModTime) > d }, nil
}
return func(r *roots.Root, now time.Time) bool { return now.Sub(r.ModTime) < d }, nil
case "is":
switch val {
case "protected":
return func(r *roots.Root, _ time.Time) bool { return r.Protected }, nil
case "current":
return func(r *roots.Root, _ time.Time) bool { return r.Current }, nil
case "dangling":
return func(r *roots.Root, _ time.Time) bool { return r.Kind == roots.KindDangling }, nil
case "hidden":
return func(r *roots.Root, _ time.Time) bool { return r.Hidden }, nil
}
return nil, fmt.Errorf("unknown is:%s (protected, current, dangling, hidden)", val)
}
}
lower := strings.ToLower(tok)
return func(r *roots.Root, _ time.Time) bool {
return strings.Contains(strings.ToLower(haystack(r)), lower)
}, nil
}
// isRegex distinguishes /re/ from a plain path like /tmp/foo, which is
// matched as a substring.
func isRegex(tok string) bool {
return len(tok) > 2 && tok[0] == '/' && tok[len(tok)-1] == '/' && strings.Count(tok, "/") == 2
}
func ParseKind(s string) (roots.Kind, error) {
for _, k := range roots.AllKinds {
if strings.HasPrefix(string(k), s) && s != "" {
return k, nil
}
}
return "", fmt.Errorf("unknown kind %q", s)
}
func ParseDuration(s string) (time.Duration, error) {
if len(s) < 2 {
return 0, fmt.Errorf("bad duration %q", s)
}
n, err := strconv.ParseFloat(s[:len(s)-1], 64)
if err != nil {
return 0, fmt.Errorf("bad duration %q", s)
}
unit := map[byte]time.Duration{
's': time.Second, 'm': time.Minute, 'h': time.Hour,
'd': 24 * time.Hour, 'w': 7 * 24 * time.Hour, 'y': 365 * 24 * time.Hour,
}[s[len(s)-1]]
if unit == 0 {
return 0, fmt.Errorf("bad duration unit in %q (s, m, h, d, w, y)", s)
}
return time.Duration(n * float64(unit)), nil
}
func ParseSize(s string) (int64, error) {
s = strings.TrimSuffix(strings.TrimSuffix(strings.ToUpper(s), "B"), "I")
mult := int64(1)
if s != "" {
switch s[len(s)-1] {
case 'K':
mult = 1 << 10
case 'M':
mult = 1 << 20
case 'G':
mult = 1 << 30
case 'T':
mult = 1 << 40
}
if mult > 1 {
s = s[:len(s)-1]
}
}
n, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0, fmt.Errorf("bad size %q", s)
}
return int64(n * float64(mult)), nil
}
func (q *Query) Match(r *roots.Root, now time.Time) bool {
for _, p := range q.preds {
if !p(r, now) {
return false
}
}
return true
}
type SortKey int
const (
SortLink SortKey = iota
SortAge
SortSize
SortTarget
SortKind
)
var SortNames = []string{"link", "age", "size", "target", "kind"}
func (k SortKey) String() string { return SortNames[k] }
func ParseSort(s string) SortKey {
for i, n := range SortNames {
if n == s {
return SortKey(i)
}
}
return SortLink
}
// Apply filters by query and kind set (nil means all kinds) and sorts the
// result. Hidden roots are dropped unless showHidden or the query asks for
// them.
func Apply(rs []roots.Root, q *Query, kinds map[roots.Kind]bool, showHidden bool, key SortKey, desc bool) []roots.Root {
showHidden = showHidden || (q != nil && q.ShowHidden)
now := time.Now()
out := make([]roots.Root, 0, len(rs))
for i := range rs {
r := &rs[i]
if kinds != nil && !kinds[r.Kind] {
continue
}
if r.Hidden && !showHidden {
continue
}
if q != nil && !q.Match(r, now) {
continue
}
out = append(out, *r)
}
less := func(a, b *roots.Root) bool {
switch key {
case SortAge:
// "ascending" age means newest first
return a.ModTime.After(b.ModTime)
case SortSize:
return a.ClosureSize < b.ClosureSize
case SortTarget:
return a.TargetName() < b.TargetName()
case SortKind:
if a.Kind != b.Kind {
return a.Kind < b.Kind
}
}
return a.Link < b.Link
}
sort.SliceStable(out, func(i, j int) bool {
if desc {
return less(&out[j], &out[i])
}
return less(&out[i], &out[j])
})
return out
}
func HumanSize(n int64) string {
switch {
case n == -1:
return "..."
case n < 0:
return "-"
case n < 1<<10:
return fmt.Sprintf("%dB", n)
case n < 1<<20:
return fmt.Sprintf("%.1fK", float64(n)/(1<<10))
case n < 1<<30:
return fmt.Sprintf("%.1fM", float64(n)/(1<<20))
case n < 1<<40:
return fmt.Sprintf("%.1fG", float64(n)/(1<<30))
}
return fmt.Sprintf("%.1fT", float64(n)/(1<<40))
}
func HumanAge(t time.Time, now time.Time) string {
if t.IsZero() {
return "-"
}
d := now.Sub(t)
switch {
case d < time.Minute:
return fmt.Sprintf("%ds", int(d.Seconds()))
case d < time.Hour:
return fmt.Sprintf("%dm", int(d.Minutes()))
case d < 48*time.Hour:
return fmt.Sprintf("%dh", int(d.Hours()))
case d < 365*24*time.Hour:
return fmt.Sprintf("%dd", int(d.Hours()/24))
}
return fmt.Sprintf("%.1fy", d.Hours()/24/365)
}