fix(font): do not fail on non-admin for Windows

This commit is contained in:
Jan De Dobbeleer 2022-08-29 08:21:49 +02:00 committed by Jan De Dobbeleer
parent 16d278ace3
commit c93d637128
2 changed files with 21 additions and 8 deletions

View file

@ -2,6 +2,7 @@ package cli
import (
"fmt"
"oh-my-posh/environment"
"oh-my-posh/font"
"github.com/spf13/cobra"
@ -32,7 +33,10 @@ This command is used to install fonts and configure the font in your terminal.
if len(args) > 1 {
fontName = args[1]
}
font.Run(fontName)
env := &environment.ShellEnvironment{}
env.Init()
needsAdmin := env.GOOS() == environment.WINDOWS && !env.Root()
font.Run(fontName, needsAdmin)
return
case "configure":
fmt.Println("not implemented")

View file

@ -64,16 +64,18 @@ const (
downloadFont
unzipFont
installFont
admin
quit
done
)
type main struct {
spinner spinner.Model
list *list.Model
fontname string
state state
err error
spinner spinner.Model
list *list.Model
needsAdmin bool
fontname string
state state
err error
}
func (m *main) buildFontList(nerdFonts []*Asset) {
@ -123,6 +125,10 @@ func installFontZIP(zipFile []byte) {
}
func (m *main) Init() tea.Cmd {
if m.needsAdmin {
m.state = admin
return tea.Quit
}
if len(m.fontname) != 0 {
m.state = downloadFont
if !strings.HasPrefix(m.fontname, "https") {
@ -222,6 +228,8 @@ func (m *main) View() string {
return "\n" + m.list.View()
case downloadFont:
return textStyle.Render(fmt.Sprintf("%s Downloading %s", m.spinner.View(), m.fontname))
case admin:
return textStyle.Render("You need to be admin to install a font on Windows")
case unzipFont:
return textStyle.Render(fmt.Sprintf("%s Extracting %s", m.spinner.View(), m.fontname))
case installFont:
@ -234,9 +242,10 @@ func (m *main) View() string {
return ""
}
func Run(font string) {
func Run(font string, needsAdmin bool) {
main := &main{
fontname: font,
fontname: font,
needsAdmin: needsAdmin,
}
program = tea.NewProgram(main)
if err := program.Start(); err != nil {