Skip to content

Commit 90dfdbb

Browse files
committed
added progressbar
1 parent 35b8a6d commit 90dfdbb

File tree

6 files changed

+67
-37
lines changed

6 files changed

+67
-37
lines changed

cmd/arduino_lib.go

+24-17
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ func executeDownloadCommand(cmd *cobra.Command, args []string) error {
130130
libraryOK := make([]string, 0, len(args))
131131
libraryFails := make(map[string]string, len(args))
132132

133-
for _, libraryName := range args {
133+
for i, libraryName := range args {
134134
library := status.Libraries[libraryName]
135135
if library != nil {
136136
//found
137-
_, err = libraries.DownloadAndCache(library)
137+
_, err = libraries.DownloadAndCache(library, i+1, len(args))
138138
if err != nil {
139139
libraryFails[libraryName] = err.Error()
140140
} else {
@@ -149,10 +149,10 @@ func executeDownloadCommand(cmd *cobra.Command, args []string) error {
149149
prettyPrintDownload(libraryOK, libraryFails)
150150
} else {
151151
for _, library := range libraryOK {
152-
fmt.Printf("%s - Downloaded\n", library)
152+
fmt.Printf("%-10s -Downloaded\n", library)
153153
}
154154
for library, failure := range libraryFails {
155-
fmt.Printf("%s - Error : %s\n", library, failure)
155+
fmt.Printf("%-10s -Error : %s\n", library, failure)
156156
}
157157
}
158158

@@ -184,11 +184,11 @@ func executeInstallCommand(cmd *cobra.Command, args []string) error {
184184
libraryOK := make([]string, 0, len(args))
185185
libraryFails := make(map[string]string, len(args))
186186

187-
for _, libraryName := range args {
187+
for i, libraryName := range args {
188188
library := status.Libraries[libraryName]
189189
if library != nil {
190190
//found
191-
err = libraries.DownloadAndInstall(library)
191+
err = libraries.DownloadAndInstall(library, i+1, len(args))
192192
if err != nil {
193193
libraryFails[libraryName] = err.Error()
194194
} else {
@@ -202,11 +202,13 @@ func executeInstallCommand(cmd *cobra.Command, args []string) error {
202202
if GlobalFlags.Verbose > 0 {
203203
prettyPrintInstall(libraryOK, libraryFails)
204204
} else {
205-
for _, library := range libraryOK {
206-
fmt.Printf("%s - Installed\n", library)
205+
for i, library := range libraryOK {
206+
fmt.Printf("%-10s - Installed (%d/%d)\n", library, i+1, len(libraryOK))
207207
}
208+
i := 1
208209
for library, failure := range libraryFails {
209-
fmt.Printf("%s - Error : %s\n", library, failure)
210+
i++
211+
fmt.Printf("%-10s - Error : %-10s (%d/%d)\n", library, failure, i, len(libraryFails))
210212
}
211213
}
212214

@@ -298,15 +300,20 @@ func executeUninstallCommand(cmd *cobra.Command, args []string) error {
298300
}
299301
}
300302

301-
if len(libraryFails) > 0 {
302-
fmt.Println("The following libraries were succesfully uninstalled:")
303-
fmt.Println(strings.Join(libraryOK, " "))
304-
fmt.Println("However, the uninstall process failed on the following libraries:")
305-
fmt.Println(strings.Join(libraryFails, " "))
306-
} else {
307-
fmt.Println("All libraries successfully uninstalled")
303+
if GlobalFlags.Verbose > 0 {
304+
if len(libraryFails) > 0 {
305+
fmt.Println("The following libraries were succesfully uninstalled:")
306+
fmt.Println(strings.Join(libraryOK, " "))
307+
fmt.Println("However, the uninstall process failed on the following libraries:")
308+
fmt.Println(strings.Join(libraryFails, " "))
309+
} else {
310+
fmt.Println("All libraries successfully uninstalled")
311+
}
312+
} else if len(libraryFails) > 0 {
313+
for _, failed := range libraryFails {
314+
fmt.Printf("%-10s - Failed\n", failed)
315+
}
308316
}
309-
310317
return nil
311318
}
312319

cmd/arduino_lib_list.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
package cmd
3131

3232
import (
33-
"encoding/json"
3433
"fmt"
3534
"io/ioutil"
3635
"path/filepath"
@@ -39,8 +38,8 @@ import (
3938
"os"
4039

4140
"github.com/bcmi-labs/arduino-cli/common"
42-
"github.com/bcmi-labs/arduino-cli/libraries"
4341
"github.com/spf13/cobra"
42+
"github.com/zieckey/goini"
4443
)
4544

4645
// arduinoLibListCmd represents the list libraries command.
@@ -116,20 +115,32 @@ func executeListCommand(command *cobra.Command, args []string) {
116115
continue
117116
}
118117

119-
var jsonContent libraries.IndexRelease
120-
err = json.Unmarshal(content, &jsonContent)
118+
ini := goini.New()
119+
err = ini.Parse(content, "\n", "=")
121120
if err != nil {
122-
//I use library name
121+
fmt.Println(err)
122+
}
123+
Name, ok := ini.Get("name")
124+
if !ok {
125+
fileName := file.Name()
126+
//replacing underscore in foldernames with spaces.
127+
fileName = strings.Replace(fileName, "_", " ", -1)
128+
fileName = strings.Replace(fileName, "-", " v. ", -1)
129+
//I use folder name
130+
libs = append(libs, fileName)
131+
continue
132+
}
133+
Version, ok := ini.Get("version")
134+
if !ok {
123135
fileName := file.Name()
124136
//replacing underscore in foldernames with spaces.
125137
fileName = strings.Replace(fileName, "_", " ", -1)
126138
fileName = strings.Replace(fileName, "-", " v. ", -1)
127139
//I use folder name
128140
libs = append(libs, fileName)
129141
continue
130-
} else {
131-
libs = append(libs, fmt.Sprintf("%s v. %s", jsonContent.Name, jsonContent.Version))
132142
}
143+
libs = append(libs, fmt.Sprintf("%-10s v. %s", Name, Version))
133144
}
134145
}
135146
}

cmd/pretty_print.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ import (
3636
"github.com/bcmi-labs/arduino-cli/libraries"
3737
)
3838

39+
// prettyPrintStatus pretty prints libraries from index status.
3940
func prettyPrintStatus(status *libraries.StatusContext) {
40-
//Pretty print libraries from index.
4141
for _, name := range status.Names() {
4242
if GlobalFlags.Verbose > 0 {
4343
lib := status.Libraries[name]
@@ -86,7 +86,7 @@ func prettyPrintInstall(libraryOK []string, libraryFails map[string]string) {
8686
}
8787
fmt.Println("he installation process failed on the following libraries:")
8888
for library, failure := range libraryFails {
89-
fmt.Printf("%s - %s\n", library, failure)
89+
fmt.Printf("%-10s -%s\n", library, failure)
9090
}
9191
} else {
9292
fmt.Println("All libraries successfully installed")
@@ -105,7 +105,7 @@ func prettyPrintDownload(libraryOK []string, libraryFails map[string]string) {
105105
}
106106
fmt.Println("he download of the following libraries failed:")
107107
for library, failure := range libraryFails {
108-
fmt.Printf("%s - %s\n", library, failure)
108+
fmt.Printf("%-10s -%s\n", library, failure)
109109
}
110110
} else {
111111
fmt.Println("All libraries successfully downloaded")

common/common_helper.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ import (
6565
"net/http"
6666
"os"
6767
"path/filepath"
68+
69+
"github.com/mitchellh/ioprogress"
6870
)
6971

7072
// GetFolder gets a folder on a path, and creates it if not found.
@@ -136,8 +138,8 @@ func TruncateDir(dir string) error {
136138
return nil
137139
}
138140

139-
// DownloadPackage downloads a package from arduino repository.
140-
func DownloadPackage(URL string) ([]byte, error) {
141+
// DownloadPackage downloads a package from arduino repository, applying a label for the progress bar.
142+
func DownloadPackage(URL string, downloadLabel string, progressFile int, totalFiles int) ([]byte, error) {
141143
client := http.DefaultClient
142144

143145
request, err := http.NewRequest("GET", URL, nil)
@@ -146,6 +148,7 @@ func DownloadPackage(URL string) ([]byte, error) {
146148
}
147149

148150
response, err := client.Do(request)
151+
149152
if err != nil {
150153
return nil, fmt.Errorf("Cannot fetch library. Response creation error")
151154
} else if response.StatusCode != 200 {
@@ -155,7 +158,15 @@ func DownloadPackage(URL string) ([]byte, error) {
155158
defer response.Body.Close()
156159

157160
// Download completed, now move the archive to temp location and unpack it.
158-
body, err := ioutil.ReadAll(response.Body)
161+
rd := &ioprogress.Reader{
162+
Reader: response.Body,
163+
Size: response.ContentLength,
164+
DrawFunc: ioprogress.DrawTerminalf(os.Stdout, func(progress int64, size int64) string {
165+
return fmt.Sprintf("%s ... %s -%.2f %% (%d/%d)", downloadLabel, ioprogress.DrawTextFormatBytes(progress, size), float64(progress)/float64(size)*100, progressFile, totalFiles)
166+
}),
167+
}
168+
169+
body, err := ioutil.ReadAll(rd)
159170
if err != nil {
160171
return nil, fmt.Errorf("Cannot read response body")
161172
}

libraries/download.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ package libraries
3131

3232
import (
3333
"archive/zip"
34+
"fmt"
3435
"io/ioutil"
3536
"net/http"
3637
"path/filepath"
@@ -43,8 +44,8 @@ const (
4344
)
4445

4546
// DownloadAndCache downloads a library without installing it
46-
func DownloadAndCache(library *Library) (*zip.Reader, error) {
47-
zipContent, err := downloadLatest(library)
47+
func DownloadAndCache(library *Library, progressFiles int, totalFiles int) (*zip.Reader, error) {
48+
zipContent, err := downloadLatest(library, progressFiles, totalFiles)
4849
if err != nil {
4950
return nil, err
5051
}
@@ -58,8 +59,8 @@ func DownloadAndCache(library *Library) (*zip.Reader, error) {
5859
}
5960

6061
// DownloadLatest downloads Latest version of a library.
61-
func downloadLatest(library *Library) ([]byte, error) {
62-
return common.DownloadPackage(library.Latest.URL)
62+
func downloadLatest(library *Library, progressFiles int, totalFiles int) ([]byte, error) {
63+
return common.DownloadPackage(library.Latest.URL, fmt.Sprintf("library %s", library.Name), progressFiles, totalFiles)
6364
}
6465

6566
// DownloadLibrariesFile downloads the lib file from arduino repository.

libraries/install_uninstall.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var install = common.Unzip
4646
var Uninstall = os.RemoveAll
4747

4848
// DownloadAndInstall downloads a library and installs it to its specified location.
49-
func DownloadAndInstall(library *Library) error {
49+
func DownloadAndInstall(library *Library, progressFiles int, totalFiles int) error {
5050
libFolder, err := common.GetDefaultLibFolder()
5151
if err != nil {
5252
return fmt.Errorf("Cannot get Lib destination directory")
@@ -59,12 +59,12 @@ func DownloadAndInstall(library *Library) error {
5959

6060
_, err = os.Stat(cacheFilePath)
6161
if os.IsNotExist(err) {
62-
zipArchive, err = DownloadAndCache(library)
62+
zipArchive, err = DownloadAndCache(library, progressFiles, totalFiles)
6363
if err != nil {
6464
return err
6565
}
6666
} else {
67-
fmt.Printf("%s library found in cache downloads ... using cached zip archive\n", library.Name)
67+
fmt.Printf("%-10s - Cached (%d/%d)\n", library.Name, progressFiles, totalFiles)
6868
content, err := ioutil.ReadFile(cacheFilePath)
6969
if err != nil {
7070
return err

0 commit comments

Comments
 (0)