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

View file

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

View file

@ -10,7 +10,7 @@ import (
"strings"
"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"
)
@ -50,11 +50,11 @@ func Fonts() ([]*Asset, error) {
}
func getCachedFontData() ([]*Asset, error) {
if environment == nil {
if cache == nil {
return nil, errors.New("environment not set")
}
list, OK := environment.Cache().Get(cache.FONTLISTCACHE)
list, OK := cache.Get(cache_.FONTLISTCACHE)
if !OK {
return nil, errors.New("cache not found")
}
@ -69,7 +69,7 @@ func getCachedFontData() ([]*Asset, error) {
}
func setCachedFontData(assets []*Asset) {
if environment == nil {
if cache == nil {
return
}
@ -78,7 +78,7 @@ func setCachedFontData(assets []*Asset) {
return
}
environment.Cache().Set(cache.FONTLISTCACHE, string(data), cache.ONEDAY)
cache.Set(cache_.FONTLISTCACHE, string(data), cache_.ONEDAY)
}
func CascadiaCode() ([]*Asset, error) {

View file

@ -8,6 +8,7 @@ import (
"io"
"path"
stdruntime "runtime"
"slices"
"strings"
"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 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
bytesReader := bytes.NewReader(data)
@ -45,6 +53,7 @@ func InstallZIP(data []byte, user bool) ([]string, error) {
if err != nil {
return families, err
}
defer rc.Close()
data, err := io.ReadAll(rc)
@ -59,13 +68,14 @@ func InstallZIP(data []byte, user bool) ([]string, error) {
if _, found := fonts[fontData.Name]; !found {
fonts[fontData.Name] = fontData
} else {
// 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))
if first != second && second == ".otf" {
fonts[fontData.Name] = fontData
}
continue
}
// respect the user's preference for TTF or OTF
first := strings.ToLower(path.Ext(fonts[fontData.Name].FileName))
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
}
if !contains(families, font.Family) {
if found := contains(families, font.Family); !found {
families = append(families, font.Family)
}
}
@ -84,5 +94,7 @@ func InstallZIP(data []byte, user bool) ([]string, error) {
_, _ = cmd.Run("fc-cache", "-f")
}
slices.Sort(families)
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
```
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 value="homebrew">