Skip to content

Commit a7314d7

Browse files
author
JojiiOfficial
committed
fix progressbar output
1 parent 8a03490 commit a7314d7

File tree

7 files changed

+66
-24
lines changed

7 files changed

+66
-24
lines changed

commands/FileDownload.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

commands/FileUpload.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,11 @@ func (cData *CommandData) UploadFile(uris []string, threads uint, uploadData *Up
102102
}
103103

104104
// Upload file uploads a file
105-
func (cData *CommandData) uploadFile(uploadRequest *libdm.UploadRequest, uploadData *UploadData, uri string) (uploadResponse *libdm.UploadResponse) {
105+
func (cData *CommandData) uploadFile(uploadRequest *libdm.UploadRequest, uploadData *UploadData, uri string) (uploadResponse *libdm.UploadResponse, bar *uiprogress.Bar) {
106106
var r io.Reader
107107
var size int64
108108
var chsum string
109109
var err error
110-
var bar *uiprogress.Bar
111110
var proxy libdm.WriterProxy
112111
done := make(chan string, 1)
113112

@@ -185,7 +184,7 @@ func (cData *CommandData) uploadFile(uploadRequest *libdm.UploadRequest, uploadD
185184
return
186185
}
187186

188-
return uploadResponse
187+
return uploadResponse, bar
189188
}
190189

191190
// Upload uploads a file or a url
@@ -211,6 +210,7 @@ func (cData *CommandData) upload(uploadData *UploadData, uri string) {
211210
}
212211

213212
var uploadResponse *libdm.UploadResponse
213+
var bar *uiprogress.Bar
214214

215215
// Do upload request
216216
if u, err := url.Parse(uri); err == nil && gaw.IsInStringArray(u.Scheme, []string{"http", "https"}) {
@@ -224,7 +224,7 @@ func (cData *CommandData) upload(uploadData *UploadData, uri string) {
224224
printSuccess("uploaded URL: %s", uri)
225225
} else {
226226
// -----> Upload file/stdin <-----
227-
uploadResponse = cData.uploadFile(uploadRequest, uploadData, uri)
227+
uploadResponse, bar = cData.uploadFile(uploadRequest, uploadData, uri)
228228
if uploadResponse == nil {
229229
return
230230
}
@@ -259,5 +259,5 @@ func (cData *CommandData) upload(uploadData *UploadData, uri string) {
259259
}
260260

261261
// Render table with informations
262-
cData.printUploadResponse(uploadResponse, (cData.Quiet || uploadData.TotalFiles > 1))
262+
cData.printUploadResponse(uploadResponse, (cData.Quiet || uploadData.TotalFiles > 1), bar)
263263
}

commands/FileUtils.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,18 @@ func (cData *CommandData) verifyChecksum(localCs, remoteCs string) bool {
8181
return true
8282
}
8383

84-
func (cData *CommandData) printChecksumError(resp *libdm.FileDownloadResponse) {
85-
fmt.Printf("%s checksums don't match!\n", color.YellowString("Warning"))
84+
func (cData *CommandData) getChecksumError(resp *libdm.FileDownloadResponse) string {
85+
var s string
86+
s += fmt.Sprintf("%s checksums don't match!\n", color.YellowString("Warning"))
8687
if !cData.Quiet {
87-
fmt.Printf("Local CS:\t%s\n", resp.LocalChecksum)
88-
fmt.Printf("Rem. CS:\t%s\n", resp.ServerChecksum)
88+
s += fmt.Sprintf("Local CS:\t%s\n", resp.LocalChecksum)
89+
s += fmt.Sprintf("Rem. CS:\t%s\n", resp.ServerChecksum)
8990
}
91+
return s
92+
}
93+
94+
func (cData *CommandData) printChecksumError(resp *libdm.FileDownloadResponse) {
95+
fmt.Println(cData.getChecksumError(resp))
9096
}
9197

9298
func editFile(file string) bool {

commands/cData.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
dmConfig "github.com/DataManager-Go/libdatamanager/config"
88
"github.com/JojiiOfficial/gaw"
99
"github.com/fatih/color"
10+
"github.com/gosuri/uiprogress"
1011
"github.com/sbani/go-humanizer/units"
1112
clitable "gopkg.in/benweidig/cli-table.v2"
1213
)
@@ -113,13 +114,17 @@ func (cData *CommandData) HasKeystoreSupport() bool {
113114

114115
// Print nice output for a file upload
115116
// If total files is > 1 only a summary is shown
116-
func (cData CommandData) printUploadResponse(ur *libdm.UploadResponse, short bool) {
117+
func (cData CommandData) printUploadResponse(ur *libdm.UploadResponse, short bool, bar *uiprogress.Bar) {
117118
if short {
119+
var text string
118120
if len(ur.PublicFilename) > 0 {
119-
fmt.Printf("%s %d; %s %s;\t%s %s\n", color.HiGreenString("ID:"), ur.FileID, color.HiGreenString("Name:"), ur.Filename, color.HiGreenString("Public url:"), cData.Config.GetPreviewURL(ur.PublicFilename))
121+
text = fmt.Sprintf("%s %d; %s %s;\t%s %s", color.HiGreenString("ID:"), ur.FileID, color.HiGreenString("Name:"), ur.Filename, color.HiGreenString("Public url:"), cData.Config.GetPreviewURL(ur.PublicFilename))
120122
} else {
121-
fmt.Printf("%s %d; %s %s\n", color.HiGreenString("ID"), ur.FileID, color.HiGreenString("Name:"), ur.Filename)
123+
text = fmt.Sprintf("%s %d; %s %s", color.HiGreenString("ID"), ur.FileID, color.HiGreenString("Name:"), ur.Filename)
122124
}
125+
126+
printBar(text, bar)
127+
123128
return
124129
}
125130

@@ -138,5 +143,5 @@ func (cData CommandData) printUploadResponse(ur *libdm.UploadResponse, short boo
138143
table.AddRow([]interface{}{color.HiGreenString("Size:"), units.BinarySuffix(float64(ur.FileSize))}...)
139144
table.AddRow([]interface{}{color.HiGreenString("Checksum:"), ur.Checksum}...)
140145

141-
fmt.Println(table)
146+
printBar(table.String(), bar)
142147
}

commands/utils.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ func fmtError(message ...interface{}) {
2828
fmt.Printf("%s %s\n", color.HiRedString("Error:"), fmt.Sprint(message...))
2929
}
3030

31+
func getError(message interface{}, err string) string {
32+
return fmt.Sprintf("%s %s: %s\n", color.HiRedString("Error"), message, err)
33+
}
34+
3135
func printError(message interface{}, err string) {
32-
fmt.Printf("%s %s: %s\n", color.HiRedString("Error"), message, err)
36+
fmt.Println(getError(message, err))
3337
}
3438

3539
func printWarning(message interface{}, err string) {
@@ -42,8 +46,12 @@ func printJSONError(message interface{}) {
4246
json.NewEncoder(os.Stdout).Encode(m)
4347
}
4448

49+
func sPrintSuccess(format string, message ...interface{}) string {
50+
return fmt.Sprintf("%s %s", color.HiGreenString("Successfully"), fmt.Sprintf(format, message...))
51+
}
52+
4553
func printSuccess(format string, message ...interface{}) {
46-
fmt.Printf("%s %s\n", color.HiGreenString("Successfully"), fmt.Sprintf(format, message...))
54+
fmt.Println(sPrintSuccess(format, message...) + "\n")
4755
}
4856

4957
// ProcesStrSliceParam divides args by ,

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ module github.com/DataManager-Go/DataManagerCLI
22

33
go 1.14
44

5-
replace github.com/gosuri/uiprogress v0.0.1 => github.com/JojiiOfficial/uiprogress v1.0.3
5+
replace github.com/gosuri/uiprogress v0.0.1 => github.com/JojiiOfficial/uiprogress v1.0.4
66

77
require (
88
github.com/CovenantSQL/go-sqlite3-encrypt v1.9.0
99
github.com/DataManager-Go/libdatamanager v1.1.15
10-
github.com/DataManager-Go/libdatamanager/config v0.0.0-20200412231115-a82508b229bb
10+
github.com/DataManager-Go/libdatamanager/config v0.0.0-20200413125811-cf32dc70af40
1111
github.com/JojiiOfficial/configService v0.0.0-20200219132202-6e71512e2e28
1212
github.com/JojiiOfficial/gaw v1.2.1
1313
github.com/JojiiOfficial/shred v1.2.1

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ github.com/DataManager-Go/libdatamanager v1.1.15 h1:6ftBaMXLWD1hlxjylidh578W4afV
77
github.com/DataManager-Go/libdatamanager v1.1.15/go.mod h1:s2ywdIZpWEfgxJ7iMtCOdLCNaHVnO8dr7e9cE2NzgL4=
88
github.com/DataManager-Go/libdatamanager/config v0.0.0-20200412231115-a82508b229bb h1:w53VeyM2XITRcIAGf8ArdKy1Q60McFIbuL+Jz4o6HV4=
99
github.com/DataManager-Go/libdatamanager/config v0.0.0-20200412231115-a82508b229bb/go.mod h1:RfoOPe8dQgubuBUmtqEYu1Ny0fT+htZH/ArrEl0dcHQ=
10+
github.com/DataManager-Go/libdatamanager/config v0.0.0-20200413125811-cf32dc70af40 h1:prAklwiqvcLkXnjm9Zl7H3IUW3nnB6U0ebrNuMacjZ4=
11+
github.com/DataManager-Go/libdatamanager/config v0.0.0-20200413125811-cf32dc70af40/go.mod h1:RfoOPe8dQgubuBUmtqEYu1Ny0fT+htZH/ArrEl0dcHQ=
1012
github.com/JojiiOfficial/configService v0.0.0-20200219132202-6e71512e2e28 h1:nYoIExG+Z/gSLS9Jbpu6lnrh+m6e9gTxQfGhanTsExE=
1113
github.com/JojiiOfficial/configService v0.0.0-20200219132202-6e71512e2e28/go.mod h1:j1kHFoYWAbLRPE5nyAAtODwUc0xwd2+ifPZ3uCAgv/g=
1214
github.com/JojiiOfficial/gaw v1.2.0 h1:qIu6xVtCvd4yhAfI0MGGr5CPus0hpY+eFUDUGqc/sYQ=
@@ -17,6 +19,8 @@ github.com/JojiiOfficial/shred v1.2.1 h1:658CFVTqcAkYVg815vW+guYnyJTLOIoS15tMyPT
1719
github.com/JojiiOfficial/shred v1.2.1/go.mod h1:/OAxd6eYOhrXb3KW+2wmDog2BiFlUld8oJEKa+xblxU=
1820
github.com/JojiiOfficial/uiprogress v1.0.3 h1:FeSJKEPlfyUokrA4zi/hl6s3m2DAHdo8TXDiJ44aotU=
1921
github.com/JojiiOfficial/uiprogress v1.0.3/go.mod h1:MsdctZCVERbGDu5lcgHZY12ANTIWkUjk7JyqGSTmQYg=
22+
github.com/JojiiOfficial/uiprogress v1.0.4 h1:2YL/YX6QoB5twDt2sK9cry5V277S5e9dEGzKelgOnGc=
23+
github.com/JojiiOfficial/uiprogress v1.0.4/go.mod h1:MsdctZCVERbGDu5lcgHZY12ANTIWkUjk7JyqGSTmQYg=
2024
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
2125
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
2226
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=

0 commit comments

Comments
 (0)