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

Conditional Newline #607

Closed
2 tasks done
sdsalyer opened this issue Apr 1, 2021 · 17 comments
Closed
2 tasks done

Conditional Newline #607

sdsalyer opened this issue Apr 1, 2021 · 17 comments
Labels
🚀 feat New feature

Comments

@sdsalyer
Copy link

sdsalyer commented Apr 1, 2021

Prerequisites

  • I have read and understand the CONTRIBUTING guide
  • I looked for duplicate issues before submitting this one

Description

Is it possible to set a conditional newline? For example, if I'm in a directory under Git version control, display the git / poshgit info on the next line. Otherwise, don't display the extra (blank) line.

Environment

  • Oh my Posh version: 3.130.1
  • Theme: (customized)
  • Operating System: Windows
  • Shell: PowerShell
  • Terminal: Windows Terminal

Steps to Reproduce

n/a

Expected behavior: [What you expected to happen]

 14:23:23 █C:    Bitbucket█
█develop ≢█
PS❯ cd ..
 14:23:30 █C:    PowerShell█
PS❯ pwd

Actual behavior: [What actually happened]

 14:23:23 █C:    Bitbucket█
█develop ≢█
PS❯ cd ..
 14:23:30 █C:    PowerShell█

PS❯ pwd

Theme file:

{
    "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
    "blocks": [
        {
            "type": "prompt",
            "alignment": "left",
            "segments": [
                {
                    "type": "root",
                    "style": "plain",
                    "background": "#ac0101",
                    "foreground": "#fce93c",
                    "properties": {
                        "root_icon": "[ADMIN]"
                    }
                },
                {
                    "type": "time",
                    "style": "plain",
                    "properties": {
                        "time_format": "15:04:05"
                    }
                },
                {
                    "type": "path",
                    "style": "powerline",
                    "background": "#91ddff",
                    "foreground": "#100e23",
                    "powerline_symbol": "",
                    "properties": {
                        "folder_icon": "",
                        "folder_separator_icon": "",
                        "style": "agnoster_short"
                    }
                }
            ]
        },
        {
            "type": "newline"
        },
        {
            "type": "prompt",
            "alignment": "left",
            "segments": [
                {
                    "type": "git",
                    "style": "powerline",
                    "powerline_symbol": "\uE0B0",
                    "foreground": "#193549",
                    "background": "#95ffa4"
                }
            ]
        },
        {
            "type": "newline"
        },
        {
            "type": "prompt",
            "alignment": "left",
            "segments": [
                {
                    "foreground": "#007ACC",
                    "properties": {
                        "prefix": "",
                        "text": "PS❯"
                    },
                    "style": "plain",
                    "type": "text"
                }
            ]
        }
    ],
    "final_space": false
}
@sdsalyer
Copy link
Author

sdsalyer commented Apr 1, 2021

Side note: I'm also unsure why there is a space before the time segment if I'm not in elevated prompt?

image

@lnu
Copy link
Contributor

lnu commented Apr 1, 2021

You can set prefix and postfix to empty if you don't want a space before and after(by default).

@JanDeDobbeleer JanDeDobbeleer added the 🚀 feat New feature label Apr 1, 2021
@sdsalyer
Copy link
Author

sdsalyer commented Apr 1, 2021

You can set prefix and postfix to empty if you don't want a space before and after(by default).

That did the trick ... I didn't realize the default was a space. Thanks!

@sdsalyer
Copy link
Author

sdsalyer commented Apr 1, 2021

I should note, I attempted to add a command segment that checked (using posh-git) for version control and conditionally returned a new line character, but this seems to be ignored as whitespace, so only worked if another character was included.

@JanDeDobbeleer
Copy link
Owner

JanDeDobbeleer commented Apr 1, 2021

@sdsalyer in theory, you could add the \n as the prefix to the poshgit segment which would then only render when you're in a git repo. But that's top of mind here.

@sdsalyer
Copy link
Author

sdsalyer commented Apr 2, 2021

\n as the prefix to the poshgit segment which would then only render when you're in a git repo.

I believe technically this worked, but it rendered the start of the colores powerline block on the first line? I will check again at my work laptop tomorrow.

@JanDeDobbeleer
Copy link
Owner

Yes, you are right. We can fix that with a diamond segment though by adding the \n to the leading_diamond and playing with color overrides.

@lnu
Copy link
Contributor

lnu commented Apr 2, 2021

Yes, you are right. We can fix that with a diamond segment though by adding the \n to the leading_diamond and playing with color overrides.

You're right and it could work but more generally, there is no possibility to show/hide a segment/block conditionally based on the visibility of another segment/block. This would need a not so small refactoring of the engine. And I don't know if many people asked for a feature like this?

