summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-09-14 13:26:51 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-09-14 13:26:51 +0200
commit07c24054cab965800983ef40f53a05c2db131ede (patch)
tree741db561d614b35f8f94fee04fb5ce663a629a0b /internal
parent70dccf8d573028aaed64185acb8e134e5339afa3 (diff)
downloadkrino-07c24054cab965800983ef40f53a05c2db131ede.tar.gz
krino-07c24054cab965800983ef40f53a05c2db131ede.zip
krino: 0.0.3 — the plan as one block per file, wrapped, and -Pv0.0.3
Each file shows its steps, then the rule and the reason it matched, one field per line; on a terminal every line wraps to its width with continuation lines under their own column, and piped output is never wrapped. Choosing per file shows the same block. -P / --no-pager prints the plan without the pager. A duplicate's original is shown with ~.
Diffstat (limited to 'internal')
-rw-r--r--internal/engine/facts.go4
-rw-r--r--internal/engine/facts_test.go17
-rw-r--r--internal/tui/tui.go15
-rw-r--r--internal/tui/tui_test.go22
4 files changed, 56 insertions, 2 deletions
diff --git a/internal/engine/facts.go b/internal/engine/facts.go
index 423bfd9..5790e95 100644
--- a/internal/engine/facts.go
+++ b/internal/engine/facts.go
@@ -185,11 +185,11 @@ func (f *facts) Duplicate(dirs []string) (string, bool, error) {
}
// displayOriginal reports orig relative to root when it lies inside root,
-// else as an absolute path.
+// else home-abbreviated (xdg.Abbrev), as every other user-visible path is.
func displayOriginal(orig, root string) string {
rel, err := filepath.Rel(root, orig)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
- return orig
+ return xdg.Abbrev(orig)
}
return filepath.ToSlash(rel)
}
diff --git a/internal/engine/facts_test.go b/internal/engine/facts_test.go
index c511804..7d844a0 100644
--- a/internal/engine/facts_test.go
+++ b/internal/engine/facts_test.go
@@ -75,3 +75,20 @@ func TestContentKeepsRawWithTwoVariants(t *testing.T) {
t.Fatalf("second variant: got %q, %v, want the unfolded original (raw text must still be available)", got, err)
}
}
+
+// TestDisplayOriginalAbbreviatesHome: a duplicate's original is shown
+// root-relative inside the root, and home-abbreviated outside it, the way
+// every other user-visible path is; a path outside $HOME stays absolute.
+func TestDisplayOriginalAbbreviatesHome(t *testing.T) {
+ h := sandbox(t)
+ root := filepath.Join(h, "downloads")
+ for _, tt := range []struct{ orig, want string }{
+ {filepath.Join(root, "x", "a.pdf"), "x/a.pdf"},
+ {filepath.Join(h, "docs", "work", "a.pdf"), "~/docs/work/a.pdf"},
+ {"/srv/share/a.pdf", "/srv/share/a.pdf"},
+ } {
+ if got := displayOriginal(tt.orig, root); got != tt.want {
+ t.Errorf("displayOriginal(%q) = %q, want %q", tt.orig, got, tt.want)
+ }
+ }
+}
diff --git a/internal/tui/tui.go b/internal/tui/tui.go
index c9f1241..e7872c8 100644
--- a/internal/tui/tui.go
+++ b/internal/tui/tui.go
@@ -36,6 +36,21 @@ func Colour(w io.Writer) bool {
return !noColour
}
+// Width is the terminal's column count, 0 when w is not a terminal or the
+// size cannot be read: callers wrap text to it, and treat 0 as "never
+// wrap", which keeps output piped to a file one field per line.
+func Width(w io.Writer) int {
+ f, ok := w.(*os.File)
+ if !ok || !isTerminal(int(f.Fd())) {
+ return 0
+ }
+ cols, _, err := termSize(int(f.Fd()))
+ if err != nil {
+ return 0
+ }
+ return cols
+}
+
// height is the terminal's row count, 0 when it is not a terminal or the
// size cannot be read.
func height(w io.Writer) int {
diff --git a/internal/tui/tui_test.go b/internal/tui/tui_test.go
index a3640ac..614f239 100644
--- a/internal/tui/tui_test.go
+++ b/internal/tui/tui_test.go
@@ -51,6 +51,28 @@ func TestHeight(t *testing.T) {
}
}
+// TestWidth: the terminal's column count, and 0 wherever there is no
+// terminal to wrap to, so a caller treats 0 as "never wrap".
+func TestWidth(t *testing.T) {
+ var buf bytes.Buffer
+ if w := Width(&buf); w != 0 {
+ t.Errorf("Width on a non-terminal writer = %d, want 0", w)
+ }
+
+ oldT, oldS := isTerminal, termSize
+ t.Cleanup(func() { isTerminal, termSize = oldT, oldS })
+ isTerminal = func(fd int) bool { return true }
+ termSize = func(fd int) (int, int, error) { return 132, 24, nil }
+ if w := Width(os.Stdout); w != 132 {
+ t.Errorf("Width = %d, want 132", w)
+ }
+
+ termSize = func(fd int) (int, int, error) { return 0, 0, os.ErrInvalid }
+ if w := Width(os.Stdout); w != 0 {
+ t.Errorf("Width when the size cannot be read = %d, want 0", w)
+ }
+}
+
func TestPageUsesPagerOnlyWhenTaller(t *testing.T) {
dir := t.TempDir()
marker := filepath.Join(dir, "paged")