JanDeDobbeleer / oh-my-posh

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

Truncate git branch name #755

Closed SunMar closed 3 years ago

SunMar commented 3 years ago

Hi,

While I was setting up I ran into the problem that my git branch names are too long. They can sometimes be up to 50 characters long which takes up a lot of space. It would be nice if it's possible to set a maximum (f.ex. 20 or 30 characters) and truncate anything after that.

SunMar commented 3 years ago

Sorry, took a bit for me to add back the git segment in my prompt. Thank you very much to take the time to add this feature @JanDeDobbeleer!

Unfortunately the new branch_max_length doesn't seem to be working for me. Possibly I'm doing something wrong?

mkdir omp_test
cd omp_test/
git init
git config user.email test@ohmyposh.dev
git config user.name omp
git commit --allow-empty -m test
git branch some-very-long-branch-name-that-is-going-to-take-up-65-characters
git checkout some-very-long-branch-name-that-is-going-to-take-up-65-characters

image

My git segment is configured like this:

{
    "type": "git",
    "style": "powerline",
    "powerline_symbol": "\ue0b0",
    "foreground": "#d4e157",
    "background": "#546e7a",
    "properties": {
        "display_status": true,
        "branch_max_length": "25"
    }
}

Here is my full theme file:

{
    "$schema": "https:\/\/raw.githubusercontent.com\/JanDeDobbeleer\/oh-my-posh3\/main\/themes\/schema.json",
    "final_space": true,
    "blocks": [
        {
            "type": "prompt",
            "alignment": "left",
            "segments": [
                {
                    "type": "os",
                    "style": "diamond",
                    "foreground": "#26c6da",
                    "background": "#546e7a"
                },
                {
                    "type": "session",
                    "style": "powerline",
                    "powerline_symbol": "\ue0b0",
                    "foreground": "#000000",
                    "background": "#4e9a06"
                },
                {
                    "type": "time",
                    "style": "diamond",
                    "background": "#d3d7cf",
                    "foreground": "#000000",
                    "properties": {
                        "prefix": " \uf017 "
                    }
                },
                {
                    "type": "path",
                    "style": "powerline",
                    "powerline_symbol": "\ue0b0",
                    "foreground": "#e4e4e4",
                    "background": "#3465a4",
                    "properties": {
                        "prefix": " \uf07c ",
                        "home_icon": "~",
                        "style": "full"
                    }
                },
                {
                    "type": "git",
                    "style": "powerline",
                    "powerline_symbol": "\ue0b0",
                    "foreground": "#d4e157",
                    "background": "#546e7a",
                    "properties": {
                        "display_status": true,
                        "branch_max_length": "25"
                    }
                },
                {
                    "type": "root",
                    "style": "powerline",
                    "powerline_symbol": "\ue0b0",
                    "foreground": "#100e23",
                    "background": "#ffe9aa"
                },
                {
                    "type": "python",
                    "style": "powerline",
                    "powerline_symbol": "\ue0b0",
                    "foreground": "#100e23",
                    "background": "#906cff",
                    "properties": {
                        "prefix": " \ue235 "
                    }
                },
                {
                    "type": "exit",
                    "style": "powerline",
                    "powerline_symbol": "\ue0b0",
                    "foreground": "#d3d7cf",
                    "background": "#000000",
                    "properties": {
                        "always_enabled": true,
                        "display_exit_code": true,
                        "error_color": "#cc2222",
                        "color_background": true,
                        "success_icon": "\u2714"
                    }
                }
            ]
        }
    ]
}
JanDeDobbeleer commented 3 years ago

@SunMar I take it you upgraded oh-my-posh and restarted your terminal

ooh wait, it's an int so you need to change it to "branch_max_length": 25

SunMar commented 3 years ago

@JanDeDobbeleer yes in the screenshot you can see that I'm on the latest version 3.166.0 from yesterday and indeed I did restart the terminal just to be sure. Oh My Posh doesn't use a background service like powerline can, does it? I assumed I don't have to kill anything or reboot, just restart the terminal session.

Oops, sorry about the int, not sure why I put it between quotes. But changing that doesn't seem to resolve the issue, I'm still seeing the whole branch name when I expected to only see a partial some-very-long-branch-nam.

image

My updated theme is:

{
    "type": "git",
    "style": "powerline",
    "powerline_symbol": "\ue0b0",
    "foreground": "#d4e157",
    "background": "#546e7a",
    "properties": {
        "display_status": true,
        "branch_max_length": 25
    }
}

And the full updated JSON:

{
    "$schema": "https:\/\/raw.githubusercontent.com\/JanDeDobbeleer\/oh-my-posh3\/main\/themes\/schema.json",
    "final_space": true,
    "blocks": [
        {
            "type": "prompt",
            "alignment": "left",
            "segments": [
                {
                    "type": "os",
                    "style": "diamond",
                    "foreground": "#26c6da",
                    "background": "#546e7a"
                },
                {
                    "type": "session",
                    "style": "powerline",
                    "powerline_symbol": "\ue0b0",
                    "foreground": "#000000",
                    "background": "#4e9a06"
                },
                {
                    "type": "time",
                    "style": "diamond",
                    "background": "#d3d7cf",
                    "foreground": "#000000",
                    "properties": {
                        "prefix": " \uf017 "
                    }
                },
                {
                    "type": "path",
                    "style": "powerline",
                    "powerline_symbol": "\ue0b0",
                    "foreground": "#e4e4e4",
                    "background": "#3465a4",
                    "properties": {
                        "prefix": " \uf07c ",
                        "home_icon": "~",
                        "style": "full"
                    }
                },
                {
                    "type": "git",
                    "style": "powerline",
                    "powerline_symbol": "\ue0b0",
                    "foreground": "#d4e157",
                    "background": "#546e7a",
                    "properties": {
                        "display_status": true,
                        "branch_max_length": 25
                    }
                },
                {
                    "type": "root",
                    "style": "powerline",
                    "powerline_symbol": "\ue0b0",
                    "foreground": "#100e23",
                    "background": "#ffe9aa"
                },
                {
                    "type": "python",
                    "style": "powerline",
                    "powerline_symbol": "\ue0b0",
                    "foreground": "#100e23",
                    "background": "#906cff",
                    "properties": {
                        "prefix": " \ue235 "
                    }
                },
                {
                    "type": "exit",
                    "style": "powerline",
                    "powerline_symbol": "\ue0b0",
                    "foreground": "#d3d7cf",
                    "background": "#000000",
                    "properties": {
                        "always_enabled": true,
                        "display_exit_code": true,
                        "error_color": "#cc2222",
                        "color_background": true,
                        "success_icon": "\u2714"
                    }
                }
            ]
        }
    ]
}
SunMar commented 3 years ago

Just to be sure I double checked PowerShell as well using the same theme file (with the int fix), and there it's also not truncating:

image

JanDeDobbeleer commented 3 years ago

@SunMar allow me to validate this. Maybe I missed something obvious. Can you in the meantime ensure git is up-to-date as well?

SunMar commented 3 years ago

@JanDeDobbeleer Thank for taking the time to check.

My WSL is running Ubuntu 21.04 and has git version 2.30.2 installed, which is the latest version available in the Ubuntu repositories.

In Windows my git version is the latest 2.32.0 and in PowerShell my posh-git version is the latest 1.0.0.

Let me know if there is any else I can help with.

JanDeDobbeleer commented 3 years ago

@SunMar fix incoming. Json parses as float. Standard "unit test succeeds, integration test fails" scenario.

SunMar commented 3 years ago

@JanDeDobbeleer it's working now, thank you very much !! 🥳

github-actions[bot] commented 7 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.