fork/gdu

git clone https://git.julia.blue/fork/gdu

ShivamB25feat: stop scanning and keep partial results (#606)99a7f2a

subvolume
2.1 KiB81 linesraw
1package tui
2
3import (
4	"fmt"
5	"os"
6	"time"
7
8	"github.com/dundee/gdu/v5/internal/common"
9)
10
11func (ui *UI) updateProgress(analyzer common.Analyzer, doneChan common.SignalGroup) {
12	color := "[white:black:b]"
13	if ui.UseColors {
14		color = "[red:black:b]"
15	}
16
17	deviceSize := ui.currentDeviceSize
18	showBar := ui.showDiskProgressBar
19
20	start := time.Now()
21	ticker := time.NewTicker(100 * time.Millisecond)
22	defer ticker.Stop()
23
24	for {
25		select {
26		case <-doneChan:
27			if deviceSize > 0 && showBar {
28				clearTerminalProgress()
29				ui.currentDeviceSize = 0
30			}
31			ui.app.QueueUpdateDraw(func() {
32				ui.progress.SetTitle(" Finalizing... ")
33				ui.progress.SetText("Calculating disk usage...")
34			})
35			return
36		case <-ticker.C:
37		}
38
39		progress := analyzer.GetProgress()
40
41		func(itemCount int64, totalUsage int64) {
42			delta := time.Since(start).Round(time.Second)
43
44			if deviceSize > 0 && showBar {
45				percent := int(totalUsage * 100 / deviceSize)
46				writeTerminalProgress(percent)
47				if ui.progressBar != nil {
48					ui.progressBar.SetProgress(percent)
49				}
50			}
51
52			ui.app.QueueUpdateDraw(func() {
53				ui.progress.SetText("Total items: " +
54					color +
55					common.FormatNumber(int64(itemCount)) +
56					"[white:black:-], size: " +
57					color +
58					ui.formatSize(totalUsage, false, false) +
59					"[white:black:-], elapsed time: " +
60					color +
61					delta.String() +
62					"[white:black:-]\n\nPress Tab to preview results found so far\n" +
63					"Press Ctrl+C to stop scanning and keep results")
64			})
65		}(progress.ItemCount, progress.TotalUsage)
66	}
67}
68
69// writeTerminalProgress emits an OSC 9;4 sequence to update the terminal
70// tab/taskbar progress indicator. percent must be in the range [0, 100].
71// This sequence is supported by Windows Terminal, ConEmu, and compatible
72// terminals.  Writing to stderr ensures it reaches the terminal even when
73// the TUI has taken over stdout/stdin via tcell.
74func writeTerminalProgress(percent int) {
75	fmt.Fprintf(os.Stderr, "\x1b]9;4;1;%d\x1b\\", percent)
76}
77
78// clearTerminalProgress removes the terminal tab/taskbar progress indicator.
79func clearTerminalProgress() {
80	fmt.Fprintf(os.Stderr, "\x1b]9;4;0;0\x1b\\")
81}