From 9c741cab1a5e42fea4c52188ebeeb786b6a080e7 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 12 Dec 2024 13:19:23 +0100 Subject: [PATCH 1/5] refactor(python): add log statements --- src/segments/python.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/segments/python.go b/src/segments/python.go index 68015c059f1a..9b45a061603f 100644 --- a/src/segments/python.go +++ b/src/segments/python.go @@ -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" ) @@ -68,6 +69,7 @@ func (p *Python) loadContext() { p.Venv = prompt return } + venvVars := []string{ "VIRTUAL_ENV", "CONDA_ENV_PATH", @@ -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) { @@ -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 } @@ -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]) From 63b339a29c738dde566f1bd513e7e87dbf1a9771 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 12 Dec 2024 13:32:52 +0100 Subject: [PATCH 2/5] feat(block): allow force rendering resolves #6002 --- src/config/block.go | 1 + src/prompt/engine.go | 4 ++-- website/docs/configuration/block.mdx | 5 +++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/config/block.go b/src/config/block.go index bb9fda95e036..ff9dfa094356 100644 --- a/src/config/block.go +++ b/src/config/block.go @@ -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"` } diff --git a/src/prompt/engine.go b/src/prompt/engine.go index 2ed895aba2fe..160a5d02a78c 100644 --- a/src/prompt/engine.go +++ b/src/prompt/engine.go @@ -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 } diff --git a/website/docs/configuration/block.mdx b/website/docs/configuration/block.mdx index 59338a3d7c25..9c488a2c6745 100644 --- a/website/docs/configuration/block.mdx +++ b/website/docs/configuration/block.mdx @@ -32,6 +32,7 @@ import Config from "@site/src/components/Config.js"; | `leading_diamond` | `string` | | `trailing_diamond` | `string` | | `segments` | `array` | +| `force` | `boolean` | ### Type @@ -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 From 01e03d5a81a72f6367d6812e96578b5c8fdef266 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 12 Dec 2024 20:39:47 +0100 Subject: [PATCH 3/5] feat(font): specify ttf or otf (default) using --ttf flag resolves #5996 --- src/cli/font.go | 6 ++++-- src/font/cli.go | 28 ++++++++++++++------------- src/font/fonts.go | 10 +++++----- src/font/install.go | 30 ++++++++++++++++++++--------- website/docs/installation/fonts.mdx | 6 ++++++ 5 files changed, 51 insertions(+), 29 deletions(-) diff --git a/src/cli/font.go b/src/cli/font.go index cf1070ed18f4..a1212445eaa2 100644 --- a/src/cli/font.go +++ b/src/cli/font.go @@ -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) } diff --git a/src/font/cli.go b/src/font/cli.go index 424caf0cc6c8..1a495b1dab7c 100644 --- a/src/font/cli.go +++ b/src/font/cli.go @@ -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 { diff --git a/src/font/fonts.go b/src/font/fonts.go index 12c53bdf385f..db022f3cb78c 100644 --- a/src/font/fonts.go +++ b/src/font/fonts.go @@ -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) { diff --git a/src/font/install.go b/src/font/install.go index 326d2b2e5a37..c4feff8bf966 100644 --- a/src/font/install.go +++ b/src/font/install.go @@ -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 } diff --git a/website/docs/installation/fonts.mdx b/website/docs/installation/fonts.mdx index fd6b54fe9c31..3023de004013 100644 --- a/website/docs/installation/fonts.mdx +++ b/website/docs/installation/fonts.mdx @@ -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 +``` + From 1970188fd94d4e6e6f8edbf88d477da73f61df36 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sat, 14 Dec 2024 14:15:58 +0100 Subject: [PATCH 4/5] fix(segment): do not set cache when restored resolves #6011 --- src/config/segment.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/config/segment.go b/src/config/segment.go index f1b14751c027..c86f62627da1 100644 --- a/src/config/segment.go +++ b/src/config/segment.go @@ -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 { @@ -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 } From d1a2657e9451f06c501797cc7bef86a5e8a0cae9 Mon Sep 17 00:00:00 2001 From: Pavlo Golub Date: Thu, 12 Dec 2024 16:16:08 +0100 Subject: [PATCH 5/5] docs(go): update symbol resolves #4625 --- website/docs/segments/languages/golang.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/segments/languages/golang.mdx b/website/docs/segments/languages/golang.mdx index 8638b9c16829..89d9c0de865e 100644 --- a/website/docs/segments/languages/golang.mdx +++ b/website/docs/segments/languages/golang.mdx @@ -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 }} ", }} />