feat(init): remote config

This commit is contained in:
Jan De Dobbeleer 2022-02-25 12:51:38 +01:00 committed by Jan De Dobbeleer
parent 917bb8e7c5
commit e6fd50a48f
7 changed files with 49 additions and 29 deletions

View file

@ -1,4 +1,4 @@
A [Homebrew][brew] formula is available for easy installation. When installing Homebrew for Linux, be sure to follow *[Next steps][nextsteps]* instructions to add Homebrew to your PATH and to your bash shell profile script. A [Homebrew][brew] formula is available for easy installation. When installing Homebrew for Linux, be sure to follow *[Next steps][nextsteps]* instructions to add Homebrew to your PATH and to your bash shell profile script.
```bash ```bash
brew tap jandedobbeleer/oh-my-posh brew tap jandedobbeleer/oh-my-posh
@ -30,9 +30,6 @@ brew update && brew upgrade && exec zsh
## Replace your existing prompt ## Replace your existing prompt
The guides below assume you copied the theme called `jandedobbeleer.omp.json` to your user's `$HOME` folder.
When using brew, you can find this one at `$(brew --prefix oh-my-posh)/share/oh-my-posh/themes/jandedobbeleer.omp.json`.
[brew]: https://brew.sh [brew]: https://brew.sh
[nextsteps]: https://docs.brew.sh/Homebrew-on-Linux#install [nextsteps]: https://docs.brew.sh/Homebrew-on-Linux#install
[themes]: https://ohmyposh.dev/docs/themes [themes]: https://ohmyposh.dev/docs/themes

View file

@ -60,9 +60,6 @@ rm ~/.poshthemes/themes.zip
## Replace your existing prompt ## Replace your existing prompt
The guides below assume you copied the theme called `jandedobbeleer.omp.json` to your user's `$HOME` folder.
When you've downloaded the themes, you can find this one at `~/.poshthemes/jandedobbeleer.omp.json`.
</TabItem> </TabItem>
</Tabs> </Tabs>

View file

@ -41,7 +41,7 @@ Import-Module oh-my-posh
::: :::
```powershell ```powershell
oh-my-posh --init --shell pwsh --config ~/jandedobbeleer.omp.json | Invoke-Expression oh-my-posh --init --shell pwsh --config https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/v$(oh-my-posh --version)/themes/jandedobbeleer.omp.json | Invoke-Expression
``` ```
Once added, reload your profile for the changes to take effect. Once added, reload your profile for the changes to take effect.
@ -65,7 +65,7 @@ Use the full path to the config file, not the relative path.
::: :::
```lua title="oh-my-posh.lua" ```lua title="oh-my-posh.lua"
load(io.popen('oh-my-posh --config="C:/Users/jan/jandedobbeleer.omp.json" --init --shell cmd'):read("*a"))() load(io.popen('oh-my-posh --config="https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/jandedobbeleer.omp.json" --init --shell cmd'):read("*a"))()
``` ```
Once added, restart cmd for the changes to take effect. Once added, restart cmd for the changes to take effect.
@ -76,7 +76,7 @@ Once added, restart cmd for the changes to take effect.
Add the following to `~/.zshrc`: Add the following to `~/.zshrc`:
```bash ```bash
eval "$(oh-my-posh --init --shell zsh --config ~/jandedobbeleer.omp.json)" eval "$(oh-my-posh --init --shell zsh --config https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/v$(oh-my-posh --version)/themes/jandedobbeleer.omp.json)"
``` ```
Once added, reload your profile for the changes to take effect. Once added, reload your profile for the changes to take effect.
@ -91,7 +91,7 @@ source ~/.zshrc
Add the following to `~/.bashrc` (could be `~/.profile` or `~/.bash_profile` depending on your environment): Add the following to `~/.bashrc` (could be `~/.profile` or `~/.bash_profile` depending on your environment):
```bash ```bash
eval "$(oh-my-posh --init --shell bash --config ~/jandedobbeleer.omp.json)" eval "$(oh-my-posh --init --shell bash --config https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/v$(oh-my-posh --version)/themes/jandedobbeleer.omp.json)"
``` ```
Once added, reload your profile for the changes to take effect. Once added, reload your profile for the changes to take effect.
@ -116,7 +116,7 @@ It's advised to be on the latest version of fish. Versions below 3.1.2 have issu
Initialize Oh My Posh in `~/.config/fish/config.fish`: Initialize Oh My Posh in `~/.config/fish/config.fish`:
```bash ```bash
oh-my-posh --init --shell fish --config ~/jandedobbeleer.omp.json | source oh-my-posh --init --shell fish --config hhttps://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/v(oh-my-posh --version)/themes/jandedobbeleer.omp.json | source
``` ```
Once added, reload your config for the changes to take effect. Once added, reload your config for the changes to take effect.
@ -133,13 +133,13 @@ Set the prompt and restart nu shell:
### Nu < 0.32.0 ### Nu < 0.32.0
```bash ```bash
config set prompt "= `{{$(oh-my-posh --config ~/jandedobbeleer.omp.json | str collect)}}`" config set prompt "= `{{$(oh-my-posh --config https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/jandedobbeleer.omp.json | str collect)}}`"
``` ```
### Nu >= 0.32.0 ### Nu >= 0.32.0
```bash ```bash
config set prompt "(oh-my-posh --config ~/jandedobbeleer.omp.json | str collect)" config set prompt "(oh-my-posh --config https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/jandedobbeleer.omp.json | str collect)"
``` ```
Restart nu shell for the changes to take effect. Restart nu shell for the changes to take effect.

View file