@JanDeDobbeleer
Copy link
Owner

@lnu this is the first time that has been asked. But I understand the feature.

@sdsalyer works with the following config:

{
    "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
    "blocks": [
        {
            "type": "prompt",
            "alignment": "left",
            "segments": [
                {
                    "type": "root",
                    "style": "plain",
                    "background": "#ac0101",
                    "foreground": "#fce93c",
                    "properties": {
                        "root_icon": "[ADMIN]"
                    }
                },
                {
                    "type": "time",
                    "style": "plain",
                    "properties": {
                        "prefix": "",
                        "time_format": "15:04:05"
                    }
                },
                {
                    "type": "path",
                    "style": "powerline",
                    "background": "#91ddff",
                    "foreground": "#100e23",
                    "powerline_symbol": "",
                    "properties": {
                        "folder_icon": "",
                        "folder_separator_icon": "",
                        "style": "agnoster_short"
                    }
                }
            ]
        },
        {
            "type": "prompt",
            "alignment": "left",
            "segments": [
                {
                    "type": "git",
                    "style": "diamond",
                    "leading_diamond": "\n<transparent,#95ffa4>\uE0B0</>",
                    "trailing_diamond": "\uE0B0",
                    "foreground": "#193549",
                    "background": "#95ffa4"
                }
            ]
        },
        {
            "type": "newline"
        },
        {
            "type": "prompt",
            "alignment": "left",
            "segments": [
                {
                    "foreground": "#007ACC",
                    "properties": {
                        "prefix": "",
                        "text": "PS❯"
                    },
                    "style": "plain",
                    "type": "text"
                }
            ]
        }
    ],
    "final_space": false
}

@sdsalyer
Copy link
Author

sdsalyer commented Apr 2, 2021

works with the following config:

@JanDeDobbeleer Works like a charm -- thanks so much! Feel free to close this issue unless you think it is a relevant feature add to make blocks conditional. Thanks again for your help.

@Phothiabea
Copy link

Hey I found this issue because I wanted to do the same but the solution only works for left alignment because \n will always move the cursor to the left obviously. I think a real conditional statement for the newline would be good. Or is there another way to achieve the same but with right alignment?

@JanDeDobbeleer
Copy link
Owner

@Linard11 hmm, not without in depth knowledge of ANSI. Let's do it.

@JanDeDobbeleer JanDeDobbeleer reopened this Apr 5, 2021
@Phothiabea
Copy link

Phothiabea commented Apr 5, 2021

To not have over-complicated statements that link a newline blocks to specific segments in other blocks. Maybe the easiest approach would be to have a single bool option (e.g. hide_if_empty) on the newline type that looks ahead into the next block and if none of the segments would be rendered the newline isn't either.

@JanDeDobbeleer
Copy link
Owner

@Linard11 I would opt to remove the previous newline (if present) if the current block is empty. Not sure of the side-effects here so I need to test that a bit.

@JanDeDobbeleer
Copy link
Owner

JanDeDobbeleer commented Apr 17, 2021

@Linard11 I gave this a bit of thought and what we can do is the following:

  • when a block is empty (all segments are disabled), do not render that block
  • add a block property newline (or a better name) which is a boolean indicating the block should start on a newline

If I'm not mistaken, that can render the newline block obsolete and we have everything we need.

@Phothiabea
Copy link

Sounds like a good solution 👍

JanDeDobbeleer added a commit that referenced this issue Apr 18, 2021
this deprecates the "newline" block and favours using the newline
property on the Block component. For backwards compatibility we'll
keep recognizing the newline block for the time being.

resolves #607
JanDeDobbeleer added a commit that referenced this issue Apr 18, 2021
this deprecates the "newline" block and favours using the newline
property on the Block component. For backwards compatibility we'll
keep recognizing the newline block for the time being.

resolves #607
JanDeDobbeleer added a commit that referenced this issue Apr 20, 2021
this deprecates the "newline" block and favours using the newline
property on the Block component. For backwards compatibility we'll
keep recognizing the newline block for the time being.

resolves #607
JanDeDobbeleer added a commit that referenced this issue Apr 21, 2021
this deprecates the "newline" block and favours using the newline
property on the Block component. For backwards compatibility we'll
keep recognizing the newline block for the time being.

resolves #607
JanDeDobbeleer added a commit that referenced this issue Apr 21, 2021
this deprecates the "newline" block and favours using the newline
property on the Block component. For backwards compatibility we'll
keep recognizing the newline block for the time being.

resolves #607
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🚀 feat New feature
Projects
None yet
Development

No branches or pull requests

4 participants