fix(font): provide feedback when installing

relates to #2338
This commit is contained in:
Jan De Dobbeleer 2022-06-10 10:31:44 +02:00 committed by Jan De Dobbeleer
parent 265790659c
commit 0dbe5f2aa4
3 changed files with 19 additions and 22 deletions

View file

@ -3,7 +3,6 @@ package cli
import ( import (
"fmt" "fmt"
"oh-my-posh/font" "oh-my-posh/font"
"os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -17,7 +16,7 @@ var (
This command is used to install fonts and configure the font in your terminal. This command is used to install fonts and configure the font in your terminal.
- install: oh-my-posh font install https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/3270.zip`, - install: oh-my-posh font install 3270`,
ValidArgs: []string{ ValidArgs: []string{
"install", "install",
"configure", "configure",
@ -29,15 +28,11 @@ This command is used to install fonts and configure the font in your terminal.
} }
switch args[0] { switch args[0] {
case "install": case "install":
if len(args) == 1 { var fontName string
font.Run() if len(args) > 1 {
return fontName = args[1]
}
err := font.Install(args[1])
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
} }
font.Run(fontName)
return return
case "configure": case "configure":
fmt.Println("not implemented") fmt.Println("not implemented")

View file

@ -122,6 +122,15 @@ func installFontZIP(zipFile []byte) {
} }
func (m *main) Init() tea.Cmd { func (m *main) Init() tea.Cmd {
if len(m.fontname) != 0 {
m.state = downloadFont
url := fmt.Sprintf("https://github.com/ryanoasis/nerd-fonts/releases/latest/download/%s.zip", m.fontname)
defer func() {
go downloadFontZip(url)
}()
m.spinner.Spinner = spinner.Globe
return m.spinner.Tick
}
defer func() { defer func() {
go getFontsList() go getFontsList()
}() }()
@ -219,8 +228,11 @@ func (m *main) View() string {
return "" return ""
} }
func Run() { func Run(font string) {
program = tea.NewProgram(&main{}) main := &main{
fontname: font,
}
program = tea.NewProgram(main)
if err := program.Start(); err != nil { if err := program.Start(); err != nil {
print("Error running program: %v", err) print("Error running program: %v", err)
os.Exit(1) os.Exit(1)

View file

@ -5,22 +5,12 @@ package font
import ( import (
"archive/zip" "archive/zip"
"bytes" "bytes"
"fmt"
"io" "io"
"path" "path"
"runtime" "runtime"
"strings" "strings"
) )
func Install(font string) error {
location := fmt.Sprintf("https://github.com/ryanoasis/nerd-fonts/releases/latest/download/%s.zip", font)
zipFile, err := Download(location)
if err != nil {
return err
}
return InstallZIP(zipFile)
}
func InstallZIP(data []byte) (err error) { func InstallZIP(data []byte) (err error) {
bytesReader := bytes.NewReader(data) bytesReader := bytes.NewReader(data)