Skip to content

Commit

Permalink
fix(print): redesign sprint, add options
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed Feb 12, 2022
1 parent a76a52e commit 42a0519
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Package format `deb` `rpm` `apk` will be downloaded directly
```
- Setups for executable: `ghdl` moves executable to specified location and add execute permissions to the file.
- Auto filtering: multiple assets in one release will be filtered by OS or ARCH.
- Auto filtering: multiple assets in one release will be filtered by OS or ARCH. This feature can be disabled using `-F` flag.
- Interactive TUI: when auto filtering is failed or returned multiple options, you can select assets in a interactive way, with vim key bindings support.
- Release tags: `ghdl` downloads latest release by default, other or old tagged releases can be downloaded by specifying release tag: `username/repo#tagname`
- Inspect download status with real-time progress bar.
Expand Down
4 changes: 1 addition & 3 deletions dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ func (dl GHReleaseDl) ExtractBinary() error {
}
case "":
decompressedBinary = openfile
case ".deb":
case ".rpm":
case ".apk":
case ".deb", ".rpm", ".apk":
fileName := dl.BinaryName + fileExt
if err := os.Rename(tmpfileName, fileName); err != nil {
panic(err)
Expand Down
19 changes: 10 additions & 9 deletions ghdl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,36 @@ ghdl handles archived or compressed file as well`,
repo, tag := parseArg(args[0])
ghRelease := ghdl.GHRelease{RepoPath: repo, TagName: tag}
ghReleaseDl, err := ghRelease.GetGHReleases(filterOff)

if err != nil {
h.Print(fmt.Sprintf("get gh releases failed: %s", err), h.PrintModeErr)
h.Println(fmt.Sprintf("get gh releases failed: %s", err), h.PrintModeErr)
os.Exit(1)
}

if binaryNameFlag != "" {
ghReleaseDl.BinaryName = binaryNameFlag
}
h.Print(fmt.Sprintf("start downloading [%s]", filepath.Base(ghReleaseDl.Url)), h.PrintModeInfo)
h.Println(fmt.Sprintf("start downloading %s", h.Sprint(filepath.Base(ghReleaseDl.Url), h.SprintOptions{PromptOff: true, PrintMode: h.PrintModeSuccess})), h.PrintModeInfo)
if err := ghReleaseDl.DlTo(pathFlag); err != nil {
h.Print(fmt.Sprintf("download failed: %s", err), h.PrintModeErr)
h.Println(fmt.Sprintf("download failed: %s", err), h.PrintModeErr)
os.Exit(1)
}
if err := ghReleaseDl.ExtractBinary(); err != nil {
switch err {
case ghdl.ErrNeedInstall:
h.Print(fmt.Sprintf("%s. You can install %s with the appropriate commands", err, ghReleaseDl.BinaryName), h.PrintModeInfo)
h.Println(fmt.Sprintf("%s. You can install it with the appropriate commands", err), h.PrintModeInfo)
os.Exit(0)
case ghdl.ErrNoBin:
h.Print(fmt.Sprintf("%s. Try to specify binary name flag", err), h.PrintModeInfo)
h.Println(fmt.Sprintf("%s. Try to specify binary name flag", err), h.PrintModeInfo)
os.Exit(0)
default:
h.Print(fmt.Sprintf("extract failed: %s", err), h.PrintModeErr)
h.Println(fmt.Sprintf("extract failed: %s", err), h.PrintModeErr)
os.Exit(1)
}
}
h.Print(fmt.Sprintf("saved executable to %s", ghReleaseDl.BinaryName), h.PrintModeSuccess)
h.Println(fmt.Sprintf("saved executable to %s", ghReleaseDl.BinaryName), h.PrintModeSuccess)
if err := os.Chmod(ghReleaseDl.BinaryName, 0777); err != nil {
h.Print(fmt.Sprintf("chmod failed: %s", err), h.PrintModeErr)
h.Println(fmt.Sprintf("chmod failed: %s", err), h.PrintModeErr)
}
},
}
Expand All @@ -78,7 +79,7 @@ func main() {
func init() {
rootCmd.PersistentFlags().StringP("name", "n", "", "specify binary file name to enhance filtering and extracting accuracy")
rootCmd.PersistentFlags().StringP("path", "p", ".", "save binary to `path` and add execute permission to it")
rootCmd.PersistentFlags().BoolP("filter-off", "-F", false, "turn off auto-filtering feature")
rootCmd.PersistentFlags().BoolP("filter-off", "F", false, "turn off auto-filtering feature")
}

// parse user/repo[#tagname] arg
Expand Down
2 changes: 1 addition & 1 deletion helper/pg/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Progress(starter func(updater func(float64)), humanize string) {
}

if err := tea.NewProgram(&state).Start(); err != nil {
h.Print(fmt.Sprintln("Oh no!", err), h.PrintModeErr)
h.Println(fmt.Sprintln("Oh no!", err), h.PrintModeErr)
os.Exit(1)
}
}
28 changes: 20 additions & 8 deletions helper/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,24 @@ const (
PrintModeErr = 2
)

func Sprint(str string, printMode int) string {
printWidth := 80
var newStyle = lipgloss.NewStyle().Width(printWidth)
prompt := lipgloss.NewStyle().Foreground(lipgloss.Color("13")).Render("→ ")
var sPrint string = prompt
switch printMode {
type SprintOptions struct {
PrintMode int
PrintWidth int
PromptOff bool
}

func Sprint(str string, options SprintOptions) string {
newStyle := lipgloss.NewStyle()
if options.PrintWidth > 0 {
newStyle = newStyle.Width(options.PrintWidth)
}
sPrint := ""
if !options.PromptOff {
prompt := lipgloss.NewStyle().Foreground(lipgloss.Color("13")).Render("→ ")
sPrint = prompt
}

switch options.PrintMode {
case PrintModeInfo:
sPrint += newStyle.Copy().Foreground(lipgloss.Color("146")).Render(str)
case PrintModeSuccess:
Expand All @@ -29,7 +41,7 @@ func Sprint(str string, printMode int) string {
return sPrint
}

func Print(str string, printMode int) {
sPrint := Sprint(str, printMode)
func Println(str string, printMode int) {
sPrint := Sprint(str, SprintOptions{PrintMode: printMode})
fmt.Println(sPrint)
}
4 changes: 2 additions & 2 deletions helper/sl/sl.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (m model) View() string {
paddingS := lipgloss.NewStyle().PaddingLeft(2).Width(printWidth)
colorS := paddingS.Copy().
Foreground(blue).BorderLeft(true).BorderForeground(blue)
s := h.Sprint("there is more than one option after filtering, please select it manually", h.PrintModeInfo) + "\n"
s := h.Sprint("multiple options after filtering, please select asset manually", h.SprintOptions{PrintWidth: 80}) + "\n"
if m.selected == -1 {
for i, choice := range m.choices {
if m.cursor == i {
Expand All @@ -73,7 +73,7 @@ func Select(choices *[]string) int {
state := initialModel(choices)
p := tea.NewProgram(&state)
if err := p.Start(); err != nil {
h.Print(fmt.Sprintf("Alas, there's been an error: %v", err), h.PrintModeErr)
h.Println(fmt.Sprintf("Alas, there's been an error: %v", err), h.PrintModeErr)
os.Exit(1)
}
return state.selected
Expand Down

0 comments on commit 42a0519

Please sign in to comment.