Skip to content

Commit

Permalink
feat: make print sytle better
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed Feb 11, 2022
1 parent fda638c commit 568df3f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions ghdl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ ghdl handles archived or compressed file as well`,
h.Print(fmt.Sprintf("%s. You can install %s with the appropriate commands", err, ghReleaseDl.BinaryName), h.PrintModeInfo)
os.Exit(0)
case ghdl.NoBinError:
h.Print(fmt.Sprintf("%s. Try specify binary name flag", err), h.PrintModeInfo)
h.Print(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)
os.Exit(1)
}
}
h.Print(fmt.Sprintf("saved binary executable to %s", ghReleaseDl.BinaryName), h.PrintModeSuccess)
h.Print(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)
}
Expand Down
9 changes: 1 addition & 8 deletions helper/pg/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"os"
"strings"

h "github.com/beetcb/ghdl/helper"
"github.com/charmbracelet/bubbles/progress"
Expand Down Expand Up @@ -33,11 +32,6 @@ func (pbr *ProgressBytesReader) Read(b []byte) (n int, err error) {
return
}

const (
padding = 4
maxWidth = 80
)

func (m model) Init() tea.Cmd {
return m.init
}
Expand All @@ -50,8 +44,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (e model) View() string {
pad := strings.Repeat(" ", padding)
return "\n" + pad + e.progress.ViewAs(e.percent) + fmt.Sprintf(" of %s", e.humanize) + "\n\n"
return "\n " + e.progress.ViewAs(e.percent) + fmt.Sprintf(" of %s", e.humanize) + "\n\n"
}

func Progress(starter func(updater func(float64)), humanize string) {
Expand Down
22 changes: 15 additions & 7 deletions helper/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@ import (
"github.com/charmbracelet/lipgloss"
)

const maxWidth = 80

const (
PrintModeInfo = 0
PrintModeSuccess = 1
PrintModeErr = 2
)

func Print(str string, printMode int) {
var PaddingLeft = lipgloss.NewStyle().PaddingLeft(2).MaxWidth(maxWidth)
func Sprint(str string, printMode int) string {
printWidth := 60
var newStyle = lipgloss.NewStyle().Width(printWidth)
prompt := lipgloss.NewStyle().Foreground(lipgloss.Color("13")).Render("→ ")
var sPrint string = prompt
switch printMode {
case PrintModeInfo:
fmt.Println(PaddingLeft.Foreground(lipgloss.Color("11")).Render(str))
sPrint += newStyle.Copy().Foreground(lipgloss.Color("146")).Render(str)
case PrintModeSuccess:
fmt.Println(PaddingLeft.Foreground(lipgloss.Color("14")).Render(str))
sPrint += newStyle.Copy().Foreground(lipgloss.Color("6")).Render(str)
case PrintModeErr:
fmt.Println(PaddingLeft.Foreground(lipgloss.Color("202")).Render(str))
sPrint += newStyle.Copy().Foreground(lipgloss.Color("9")).Render(str)
}

return sPrint
}

func Print(str string, printMode int) {
sPrint := Sprint(str, printMode)
fmt.Println(sPrint)
}
10 changes: 5 additions & 5 deletions helper/sl/sl.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m model) View() string {
blue := lipgloss.Color("14")
yellow := lipgloss.Color("11")
paddingS := lipgloss.NewStyle().PaddingLeft(2).MaxWidth(maxWidth)
blue, printWidth := lipgloss.Color("14"), 60
paddingS := lipgloss.NewStyle().PaddingLeft(2).Width(printWidth)
colorS := paddingS.Copy().
Foreground(blue).BorderLeft(true).BorderForeground(blue)
s := "\n" + h.Sprint("there is more than one option after filtering, please select it manually", h.PrintModeInfo) + "\n"
if m.selected == -1 {
s := "\n" + paddingS.Copy().Foreground(yellow).Render("gh-dl can't figure out which release to download\nplease select it manully") + "\n\n"
s += "\n"
for i, choice := range m.choices {
if m.cursor == i {
s += colorS.Render(choice) + "\n"
Expand All @@ -68,7 +68,7 @@ func (m model) View() string {
// Send the UI for rendering
return s + "\n"
} else {
s := "\n" + paddingS.Copy().Foreground(yellow).Render(fmt.Sprintf("start downloading %s", lipgloss.NewStyle().Foreground(blue).Render(m.choices[m.selected]))) + "\n"
s += h.Sprint(fmt.Sprintf("start downloading %s", lipgloss.NewStyle().Foreground(blue).Render(m.choices[m.selected])), h.PrintModeInfo) + "\n"
return s
}
}
Expand Down

0 comments on commit 568df3f

Please sign in to comment.