Skip to content

Commit

Permalink
feat(font): cache font list
Browse files Browse the repository at this point in the history
resolves #5300
  • Loading branch information
JanDeDobbeleer committed Jul 22, 2024
1 parent 0b4a9ce commit b356900
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@
"upgrade"
]
},
{
"name": "Font install",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/src",
"args": [
"font",
"install"
]
},
{
"type": "node",
"request": "launch",
Expand Down
7 changes: 7 additions & 0 deletions src/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ var (
TOGGLECACHE = fmt.Sprintf("toggle_cache_%s", pid())
PROMPTCOUNTCACHE = fmt.Sprintf("prompt_count_cache_%s", pid())
ENGINECACHE = fmt.Sprintf("engine_cache_%s", pid())
FONTLISTCACHE = "font_list_cache"
)

const (
ONEDAY = 1440
ONEWEEK = 10080
ONEMONTH = 43200
)

type Entry struct {
Expand Down
3 changes: 3 additions & 0 deletions src/cli/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ You can do the following:
_ = cmd.Help()
return
}

env := &runtime.Terminal{
CmdFlags: &runtime.Flags{},
}

env.Init()
defer env.Close()

switch args[0] {
case "path":
fmt.Print(env.CachePath())
Expand Down
3 changes: 2 additions & 1 deletion src/cli/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ This command is used to install fonts and configure the font in your terminal.
if len(args) > 1 {
fontName = args[1]
}

env := &runtime.Terminal{}
env.Init()
defer env.Close()

terminal.Init(env.Shell())

font.Run(fontName, env.Root())
font.Run(fontName, env)

return
case "configure":
Expand Down
23 changes: 20 additions & 3 deletions src/font/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import (
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/terminal"
)

var program *tea.Program
var (
program *tea.Program
environment runtime.Environment
)

const listHeight = 14

Expand Down Expand Up @@ -114,6 +118,7 @@ func downloadFontZip(location string) {
program.Send(errMsg(err))
return
}

program.Send(zipMsg(zipFile))
}

Expand All @@ -123,6 +128,7 @@ func installLocalFontZIP(zipFile string, user bool) {
program.Send(errMsg(err))
return
}

installFontZIP(data, user)
}

Expand All @@ -132,6 +138,7 @@ func installFontZIP(zipFile []byte, user bool) {
program.Send(errMsg(err))
return
}

program.Send(successMsg(families))
}

Expand Down Expand Up @@ -159,6 +166,7 @@ func (m *main) Init() tea.Cmd {
}
go getFontsList()
}()

s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("170"))
Expand All @@ -167,6 +175,7 @@ func (m *main) Init() tea.Cmd {
if isLocalZipFile() {
m.state = unzipFont
}

return m.spinner.Tick
}

Expand Down Expand Up @@ -245,6 +254,7 @@ func (m *main) View() string {
if m.err != nil {
return textStyle.Render(m.err.Error())
}

switch m.state {
case getFonts:
return textStyle.Render(fmt.Sprintf("%s Downloading font list%s", m.spinner.View(), terminal.StartProgress()))
Expand All @@ -260,25 +270,32 @@ func (m *main) View() string {
return textStyle.Render(fmt.Sprintf("No need to install a new font? That's cool.%s", terminal.StopProgress()))
case done:
var builder strings.Builder

builder.WriteString(fmt.Sprintf("Successfully installed %s 🚀\n\n%s", m.font, terminal.StopProgress()))
builder.WriteString("The following font families are now available for configuration:\n")

for i, family := range m.families {
builder.WriteString(fmt.Sprintf(" • %s", family))

if i < len(m.families)-1 {
builder.WriteString("\n")
}
}

return textStyle.Render(builder.String())
}

return ""
}

func Run(font string, system bool) {
func Run(font string, env runtime.Environment) {
main := &main{
font: font,
system: system,
system: env.Root(),
}

environment = env

program = tea.NewProgram(main)
if _, err := program.Run(); err != nil {
print("Error running program: %v", err)
Expand Down
39 changes: 39 additions & 0 deletions src/font/fonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/http"
)

Expand All @@ -26,6 +27,10 @@ type Asset struct {
func (a Asset) FilterValue() string { return a.Name }

func Fonts() ([]*Asset, error) {
if assets, err := getCachedFontData(); err == nil {
return assets, nil
}

assets, err := fetchFontAssets("ryanoasis/nerd-fonts")
if err != nil {
return nil, err
Expand All @@ -39,9 +44,43 @@ func Fonts() ([]*Asset, error) {
assets = append(assets, cascadiaCode...)
sort.Slice(assets, func(i, j int) bool { return assets[i].Name < assets[j].Name })

setCachedFontData(assets)

return assets, nil
}

func getCachedFontData() ([]*Asset, error) {
if environment == nil {
return nil, errors.New("environment not set")
}

list, OK := environment.Cache().Get(cache.FONTLISTCACHE)
if !OK {
return nil, errors.New("cache not found")
}

assets := make([]*Asset, 0)
err := json.Unmarshal([]byte(list), &assets)
if err != nil {
return nil, err
}

return assets, nil
}

func setCachedFontData(assets []*Asset) {
if environment == nil {
return
}

data, err := json.Marshal(assets)
if err != nil {
return
}

environment.Cache().Set(cache.FONTLISTCACHE, string(data), cache.ONEDAY)
}

func CascadiaCode() ([]*Asset, error) {
return fetchFontAssets("microsoft/cascadia-code")
}
Expand Down

0 comments on commit b356900

Please sign in to comment.