@@ -38,7 +38,7 @@ import (
38
38
)
39
39
40
40
const (
41
- prg_VERSION = "1.32 "
41
+ prg_VERSION = "1.34 "
42
42
dft_MAXSHOWNLINES = 15
43
43
dft_MAXEMPTYDIRS = 0
44
44
dft_MAXDENIEDDIRS = 0
@@ -91,6 +91,7 @@ type s_scan struct { // Global variables
91
91
maxPathLen int64 // maximum directory path length
92
92
maxFNameLen int64 // maximum filename length
93
93
currentDevice uint64 // device number of current partition
94
+ refreshDelay int64 // delay between progress bar updates
94
95
maxWidth int // display width (tty columns)
95
96
maxNameLen int // max filename length for depth = 1
96
97
maxShownLines int // number of depth 1 items to display
@@ -105,6 +106,7 @@ type s_scan struct { // Global variables
105
106
foundBoundary bool // found other filesystems
106
107
showMax bool // show deepest and longest paths
107
108
export bool // export result to Ncdu's JSON format
109
+ tty bool // stdout is on a TTY
108
110
humanReadable bool // print sizes in human readable format
109
111
exportPath string // path to exported file
110
112
exportFile * os.File // exported file
@@ -126,6 +128,7 @@ type s_scan struct { // Global variables
126
128
start time.Time // time at process start
127
129
msg chan string
128
130
done chan bool
131
+ sys interface {} // OS functions
129
132
}
130
133
131
134
func detectOS (sc * s_scan ) {
@@ -147,7 +150,7 @@ func detectOS(sc *s_scan) {
147
150
148
151
func getConsoleWidth (sc * s_scan ) {
149
152
sc .maxWidth = 80
150
- w := getTtyWidth ()
153
+ w := getTtyWidth (sc )
151
154
if w >= 72 {
152
155
if w <= 120 {
153
156
sc .maxWidth = w
@@ -158,13 +161,15 @@ func getConsoleWidth(sc *s_scan) {
158
161
sc .maxNameLen = sc .maxWidth - 43 // formatting: stay below N columns
159
162
}
160
163
161
- func newScanStruct (start time.Time ) * s_scan {
164
+ func newScanStruct (start time.Time , sys interface {} ) * s_scan {
162
165
var sc s_scan
163
166
sc .pathSeparator = string (os .PathSeparator )
164
167
sc .inodes = make (map [uint64 ]uint16 , 256 )
165
168
sc .start = start
166
169
sc .msg = make (chan string , 32 )
167
170
sc .done = make (chan bool )
171
+ sc .refreshDelay = cst_PROGRESSBEAT
172
+ sc .sys = sys
168
173
return & sc
169
174
}
170
175
@@ -648,10 +653,6 @@ func changeDir(args []string) (string, error) {
648
653
dir , _ = os .Getwd ()
649
654
return dir , nil
650
655
}
651
- if len (args ) > 1 {
652
- e1 := fmt .Errorf ("Can only scan one top directory: got %d" , len (args ))
653
- return dir , e1
654
- }
655
656
err := os .Chdir (args [0 ])
656
657
if err != nil {
657
658
e2 := fmt .Errorf ("Cannot change directory to %s\n %v" , args [0 ], err )
@@ -668,7 +669,7 @@ func changeDir(args []string) (string, error) {
668
669
func usage (sc * s_scan ) []string {
669
670
flag .Usage = func () {
670
671
showTitle ()
671
- fmt .Println (" Copyright (c) 2019 Joseph Paul <joseph.paul1@gmx.com>" )
672
+ fmt .Println (" Copyright (c) 2020 Joseph Paul <joseph.paul1@gmx.com>" )
672
673
fmt .Println (" https://bitbucket.org/josephpaul0/tdu" )
673
674
fmt .Println ()
674
675
fmt .Printf (" Usage: %s [options] [directory]\n " , os .Args [0 ])
@@ -739,6 +740,17 @@ func usage(sc *s_scan) []string {
739
740
sc .export = true
740
741
sc .exportPath = * ex
741
742
}
743
+ if len (flag .Args ()) > 1 {
744
+ fmt .Println ()
745
+ fmt .Printf ("[ERROR] can only scan one top directory: got %d" , len (args ))
746
+ fmt .Println ()
747
+ fmt .Println ()
748
+ fmt .Println ("[TIP] Use double-quotes around the directory path if it contains spaces." )
749
+ fmt .Println ("[TIP] Example: tdu.exe \" C:\\ Program Files\" " )
750
+ fmt .Println ()
751
+ flag .Usage ()
752
+ os .Exit (2 )
753
+ }
742
754
return args
743
755
}
744
756
@@ -751,8 +763,9 @@ func showProgress(sc *s_scan) {
751
763
var i int
752
764
var m string
753
765
space := strings .Repeat (" " , 42 )
766
+ fmt .Println ()
754
767
for {
755
- time .Sleep (cst_PROGRESSBEAT * time .Millisecond )
768
+ time .Sleep (time . Duration ( sc . refreshDelay ) * time .Millisecond )
756
769
select {
757
770
case m = <- sc .msg :
758
771
fmt .Print (space )
@@ -762,8 +775,7 @@ func showProgress(sc *s_scan) {
762
775
}
763
776
default :
764
777
i ++
765
- n := sc .nErrors + sc .nItems
766
- fmt .Printf (" [.... scanning... %6d ....]\r " , n )
778
+ printProgress (sc )
767
779
}
768
780
if m == cst_ENDPROGRESS {
769
781
break
@@ -774,8 +786,10 @@ func showProgress(sc *s_scan) {
774
786
}
775
787
776
788
func endProgress (sc * s_scan ) {
777
- sc .msg <- cst_ENDPROGRESS
778
- <- sc .done
789
+ if sc .tty {
790
+ sc .msg <- cst_ENDPROGRESS
791
+ <- sc .done
792
+ }
779
793
}
780
794
781
795
func push (sc * s_scan , msg string ) {
@@ -790,7 +804,7 @@ func showTitle() {
790
804
fmt .Println ()
791
805
}
792
806
793
- func relocate (args []string ) string {
807
+ func relocate (sc * s_scan , args []string ) string {
794
808
d , err := changeDir (flag .Args ())
795
809
if err != nil {
796
810
showTitle ()
@@ -801,37 +815,49 @@ func relocate(args []string) string {
801
815
return d
802
816
}
803
817
818
+ func showResults (sc * s_scan , fi []file , total * file ) {
819
+ show (sc , fi , total ) // Step 3
820
+ showmax (sc , total ) // step 4
821
+ showempty (sc )
822
+ showdenied (sc )
823
+ showerrors (sc )
824
+ showstreams (sc )
825
+ showdevices (sc )
826
+ }
827
+
828
+ func startProgress (sc * s_scan ) {
829
+ if sc .tty {
830
+ go showProgress (sc )
831
+ } else {
832
+ fmt .Fprintln (os .Stderr , " Please wait..." )
833
+ }
834
+ }
835
+
804
836
/* Basically, the process has got several steps:
805
837
* 1. change directory to given path
806
838
* 2. scan all files recursively, collecting 'stat' data
807
839
* 3. sort results and output a list of biggest items at depth 1.
808
840
* 4. show the largest files at any depth.
809
841
*/
810
842
func main () {
811
- osInit ()
843
+ _ , sys := osInit ()
812
844
start := time .Now ()
813
- sc := newScanStruct (start )
845
+ sc := newScanStruct (start , sys )
814
846
args := usage (sc )
815
- d := relocate (args ) // step 1
847
+ d := relocate (sc , args ) // step 1
816
848
detectOS (sc )
817
- clearTty ( )
849
+ initTty ( sc )
818
850
getConsoleWidth (sc )
819
851
showTitle ()
820
852
fmt .Printf (" OS: %s %s," , sc .os , runtime .GOARCH )
821
853
fmt .Printf (" scanning [%s]...\n " , d )
822
854
ncduInit (sc )
823
- go showProgress (sc )
855
+ startProgress (sc )
824
856
var fi []file
825
857
t , _ := scan (sc , & fi , "." , 1 ) // Step 2
826
858
endProgress (sc )
827
- show (sc , fi , t ) // Step 3
828
- showmax (sc , t ) // step 4
829
- showempty (sc )
830
- showdenied (sc )
831
- showerrors (sc )
832
- showstreams (sc )
833
- showdevices (sc )
859
+ showResults (sc , fi , t )
834
860
ncduEnd (sc )
835
861
showElapsed (sc )
836
- osEnd ()
862
+ osEnd (sys )
837
863
}
0 commit comments