@ -164,8 +164,6 @@ when setting the prompt using the `--config` flag.
</TabItem> </TabItem>
</Tabs> </Tabs>
The guides below assume you copied the theme called `jandedobbeleer.omp.json` to your user's `$HOME` folder.
:::caution :::caution
When using oh-my-posh inside the WSL, make sure to follow the [linux][linux] installation guide. When using oh-my-posh inside the WSL, make sure to follow the [linux][linux] installation guide.
::: :::

View file

@ -37,7 +37,7 @@ const (
plain = "shell" plain = "shell"
) )
func getExecutablePath(shell string) (string, error) { func getExecutablePath(env environment.Environment) (string, error) {
executable, err := os.Executable() executable, err := os.Executable()
if err != nil { if err != nil {
return "", err return "", err
@ -46,7 +46,7 @@ func getExecutablePath(shell string) (string, error) {
// which uses unix style paths to resolve the executable's location. // which uses unix style paths to resolve the executable's location.
// PowerShell knows how to resolve both, so we can swap this without any issue. // PowerShell knows how to resolve both, so we can swap this without any issue.
executable = strings.ReplaceAll(executable, "\\", "/") executable = strings.ReplaceAll(executable, "\\", "/")
switch shell { switch *env.Args().Shell {
case bash, zsh: case bash, zsh:
executable = strings.ReplaceAll(executable, " ", "\\ ") executable = strings.ReplaceAll(executable, " ", "\\ ")
executable = strings.ReplaceAll(executable, "(", "\\(") executable = strings.ReplaceAll(executable, "(", "\\(")
@ -56,26 +56,29 @@ func getExecutablePath(shell string) (string, error) {
return executable, nil return executable, nil
} }
func InitShell(shell, configFile string) string { func InitShell(env environment.Environment) string {
executable, err := getExecutablePath(shell) executable, err := getExecutablePath(env)
if err != nil { if err != nil {
return noExe return noExe
} }
shell := *env.Args().Shell
switch shell { switch shell {
case pwsh, powershell5: case pwsh, powershell5:
return fmt.Sprintf("(@(&\"%s\" --print-init --shell=%s --config=\"%s\") -join \"`n\") | Invoke-Expression", executable, shell, configFile) return fmt.Sprintf("(@(&\"%s\" --print-init --shell=%s --config=\"%s\") -join \"`n\") | Invoke-Expression", executable, shell, *env.Args().Config)
case zsh, bash, fish, winCMD: case zsh, bash, fish, winCMD:
return PrintShellInit(shell, configFile) return PrintShellInit(env)
default: default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell) return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
} }
} }
func PrintShellInit(shell, configFile string) string { func PrintShellInit(env environment.Environment) string {
executable, err := getExecutablePath(shell) executable, err := getExecutablePath(env)
if err != nil { if err != nil {
return noExe return noExe
} }
shell := *env.Args().Shell
configFile := *env.Args().Config
switch shell { switch shell {
case pwsh, powershell5: case pwsh, powershell5:
return getShellInitScript(executable, configFile, pwshInit) return getShellInitScript(executable, configFile, pwshInit)

View file

@ -5,9 +5,11 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"net/url"
"oh-my-posh/regex" "oh-my-posh/regex"
"os" "os"
"os/exec" "os/exec"
@ -228,10 +230,33 @@ func (env *ShellEnvironment) Init(args *Args) {
} }
} }
func (env *ShellEnvironment) getConfigPath(location string) {
cfg, err := env.HTTPRequest(location, 5000)
if err != nil {
return
}
configPath := filepath.Join(env.CachePath(), "config.omp.json")
out, err := os.Create(configPath)
if err != nil {
return
}
defer out.Close()
_, err = io.Copy(out, bytes.NewReader(cfg))
if err != nil {
return
}
env.args.Config = &configPath
}
func (env *ShellEnvironment) ResolveConfigPath() { func (env *ShellEnvironment) ResolveConfigPath() {
if env.args == nil || env.args.Config == nil || len(*env.args.Config) == 0 { if env.args == nil || env.args.Config == nil || len(*env.args.Config) == 0 {
return return
} }
location, err := url.ParseRequestURI(*env.Args().Config)
if err == nil {
env.getConfigPath(location.String())
return
}
// Cygwin path always needs the full path as we're on Windows but not really. // Cygwin path always needs the full path as we're on Windows but not really.
// Doing filepath actions will convert it to a Windows path and break the init script. // Doing filepath actions will convert it to a Windows path and break the init script.
if env.Platform() == WindowsPlatform && env.Shell() == "bash" { if env.Platform() == WindowsPlatform && env.Shell() == "bash" {
@ -560,11 +585,11 @@ func (env *ShellEnvironment) Shell() string {
return *env.args.Shell return *env.args.Shell
} }
func (env *ShellEnvironment) HTTPRequest(url string, timeout int, requestModifiers ...HTTPRequestModifier) ([]byte, error) { func (env *ShellEnvironment) HTTPRequest(targetURL string, timeout int, requestModifiers ...HTTPRequestModifier) ([]byte, error) {
defer env.trace(time.Now(), "HTTPRequest", url) defer env.trace(time.Now(), "HTTPRequest", targetURL)
ctx, cncl := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(timeout)) ctx, cncl := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(timeout))
defer cncl() defer cncl()
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) request, err := http.NewRequestWithContext(ctx, http.MethodGet, targetURL, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -167,12 +167,12 @@ func main() {
return return
} }
if *args.Init { if *args.Init {
init := engine.InitShell(*args.Shell, *args.Config) init := engine.InitShell(env)
fmt.Print(init) fmt.Print(init)
return return
} }
if *args.PrintInit { if *args.PrintInit {
init := engine.PrintShellInit(*args.Shell, *args.Config) init := engine.PrintShellInit(env)
fmt.Print(init) fmt.Print(init)
return return
} }