Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weekend #6009

Merged
merged 5 commits into from
Dec 14, 2024
Merged

weekend #6009

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
1 change: 1 addition & 0 deletions src/config/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ type Block struct {
MaxWidth int `json:"max_width,omitempty" toml:"max_width,omitempty"`
MinWidth int `json:"min_width,omitempty" toml:"min_width,omitempty"`
Newline bool `json:"newline,omitempty" toml:"newline,omitempty"`
Force bool `json:"force,omitempty" toml:"force,omitempty"`
}
5 changes: 4 additions & 1 deletion src/config/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Segment struct {
Enabled bool `json:"-" toml:"-"`
Newline bool `json:"newline,omitempty" toml:"newline,omitempty"`
InvertPowerline bool `json:"invert_powerline,omitempty" toml:"invert_powerline,omitempty"`
restored bool `json:"-" toml:"-"`
}

func (segment *Segment) Name() string {
Expand Down Expand Up @@ -238,11 +239,13 @@ func (segment *Segment) restoreCache() bool {

log.Debug("restored segment from cache: ", segment.Name())

segment.restored = true

return true
}

func (segment *Segment) setCache() {
if !segment.hasCache() {
if segment.restored || !segment.hasCache() {
return
}

Expand Down
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
}
4 changes: 2 additions & 2 deletions src/prompt/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ func (e *Engine) getTitleTemplateText() string {
func (e *Engine) renderBlock(block *config.Block, cancelNewline bool) bool {
text, length := e.writeBlockSegments(block)

// do not print anything when we don't have any text
if length == 0 {
// do not print anything when we don't have any text unless forced
if !block.Force && length == 0 {
return false
}

Expand Down
8 changes: 8 additions & 0 deletions src/segments/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"slices"
"strings"

"github.com/jandedobbeleer/oh-my-posh/src/log"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/path"
)
Expand Down Expand Up @@ -68,6 +69,7 @@ func (p *Python) loadContext() {
p.Venv = prompt
return
}

venvVars := []string{
"VIRTUAL_ENV",
"CONDA_ENV_PATH",
Expand All @@ -88,9 +90,11 @@ func (p *Python) loadContext() {
}

name := path.Base(venv)
log.Debugf("virtual env name: %s", name)
if folderNameFallback && slices.Contains(defaultVenvNames, name) {
venv = strings.TrimSuffix(venv, name)
name = path.Base(venv)
log.Debugf("virtual env name (fallback): %s", name)
}

if p.canUseVenvName(name) {
Expand All @@ -108,12 +112,15 @@ func (p *Python) canUseVenvName(name string) bool {
if p.language.props.GetBool(properties.DisplayDefault, true) {
return true
}

invalidNames := [2]string{"system", "base"}
for _, a := range invalidNames {
if a == name {
log.Debugf("virtual env name %s is invalid", name)
return false
}
}

return true
}

Expand Down Expand Up @@ -193,6 +200,7 @@ func (p *Python) pyvenvCfgPrompt() string {
if len(lineSplit) != 2 {
continue
}

key := strings.TrimSpace(lineSplit[0])
if key == "prompt" {
value := strings.TrimSpace(lineSplit[1])
Expand Down
5 changes: 5 additions & 0 deletions website/docs/configuration/block.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import Config from "@site/src/components/Config.js";
| `leading_diamond` | `string` |
| `trailing_diamond` | `string` |
| `segments` | `array` |
| `force` | `boolean` |

### Type

Expand Down Expand Up @@ -114,5 +115,9 @@ with the same trailing diamond, regardless of which segment is enabled or not.

Array of one or more [segments][segment].

### Force

When set to `true`, the block will always be rendered, even if all segments are empty. Defaults to `false`.

[color-overrides]: /docs/configuration/colors#color-overrides
[segment]: segment.mdx
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
2 changes: 1 addition & 1 deletion website/docs/segments/languages/golang.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Config from "@site/src/components/Config.js";
powerline_symbol: "\uE0B0",
foreground: "#ffffff",
background: "#7FD5EA",
template: " \u202D\uFCD1 {{ .Full }} ",
template: " \ue627 {{ .Full }} ",
}}
/>

Expand Down
Loading