@@ -78,7 +78,7 @@ func (cData *CommandData) ViewFile(data *DownloadData) {
7878 defer ShredderFile (tmpFile , - 1 )
7979
8080 // Write file
81- if err = writeFile (cData , resp , tmpFile , nil ); err != nil {
81+ if err = writeFile (cData , resp , tmpFile , nil , data . bar ); err != nil {
8282 return
8383 }
8484
@@ -137,7 +137,7 @@ func (cData *CommandData) DownloadFile(data *DownloadData) {
137137 c := make (chan string , 1 )
138138
139139 go func () {
140- err = writeFile (cData , resp , outFile , cancel )
140+ err = writeFile (cData , resp , outFile , cancel , data . bar )
141141 if err != nil {
142142 // Delete file on error. On checksum error only delete if --verify was passed
143143 if err != libdm .ErrChecksumNotMatch || cData .VerifyFile {
@@ -158,20 +158,39 @@ func (cData *CommandData) DownloadFile(data *DownloadData) {
158158 // await shredder
159159 <- c
160160 }, func (s string ) {
161- printSuccess ("saved '%s'" , outFile )
161+ text := sPrintSuccess ("saved '%s'" , outFile )
162+
163+ // If a progressbar was used, set its text
164+ // instead of printing a new line
165+ if data .bar != nil {
166+ data .bar .SetText (text )
167+ } else {
168+ fmt .Println (text + "\n " )
169+ }
170+
162171 })
163172}
164173
165- func writeFile (cData * CommandData , resp * libdm.FileDownloadResponse , file string , cancel chan bool ) error {
174+ func writeFile (cData * CommandData , resp * libdm.FileDownloadResponse , file string , cancel chan bool , bar * uiprogress. Bar ) error {
166175 // Save file to tempFile
167176 err := resp .WriteToFile (file , 0600 , cancel )
168177 if err != nil {
169178 if err == libdm .ErrChecksumNotMatch {
170- cData .printChecksumError (resp )
171- } else if err != libdm . ErrCancelled {
172- printError ( "downloading file" , err .Error ())
179+ printBar ( cData .getChecksumError (resp ), bar )
180+ } else {
181+ printBar ( getError ( "downloading file" , err .Error ()), bar )
173182 }
174183 }
175184
176185 return err
177186}
187+
188+ // If bar is set, use it to print text
189+ // Otherwise print a new line
190+ func printBar (text string , bar * uiprogress.Bar ) {
191+ if bar == nil {
192+ fmt .Println (text )
193+ } else {
194+ bar .SetText (text )
195+ }
196+ }
0 commit comments