feat: compact search, simplify awk

This commit is contained in:
Viktor Varland 2026-02-16 20:25:45 +01:00
parent d8f855dfca
commit 2f8ba08d4f
No known key found for this signature in database
GPG key ID: 991E991EBEC46432
2 changed files with 23 additions and 8 deletions

View file

@ -56,17 +56,14 @@ hs() {
esac ;;
esac
local strip='awk -F" " "{OFS=\" \"; \$4=\"\"; gsub(/ +/,\" \"); print}"'
local entry="$(
verbatim search --reverse "$@" | eval $strip | \
verbatim search --compact --reverse "$@" | \
fzf --ansi --disabled --query "${*:-}" \
--bind "start:reload:verbatim search --reverse {q} | $strip" \
--bind "change:reload:sleep 0.1; verbatim search --reverse {q} | $strip || true" \
--delimiter ' '
--bind "start:reload:verbatim search --compact --reverse {q}" \
--bind "change:reload:sleep 0.1; verbatim search --compact --reverse {q} || true"
)"
local entry_cmd="$(<<<$entry awk -F' ' '{ print substr($0, index($0, $4)) }')"
local entry_cmd="$(<<<$entry sed 's/\x1b\[[0-9;]*m//g' | awk '{ print substr($0, index($0, $5)) }')"
entry_cmd="${entry_cmd//\\n/$'\n'}"
if [[ "$entry" ]]; then

View file

@ -8,6 +8,11 @@ import (
"time"
)
const (
dim = "\033[2m"
reset = "\033[0m"
)
func Run(d *sql.DB, args []string) error {
fs := flag.NewFlagSet("search", flag.ExitOnError)
host := fs.String("host", "", "filter by hostname")
@ -16,6 +21,7 @@ func Run(d *sql.DB, args []string) error {
before := fs.String("before", "", "filter entries before timestamp")
limit := fs.Int("limit", 10000, "max results")
reverse := fs.Bool("reverse", false, "newest first")
compact := fs.Bool("compact", false, "compact output with color")
fs.Parse(args)
query := strings.Join(fs.Args(), " ")
@ -71,7 +77,19 @@ func Run(d *sql.DB, args []string) error {
if t, err := time.Parse(time.RFC3339, ts); err == nil {
ts = t.Format("2006-01-02 15:04")
}
fmt.Printf("%s %s %s %s\n", ts, h, wd, cmd)
if *compact {
fmt.Printf("%s%s %s %s%s %s\n", dim, ts, h, shortenDir(wd), reset, cmd)
} else {
fmt.Printf("%s %s %s %s\n", ts, h, wd, cmd)
}
}
return rows.Err()
}
func shortenDir(dir string) string {
parts := strings.Split(dir, "/")
if len(parts) <= 3 {
return dir
}
return "../" + parts[len(parts)-2] + "/" + parts[len(parts)-1]
}