feat(font): specify ttf or otf (default) using --ttf flag

resolves #5996
This commit is contained in:
Jan De Dobbeleer 2024-12-12 20:39:47 +01:00 committed by Jan De Dobbeleer
parent 57f18f8ed2
commit c2734a97a5
5 changed files with 51 additions and 29 deletions

View file

@ -11,7 +11,8 @@ import (
) )
var ( var (
// fontCmd can work with fonts ttf bool
fontCmd = &cobra.Command{ fontCmd = &cobra.Command{
Use: "font [install|configure]", Use: "font [install|configure]",
Short: "Manage fonts", Short: "Manage fonts",
@ -46,7 +47,7 @@ This command is used to install fonts and configure the font in your terminal.
terminal.Init(env.Shell()) terminal.Init(env.Shell())
font.Run(fontName, env) font.Run(fontName, env.Cache(), env.Root(), ttf)
return return
case "configure": case "configure":
@ -59,5 +60,6 @@ This command is used to install fonts and configure the font in your terminal.
) )
func init() { func init() {
fontCmd.Flags().BoolVar(&ttf, "ttf", false, "fetch the TTF version of the font")
RootCmd.AddCommand(fontCmd) RootCmd.AddCommand(fontCmd)
} }

View file

@ -10,13 +10,13 @@ import (
"github.com/charmbracelet/bubbles/spinner" "github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/jandedobbeleer/oh-my-posh/src/runtime" cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/terminal" "github.com/jandedobbeleer/oh-my-posh/src/terminal"
) )
var ( var (
program *tea.Program program *tea.Program
environment runtime.Environment cache cache_.Cache
) )
const listHeight = 14 const listHeight = 14
@ -79,6 +79,7 @@ type main struct {
spinner spinner.Model spinner spinner.Model
state state state state
system bool system bool
ttf bool
} }
func (m *main) buildFontList(nerdFonts []*Asset) { func (m *main) buildFontList(nerdFonts []*Asset) {
@ -120,18 +121,18 @@ func downloadFontZip(location string) {
program.Send(zipMsg(zipFile)) program.Send(zipMsg(zipFile))
} }
func installLocalFontZIP(zipFile string, user bool) { func installLocalFontZIP(zipFile string, user, ttf bool) {
data, err := os.ReadFile(zipFile) data, err := os.ReadFile(zipFile)
if err != nil { if err != nil {
program.Send(errMsg(err)) program.Send(errMsg(err))
return return
} }
installFontZIP(data, user) installFontZIP(data, user, ttf)
} }
func installFontZIP(zipFile []byte, user bool) { func installFontZIP(zipFile []byte, user, ttf bool) {
families, err := InstallZIP(zipFile, user) families, err := InstallZIP(zipFile, user, ttf)
if err != nil { if err != nil {
program.Send(errMsg(err)) program.Send(errMsg(err))
return return
@ -159,7 +160,7 @@ func (m *main) Init() tea.Cmd {
defer func() { defer func() {
if isLocalZipFile() { if isLocalZipFile() {
go installLocalFontZIP(m.font, m.system) go installLocalFontZIP(m.font, m.system, m.ttf)
return return
} }
go getFontsList() go getFontsList()
@ -239,7 +240,7 @@ func (m *main) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case zipMsg: case zipMsg:
m.state = installFont m.state = installFont
defer func() { defer func() {
go installFontZIP(msg, m.system) go installFontZIP(msg, m.system, m.ttf)
}() }()
m.spinner.Spinner = spinner.Dot m.spinner.Spinner = spinner.Dot
return m, m.spinner.Tick return m, m.spinner.Tick
@ -290,7 +291,7 @@ func (m *main) View() string {
var builder strings.Builder var builder strings.Builder
builder.WriteString(fmt.Sprintf("Successfully installed %s 🚀\n\n%s", m.font, terminal.StopProgress())) 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") builder.WriteString("The following font families are now available for configuration:\n\n")
for i, family := range m.families { for i, family := range m.families {
builder.WriteString(fmt.Sprintf(" • %s", family)) builder.WriteString(fmt.Sprintf(" • %s", family))
@ -306,13 +307,14 @@ func (m *main) View() string {
return "" return ""
} }
func Run(font string, env runtime.Environment) { func Run(font string, ch cache_.Cache, root, ttf bool) {
main := &main{ main := &main{
font: font, font: font,
system: env.Root(), system: root,
ttf: ttf,
} }
environment = env cache = ch
program = tea.NewProgram(main) program = tea.NewProgram(main)
if _, err := program.Run(); err != nil { if _, err := program.Run(); err != nil {

View file

@ -10,7 +10,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/jandedobbeleer/oh-my-posh/src/cache" cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/http" "github.com/jandedobbeleer/oh-my-posh/src/runtime/http"
) )
@ -50,11 +50,11 @@ func Fonts() ([]*Asset, error) {
} }
func getCachedFontData() ([]*Asset, error) { func getCachedFontData() ([]*Asset, error) {
if environment == nil { if cache == nil {
return nil, errors.New("environment not set") return nil, errors.New("environment not set")
} }
list, OK := environment.Cache().Get(cache.FONTLISTCACHE) list, OK := cache.Get(cache_.FONTLISTCACHE)
if !OK { if !OK {
return nil, errors.New("cache not found") return nil, errors.New("cache not found")
} }
@ -69,7 +69,7 @@ func getCachedFontData() ([]*Asset, error) {
} }
func setCachedFontData(assets []*Asset) { func setCachedFontData(assets []*Asset) {
if environment == nil { if cache == nil {
return return
} }
@ -78,7 +78,7 @@ func setCachedFontData(assets []*Asset) {
return return
} }
environment.Cache().Set(cache.FONTLISTCACHE, string(data), cache.ONEDAY) cache.Set(cache_.FONTLISTCACHE, string(data), cache_.ONEDAY)
} }
func CascadiaCode() ([]*Asset, error) { func CascadiaCode() ([]*Asset, error) {

View file

@ -8,6 +8,7 @@ import (
"io" "io"
"path" "path"
stdruntime "runtime" stdruntime "runtime"
"slices"
"strings" "strings"
"github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime"
@ -20,10 +21,17 @@ func contains[S ~[]E, E comparable](s S, e E) bool {
return true return true
} }
} }
return false return false
} }
func InstallZIP(data []byte, user bool) ([]string, error) { func InstallZIP(data []byte, user, ttf bool) ([]string, error) {
// prefer OTF over TTF; otherwise prefer the first font we find
extension := ".otf"
if ttf {
extension = ".ttf"
}
var families []string var families []string
bytesReader := bytes.NewReader(data) bytesReader := bytes.NewReader(data)
@ -45,6 +53,7 @@ func InstallZIP(data []byte, user bool) ([]string, error) {
if err != nil { if err != nil {
return families, err return families, err
} }
defer rc.Close() defer rc.Close()
data, err := io.ReadAll(rc) data, err := io.ReadAll(rc)
@ -59,13 +68,14 @@ func InstallZIP(data []byte, user bool) ([]string, error) {
if _, found := fonts[fontData.Name]; !found { if _, found := fonts[fontData.Name]; !found {
fonts[fontData.Name] = fontData fonts[fontData.Name] = fontData
} else { continue
// prefer OTF over TTF; otherwise prefer the first font we find }
first := strings.ToLower(path.Ext(fonts[fontData.Name].FileName))
second := strings.ToLower(path.Ext(fontData.FileName)) // respect the user's preference for TTF or OTF
if first != second && second == ".otf" { first := strings.ToLower(path.Ext(fonts[fontData.Name].FileName))
fonts[fontData.Name] = fontData second := strings.ToLower(path.Ext(fontData.FileName))
} if first != second && second == extension {
fonts[fontData.Name] = fontData
} }
} }
@ -74,7 +84,7 @@ func InstallZIP(data []byte, user bool) ([]string, error) {
return families, err return families, err
} }
if !contains(families, font.Family) { if found := contains(families, font.Family); !found {
families = append(families, font.Family) families = append(families, font.Family)
} }
} }
@ -84,5 +94,7 @@ func InstallZIP(data []byte, user bool) ([]string, error) {
_, _ = cmd.Run("fc-cache", "-f") _, _ = cmd.Run("fc-cache", "-f")
} }
slices.Sort(families)
return families, nil return families, nil
} }

View file

@ -46,6 +46,12 @@ This will present a list of Nerd Font libraries, from which you can select `Mes
oh-my-posh font install meslo oh-my-posh font install meslo
``` ```
By default, Oh My Posh installs the `.otf` version of the font. If you prefer the `.ttf` version, you can specify it with the `--ttf` flag:
```bash
oh-my-posh font install meslo --ttf
```
</TabItem> </TabItem>
<TabItem value="homebrew"> <TabItem value="homebrew">