Repository: dundee/gdu
Pull request #173 (2022-09-13 22:13:16)

feat: crop current item path to 70 chars
#172

coveralls: ## Pull Request Test Coverage Report for [Build 3048726753](https://coveralls.io/builds/52445147)

* **8** of **10**   **(80.0%)**  changed or added relevant lines in **2** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage decreased (**-0.07%**) to **95.84%**

---

|  Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
| :-----|--------------|--------|---: |
| [tui/progress.go](https://coveralls.io/builds/52445147/source?filename=tui%2Fprogress.go#L48) | 7 | 9 | 77.78%
<!-- | **Total:** | **8** | **10** | **80.0%** | -->


|  Totals | [![Coverage Status](https://coveralls.io/builds/52445147/badge)](https://coveralls.io/builds/52445147) |
| :-- | --: |
| Change from base [Build 3047617455](https://coveralls.io/builds/52439477): |  -0.07% |
| Covered Lines: | 2419 |
| Relevant Lines: | 2524 |

---
##### 💛  - [Coveralls](https://coveralls.io)

codecov[bot]: # [Codecov](https://codecov.io/gh/dundee/gdu/pull/173?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Milde) Report
Base: **94.49**% // Head: **94.39**% // Decreases project coverage by **`-0.10%`** :warning:
> Coverage data is based on head [(`f90422a`)](https://codecov.io/gh/dundee/gdu/pull/173?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Milde) compared to base [(`137994d`)](https://codecov.io/gh/dundee/gdu/commit/137994d8c510b512adf02101d011276b1e16f0c7?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Milde).
> Patch coverage: 70.00% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #173      +/-   ##
==========================================
- Coverage   94.49%   94.39%   -0.11%     
==========================================
  Files          27       27              
  Lines        2471     2479       +8     
==========================================
+ Hits         2335     2340       +5     
- Misses         99      101       +2     
- Partials       37       38       +1     
```


| [Impacted Files](https://codecov.io/gh/dundee/gdu/pull/173?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Milde) | Coverage Δ | |
|---|---|---|
| [tui/progress.go](https://codecov.io/gh/dundee/gdu/pull/173/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Milde#diff-dHVpL3Byb2dyZXNzLmdv) | `91.42% <66.66%> (-8.58%)` | :arrow_down: |
| [tui/actions.go](https://codecov.io/gh/dundee/gdu/pull/173/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Milde#diff-dHVpL2FjdGlvbnMuZ28=) | `95.75% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Milde). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Milde)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/dundee/gdu/pull/173?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Milde).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Daniel+Milde).

dundee: ![Snímek obrazovky z 2022-09-14 00-16-58](https://user-images.githubusercontent.com/27106/190019158-d1533e36-00a8-4d96-9cb1-f992a0579993.png)

@edrozenberg @daniejstriata What do you think?

From my point of view is more important to see the beginning of the path, so you know where approximately is the analysis running. Therefore I cut off the end of path if it's too long.
edrozenberg: @dundee it's a bit tricky, seeing the beginning gives you the context, but seeing the end is also important to be able to see that activity is going on (filename keeps changing). One possibility is something like:

/beginning/something/something/......./last path element/filename

So priority would be to show the last path element and filename, then show as much of the beginning as fits.

For ex:

`/Volume/myfiles/joe/documents/.../2022/important.docx`
dundee: Sounds good, I will try to implement it.
daniejstriata: @dundee :-) Now that you're updating that modal... Won't it be interesting to see the running time too?
dundee: ![path shortening](https://user-images.githubusercontent.com/27106/190875684-d3e09a4c-2025-4392-8565-33170a4c4c15.png)

dundee: With time:
![progress with time](https://user-images.githubusercontent.com/27106/190875980-1caa134c-1df3-4c03-b8cd-85e6434c845a.png)

edrozenberg: That looks great. For the future might be nice to have a config option to allow showing multiple lines for current item for users who want to see more of the path, but it's fine as is with the latest changes.

Also would be perfect if modal could be vertically centered, currently it displays at a fixed position of X lines (10?) below the top - on small terminals it displays too low, on taller terminals too high. Thank you.
dundee: Happy to hear that! 

Yes, I have fixed the vertical alignment as well.

Patch:
diff --git a/pkg/path/path.go b/pkg/path/path.go
new file mode 100644
index 0000000..b66baed
--- /dev/null
+++ b/pkg/path/path.go
@@ -0,0 +1,26 @@
+package path
+
+import "strings"
+
+// ShortenPath removes the last but one path components to fit into maxLen
+func ShortenPath(path string, maxLen int) string {
+	if len(path) <= maxLen {
+		return path
+	}
+
+	res := ""
+	parts := strings.SplitAfter(path, "/")
+	curLen := len(parts[len(parts)-1]) // count lenght of last part for start
+
+	for _, part := range parts[:len(parts)-1] {
+		curLen += len(part)
+		if curLen > maxLen {
+			res += ".../"
+			break
+		}
+		res += part
+	}
+
+	res += parts[len(parts)-1]
+	return res
+}
diff --git a/tui/actions.go b/tui/actions.go
index 270ea3d..34ea59a 100644
--- a/tui/actions.go
+++ b/tui/actions.go
@@ -46,7 +46,7 @@ func (ui *UI) AnalyzePath(path string, parentDir fs.Item) error {
 		AddItem(nil, 0, 1, false).
 		AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
 			AddItem(nil, 0, 1, false).
-			AddItem(ui.progress, 12, 1, false).
+			AddItem(ui.progress, 8, 1, false).
 			AddItem(nil, 0, 1, false), 0, 50, false).
 		AddItem(nil, 0, 1, false)
 
diff --git a/tui/progress.go b/tui/progress.go
index 137e68d..e46e15f 100644
--- a/tui/progress.go
+++ b/tui/progress.go
@@ -4,6 +4,7 @@ import (
 	"time"
 
 	"github.com/dundee/gdu/v5/internal/common"
+	"github.com/dundee/gdu/v5/pkg/path"
 )
 
 func (ui *UI) updateProgress() {
@@ -16,6 +17,7 @@ func (ui *UI) updateProgress() {
 	doneChan := ui.Analyzer.GetDone()
 
 	var progress common.CurrentProgress
+	start := time.Now()
 
 	for {
 		select {
@@ -25,15 +27,20 @@ func (ui *UI) updateProgress() {
 		}
 
 		func(itemCount int, totalSize int64, currentItem string) {
+			delta := time.Since(start).Round(time.Second)
+
 			ui.app.QueueUpdateDraw(func() {
 				ui.progress.SetText("Total items: " +
 					color +
 					common.FormatNumber(int64(itemCount)) +
-					"[white:black:-] size: " +
+					"[white:black:-], size: " +
 					color +
 					ui.formatSize(totalSize, false, false) +
+					"[white:black:-], elapsed time: " +
+					color +
+					delta.String() +
 					"[white:black:-]\nCurrent item: [white:black:b]" +
-					currentItem)
+					path.ShortenPath(currentItem, 70))
 			})
 		}(progress.ItemCount, progress.TotalSize, progress.CurrentItemName)
 


Test patch:
diff --git a/pkg/path/path_test.go b/pkg/path/path_test.go
new file mode 100644
index 0000000..9598e28
--- /dev/null
+++ b/pkg/path/path_test.go
@@ -0,0 +1,15 @@
+package path
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestShortenPath(t *testing.T) {
+	assert.Equal(t, "/root", ShortenPath("/root", 10))
+	assert.Equal(t, "/home/.../foo", ShortenPath("/home/dundee/foo", 10))
+	assert.Equal(t, "/home/dundee/foo", ShortenPath("/home/dundee/foo", 50))
+	assert.Equal(t, "/home/dundee/.../bar.txt", ShortenPath("/home/dundee/foo/bar.txt", 20))
+	assert.Equal(t, "/home/.../bar.txt", ShortenPath("/home/dundee/foo/bar.txt", 15))
+}