feat(font): allow installation of local zip files

This commit is contained in:
Jan De Dobbeleer 2023-03-28 19:26:10 +02:00 committed by Jan De Dobbeleer
parent 7abfa84923
commit 7083cac3f1

View file

@ -73,7 +73,7 @@ type main struct {
spinner spinner.Model spinner spinner.Model
list *list.Model list *list.Model
needsAdmin bool needsAdmin bool
fontname string font string
state state state state
err error err error
} }
@ -115,6 +115,15 @@ func downloadFontZip(location string) {
program.Send(zipMsg(zipFile)) program.Send(zipMsg(zipFile))
} }
func installLocalFontZIP(zipFile string) {
data, err := os.ReadFile(zipFile)
if err != nil {
program.Send(errMsg(err))
return
}
installFontZIP(data)
}
func installFontZIP(zipFile []byte) { func installFontZIP(zipFile []byte) {
err := InstallZIP(zipFile) err := InstallZIP(zipFile)
if err != nil { if err != nil {
@ -129,18 +138,28 @@ func (m *main) Init() tea.Cmd {
m.state = admin m.state = admin
return tea.Quit return tea.Quit
} }
if len(m.fontname) != 0 {
isZipFile := func() bool {
return strings.HasSuffix(m.font, ".zip")
}
if len(m.font) != 0 && !isZipFile() {
m.state = downloadFont m.state = downloadFont
if !strings.HasPrefix(m.fontname, "https") { if !strings.HasPrefix(m.font, "https") {
m.fontname = fmt.Sprintf("https://github.com/ryanoasis/nerd-fonts/releases/latest/download/%s.zip", m.fontname) m.font = fmt.Sprintf("https://github.com/ryanoasis/nerd-fonts/releases/latest/download/%s.zip", m.font)
} }
defer func() { defer func() {
go downloadFontZip(m.fontname) go downloadFontZip(m.font)
}() }()
m.spinner.Spinner = spinner.Globe m.spinner.Spinner = spinner.Globe
return m.spinner.Tick return m.spinner.Tick
} }
defer func() { defer func() {
if isZipFile() {
go installLocalFontZIP(m.font)
return
}
go getFontsList() go getFontsList()
}() }()
s := spinner.New() s := spinner.New()
@ -148,6 +167,9 @@ func (m *main) Init() tea.Cmd {
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("170")) s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("170"))
m.spinner = s m.spinner = s
m.state = getFonts m.state = getFonts
if isZipFile() {
m.state = unzipFont
}
return m.spinner.Tick return m.spinner.Tick
} }
@ -172,7 +194,7 @@ func (m *main) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit return m, tea.Quit
case "enter": case "enter":
if len(m.fontname) != 0 { if len(m.font) != 0 {
return m, nil return m, nil
} }
var font *Asset var font *Asset
@ -182,7 +204,7 @@ func (m *main) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit return m, tea.Quit
} }
m.state = downloadFont m.state = downloadFont
m.fontname = font.Name m.font = font.Name
defer func() { defer func() {
go downloadFontZip(font.URL) go downloadFontZip(font.URL)
}() }()
@ -227,24 +249,24 @@ func (m *main) View() string {
case selectFont: case selectFont:
return "\n" + m.list.View() return "\n" + m.list.View()
case downloadFont: case downloadFont:
return textStyle.Render(fmt.Sprintf("%s Downloading %s", m.spinner.View(), m.fontname)) return textStyle.Render(fmt.Sprintf("%s Downloading %s", m.spinner.View(), m.font))
case admin: case admin:
return textStyle.Render("You need to be admin to install a font on Windows") return textStyle.Render("You need to be admin to install a font on Windows")
case unzipFont: case unzipFont:
return textStyle.Render(fmt.Sprintf("%s Extracting %s", m.spinner.View(), m.fontname)) return textStyle.Render(fmt.Sprintf("%s Extracting %s", m.spinner.View(), m.font))
case installFont: case installFont:
return textStyle.Render(fmt.Sprintf("%s Installing %s", m.spinner.View(), m.fontname)) return textStyle.Render(fmt.Sprintf("%s Installing %s", m.spinner.View(), m.font))
case quit: case quit:
return textStyle.Render("No need to install a new font? That's cool.") return textStyle.Render("No need to install a new font? That's cool.")
case done: case done:
return textStyle.Render(fmt.Sprintf("Successfully installed %s 🚀", m.fontname)) return textStyle.Render(fmt.Sprintf("Successfully installed %s 🚀", m.font))
} }
return "" return ""
} }
func Run(font string, needsAdmin bool) { func Run(font string, needsAdmin bool) {
main := &main{ main := &main{
fontname: font, font: font,
needsAdmin: needsAdmin, needsAdmin: needsAdmin,
} }
program = tea.NewProgram(main) program = tea.NewProgram(main)