JanDeDobbeleer / oh-my-posh

The most customisable and low-latency cross platform/shell prompt renderer
https://ohmyposh.dev
MIT License
16.7k stars 2.34k forks source link

feat: AWS segment #380

Closed ppittle closed 3 years ago

ppittle commented 3 years ago

Prerequisites

Description

Is it possible to have a local powershell variable display in a segment? (And be performant while doing so?)

I tried with the following segment which I want to write out $localVar:

 "segments": [  {
     "type": "command",
     "style": "plain",
     "powerline_symbol": "",
     "invert_powerline": false,
     "foreground": "#D4E157",
     "background": "#546E7A",
     "leading_diamond": "",
     "trailing_diamond": "",
     "properties": {
         "command": "echo $localVar"
      }
}]

But the command segment doesn't print anything:

image

Context:

For context, I'm basically trying to port the custom Prompt in this old aws blog on powershell for aws and port it to work inside oh-my-posh.

Environment

Steps to Reproduce

  1. Set a powershell variable $localVar to test
  2. Create a theme with a command segmant like above.
  3. Run oh-my-posh

Expected behavior: Prompt reads local variable and displays test

Actual behavior: Test is not printed inside the Prompt

JanDeDobbeleer commented 3 years ago

2 things we can do:

The first option will unblock you, the second one makes the most sense in the long run ;-)

ppittle commented 3 years ago

I might be experimenting with an AWS segment :)

Curious if a custom segment would allow me access to local PowerShell variables though? Inside the segment running env.runShellCommand or env.runCommand doesn't seam to be able to access the value.

Would the custom segment still need to use the Set-PoshContext trick in the Prompt?

JanDeDobbeleer commented 3 years ago

@ppittle you can't access Powershell variables as that runs in a different process. You can call environment variables (AWS cli sets these) and display those or call that AWS cli directly.

JanDeDobbeleer commented 3 years ago

@ppittle can you check the latest version offers what you're looking for?

glucas commented 3 years ago

Trying this out on Win10 / pwsh 7.1.1 and not seeing anything shown for the AWS template. Just to clarify, this template will look for the $env:AWS_PROFILE and similar variables, right?

JanDeDobbeleer commented 3 years ago

@glucas yes, and the config file. How did you add the segment? Because I still have a bug in the docs 😅

glucas commented 3 years ago

How did you add the segment? Because I still have a bug in the docs 😅

Ha, I just copied the example from the doc!

JanDeDobbeleer commented 3 years ago

That should be correct, did you update?

ppittle commented 3 years ago

@JanDeDobbeleer that's fantastic!

image

I tweaked out the template to replace the @ with an orange amazon logo and dropped the prefix character:

{
    "type": "aws",
    "style": "powerline",
    "powerline_symbol": "î‚°",
    "invert_powerline": false,
    "foreground": "#100e23",
    "background": "#ffffff",
    "leading_diamond": "",
    "trailing_diamond": "",
    "properties": {                        
        "template": "{{.Profile}}{{if .Region}}<#F8991D>\uf52c</>{{.Region}}{{end}}"
    }
}
JanDeDobbeleer commented 3 years ago

Feel free to close this when your request is resolved.

glucas commented 3 years ago

That should be correct, did you update?

Interesting, I get nothing. I grabbed the segment from the doc and added it to my theme. I'm setting $env:AWS_PROFILE and $env:AWS_REGION in my pwsh shell. I'll experiment further.

I can aws configure list and see profile and region are picked up from my shell. Created a theme with just path and aws segments, and only get the path. Strange.

JanDeDobbeleer commented 3 years ago

@glucas ok, but did you update oh-my-posh? Otherwise it won't work.

glucas commented 3 years ago

Yes, this is my fault. I've been updating oh-my-posh3 via scoop to newer versions, but now realize I separately have the powershell module installed and that is on an older version. Once again, apologies for the noise!

Works great now - this is very useful, thanks for the great package.

glucas commented 3 years ago

One possible enhancement for the AWS segment would be to only show when the profile is not 'default'. I think I can achieve something similar with if conditions in the template, however.

JanDeDobbeleer commented 3 years ago

@glucas not necessarily, that's an additional feature :-) But very doable indeed. I'll add it.

JanDeDobbeleer commented 3 years ago

@glucas added with the newly available display_default property.

alefranz commented 3 years ago

@glucas yes, and the config file. How did you add the segment? Because I still have a bug in the docs 😅

Hi Jan, I'm trying to configure this, but it only seems to read the value from the env variables (AWS_PROFILE, AWS_REGION) but it doesn't seem to be loading the config file ~/.aws/config.

For example with AWS_PROFILE, the active profile should be the one called "default" from the config file, but it is not displayed. When AWS_REGION is not set, the region selected should be the one of the active profile (either "default" or the one specified in the AWS_PROFILE variable).

Could you clarify how the config is loaded?

I think it would also be nice to get a status if the authentication is valid or expired, and extra infromations like the account connected and so on, essentially to get something like the output of aws sts get-caller-identity CLI command. Should I create a ticket for this feature request?

JanDeDobbeleer commented 3 years ago

@alefranz allow me to explain in the morning 😉 (you can read it here). About using the aws command, that's discouraged as it's really slow (tried it).

alefranz commented 3 years ago

@alefranz allow me to explain in the morning 😉 (you can read it here). About using the aws command, that's discouraged as it's really slow (tried it).

Appreciate the quick reply, but take your time 😂 not urgent at all! I had a look at the code, but that logic is somehow not working for me on pwsh on Windows. I'll get the repo and try to see if I can debug it.

glucas commented 3 years ago

@alefranz For what it's worth, I am running pwsh and when I enable the AWS segment it in fact shows me the default profile name and region, with no AWS variables set: image

In my case I'd prefer to only see this for non-default values but that should be a separate feature request, I'll file it.

JanDeDobbeleer commented 3 years ago

@glucas you can set the property display_default to false to get that result.

github-actions[bot] commented 4 months ago

This issue has been automatically locked since there has not been any recent activity (i.e. last half year) after it was closed. It helps our maintainers focus on the active issues. If you have found a problem that seems similar, please open a discussion first, complete the body with all the details necessary to reproduce, and mention this issue as reference.