Skip to content

Commit 42a0519

Browse files
committed
fix(print): redesign sprint, add options
1 parent a76a52e commit 42a0519

File tree

6 files changed

+35
-24
lines changed

6 files changed

+35
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Package format `deb` `rpm` `apk` will be downloaded directly
2121
```
2222
- Setups for executable: `ghdl` moves executable to specified location and add execute permissions to the file.
23-
- Auto filtering: multiple assets in one release will be filtered by OS or ARCH.
23+
- Auto filtering: multiple assets in one release will be filtered by OS or ARCH. This feature can be disabled using `-F` flag.
2424
- Interactive TUI: when auto filtering is failed or returned multiple options, you can select assets in a interactive way, with vim key bindings support.
2525
- Release tags: `ghdl` downloads latest release by default, other or old tagged releases can be downloaded by specifying release tag: `username/repo#tagname`
2626
- Inspect download status with real-time progress bar.

dl.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ func (dl GHReleaseDl) ExtractBinary() error {
103103
}
104104
case "":
105105
decompressedBinary = openfile
106-
case ".deb":
107-
case ".rpm":
108-
case ".apk":
106+
case ".deb", ".rpm", ".apk":
109107
fileName := dl.BinaryName + fileExt
110108
if err := os.Rename(tmpfileName, fileName); err != nil {
111109
panic(err)

ghdl/main.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,36 @@ ghdl handles archived or compressed file as well`,
3535
repo, tag := parseArg(args[0])
3636
ghRelease := ghdl.GHRelease{RepoPath: repo, TagName: tag}
3737
ghReleaseDl, err := ghRelease.GetGHReleases(filterOff)
38+
3839
if err != nil {
39-
h.Print(fmt.Sprintf("get gh releases failed: %s", err), h.PrintModeErr)
40+
h.Println(fmt.Sprintf("get gh releases failed: %s", err), h.PrintModeErr)
4041
os.Exit(1)
4142
}
4243

4344
if binaryNameFlag != "" {
4445
ghReleaseDl.BinaryName = binaryNameFlag
4546
}
46-
h.Print(fmt.Sprintf("start downloading [%s]", filepath.Base(ghReleaseDl.Url)), h.PrintModeInfo)
47+
h.Println(fmt.Sprintf("start downloading %s", h.Sprint(filepath.Base(ghReleaseDl.Url), h.SprintOptions{PromptOff: true, PrintMode: h.PrintModeSuccess})), h.PrintModeInfo)
4748
if err := ghReleaseDl.DlTo(pathFlag); err != nil {
48-
h.Print(fmt.Sprintf("download failed: %s", err), h.PrintModeErr)
49+
h.Println(fmt.Sprintf("download failed: %s", err), h.PrintModeErr)
4950
os.Exit(1)
5051
}
5152
if err := ghReleaseDl.ExtractBinary(); err != nil {
5253
switch err {
5354
case ghdl.ErrNeedInstall:
54-
h.Print(fmt.Sprintf("%s. You can install %s with the appropriate commands", err, ghReleaseDl.BinaryName), h.PrintModeInfo)
55+
h.Println(fmt.Sprintf("%s. You can install it with the appropriate commands", err), h.PrintModeInfo)
5556
os.Exit(0)
5657
case ghdl.ErrNoBin:
57-
h.Print(fmt.Sprintf("%s. Try to specify binary name flag", err), h.PrintModeInfo)
58+
h.Println(fmt.Sprintf("%s. Try to specify binary name flag", err), h.PrintModeInfo)
5859
os.Exit(0)
5960
default:
60-
h.Print(fmt.Sprintf("extract failed: %s", err), h.PrintModeErr)
61+
h.Println(fmt.Sprintf("extract failed: %s", err), h.PrintModeErr)
6162
os.Exit(1)
6263
}
6364
}
64-
h.Print(fmt.Sprintf("saved executable to %s", ghReleaseDl.BinaryName), h.PrintModeSuccess)
65+
h.Println(fmt.Sprintf("saved executable to %s", ghReleaseDl.BinaryName), h.PrintModeSuccess)
6566
if err := os.Chmod(ghReleaseDl.BinaryName, 0777); err != nil {
66-
h.Print(fmt.Sprintf("chmod failed: %s", err), h.PrintModeErr)
67+
h.Println(fmt.Sprintf("chmod failed: %s", err), h.PrintModeErr)
6768
}
6869
},
6970
}
@@ -78,7 +79,7 @@ func main() {
7879
func init() {
7980
rootCmd.PersistentFlags().StringP("name", "n", "", "specify binary file name to enhance filtering and extracting accuracy")
8081
rootCmd.PersistentFlags().StringP("path", "p", ".", "save binary to `path` and add execute permission to it")
81-
rootCmd.PersistentFlags().BoolP("filter-off", "-F", false, "turn off auto-filtering feature")
82+
rootCmd.PersistentFlags().BoolP("filter-off", "F", false, "turn off auto-filtering feature")
8283
}
8384

8485
// parse user/repo[#tagname] arg

helper/pg/pg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func Progress(starter func(updater func(float64)), humanize string) {
5959
}
6060

6161
if err := tea.NewProgram(&state).Start(); err != nil {
62-
h.Print(fmt.Sprintln("Oh no!", err), h.PrintModeErr)
62+
h.Println(fmt.Sprintln("Oh no!", err), h.PrintModeErr)
6363
os.Exit(1)
6464
}
6565
}

helper/print.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ const (
1212
PrintModeErr = 2
1313
)
1414

15-
func Sprint(str string, printMode int) string {
16-
printWidth := 80
17-
var newStyle = lipgloss.NewStyle().Width(printWidth)
18-
prompt := lipgloss.NewStyle().Foreground(lipgloss.Color("13")).Render("→ ")
19-
var sPrint string = prompt
20-
switch printMode {
15+
type SprintOptions struct {
16+
PrintMode int
17+
PrintWidth int
18+
PromptOff bool
19+
}
20+
21+
func Sprint(str string, options SprintOptions) string {
22+
newStyle := lipgloss.NewStyle()
23+
if options.PrintWidth > 0 {
24+
newStyle = newStyle.Width(options.PrintWidth)
25+
}
26+
sPrint := ""
27+
if !options.PromptOff {
28+
prompt := lipgloss.NewStyle().Foreground(lipgloss.Color("13")).Render("→ ")
29+
sPrint = prompt
30+
}
31+
32+
switch options.PrintMode {
2133
case PrintModeInfo:
2234
sPrint += newStyle.Copy().Foreground(lipgloss.Color("146")).Render(str)
2335
case PrintModeSuccess:
@@ -29,7 +41,7 @@ func Sprint(str string, printMode int) string {
2941
return sPrint
3042
}
3143

32-
func Print(str string, printMode int) {
33-
sPrint := Sprint(str, printMode)
44+
func Println(str string, printMode int) {
45+
sPrint := Sprint(str, SprintOptions{PrintMode: printMode})
3446
fmt.Println(sPrint)
3547
}

helper/sl/sl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (m model) View() string {
5353
paddingS := lipgloss.NewStyle().PaddingLeft(2).Width(printWidth)
5454
colorS := paddingS.Copy().
5555
Foreground(blue).BorderLeft(true).BorderForeground(blue)
56-
s := h.Sprint("there is more than one option after filtering, please select it manually", h.PrintModeInfo) + "\n"
56+
s := h.Sprint("multiple options after filtering, please select asset manually", h.SprintOptions{PrintWidth: 80}) + "\n"
5757
if m.selected == -1 {
5858
for i, choice := range m.choices {
5959
if m.cursor == i {
@@ -73,7 +73,7 @@ func Select(choices *[]string) int {
7373
state := initialModel(choices)
7474
p := tea.NewProgram(&state)
7575
if err := p.Start(); err != nil {
76-
h.Print(fmt.Sprintf("Alas, there's been an error: %v", err), h.PrintModeErr)
76+
h.Println(fmt.Sprintf("Alas, there's been an error: %v", err), h.PrintModeErr)
7777
os.Exit(1)
7878
}
7979
return state.selected

0 commit comments

Comments
 (0)