Skip to content

Commit

Permalink
feat(font): specify ttf or otf (default) using --ttf flag
Browse files Browse the repository at this point in the history
resolves #5996
  • Loading branch information
JanDeDobbeleer committed Dec 14, 2024
1 parent 57f18f8 commit c2734a9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
6 changes: 4 additions & 2 deletions src/cli/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

var (
// fontCmd can work with fonts
ttf bool

fontCmd = &cobra.Command{
Use: "font [install|configure]",
Short: "Manage fonts",
Expand Down Expand Up @@ -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":
Expand All @@ -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)
}
28 changes: 15 additions & 13 deletions src/font/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,6 +79,7 @@ type main struct {
spinner spinner.Model
state state
system bool
ttf bool
}

func (m *main) buildFontList(nerdFonts []*Asset) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions src/font/fonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
}
Expand All @@ -69,7 +69,7 @@ func getCachedFontData() ([]*Asset, error) {
}

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

Expand All @@ -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) {
Expand Down
30 changes: 21 additions & 9 deletions src/font/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"path"
stdruntime "runtime"
"slices"
"strings"

"github.com/jandedobbeleer/oh-my-posh/src/runtime"
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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
}
}

Expand All @@ -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)
}
}
Expand All @@ -84,5 +94,7 @@ func InstallZIP(data []byte, user bool) ([]string, error) {
_, _ = cmd.Run("fc-cache", "-f")
}

slices.Sort(families)

return families, nil
}
6 changes: 6 additions & 0 deletions website/docs/installation/fonts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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">

Expand Down

0 comments on commit c2734a9

Please sign in to comment.