Skip to content

Instantly share code, notes, and snippets.

@fitrh
Last active September 6, 2023 07:25
Show Gist options
  • Save fitrh/f8c70203e3bca142d52a585e34f0248d to your computer and use it in GitHub Desktop.
Save fitrh/f8c70203e3bca142d52a585e34f0248d to your computer and use it in GitHub Desktop.
format = """
$username\
$kubernetes\
$directory\
${custom.git_host}\
$git_branch\
$git_commit\
$git_state\
$git_status\
$hg_branch\
$docker_context\
$package\
$cmake\
$dart\
$deno\
$dotnet\
$elixir\
$elm\
$erlang\
$golang\
$helm\
$java\
$julia\
$kotlin\
$nim\
$nodejs\
$ocaml\
$perl\
$php\
$purescript\
$python\
$red\
$ruby\
$rust\
$swift\
$terraform\
$vlang\
$vagrant\
$zig\
$nix_shell\
$conda\
$aws\
$gcloud\
$openstack\
$env_var\
$crystal\
$custom\
$cmd_duration\
$lua\
$line_break\
$jobs\
$character"""
scan_timeout = 10
add_newline = false
[battery]
disabled = true
[character]
success_symbol = "[](blue)"
error_symbol = "[](bright-red)"
[cmd_duration]
show_notifications = true
min_time_to_notify = 60_000
[directory]
truncation_length = 1
format = "[](blue) [$path]($style)[$read_only]($read_only_style)"
read_only = ""
truncation_symbol = ""
fish_style_pwd_dir_length = 0
[git_branch]
format = ":[$symbol$branch]($style)"
symbol = ""
[git_state]
format = '\(:[$state( $progress_current/$progress_total)]($style)\)'
[git_status]
format = '(:[$all_status$ahead_behind]($style)) '
conflicted = '\[ [$count](bright-white bold)\]'
ahead = '\[[﯁ ](bright-blue)[$count](bright-white bold)\]'
behind = '\[[﮾ ](white)[$count](bright-white bold)\]'
diverged = '\[[ ](purple)|[ ﯁ ](bright-blue)[$ahead_count](bright-white bold)[ ﮾ ](white)[$behind_count](bright-white)\]'
untracked = '\[ [$count](bright-white bold)\]'
stashed = '\[[](yellow) [$count](bright-white bold)\]'
modified = '\[[ ](bright-yellow)[$count](bright-white bold)\]'
staged = '\[[ ](bright-green)[$count](bright-white bold)\]'
renamed = '\[[ ](bright-cyan) [$count](bright-white bold)\]'
deleted = '\[ [$count](bright-white bold)\]'
[python]
format = 'via [${symbol}${pyenv_prefix}(${version} )(\($virtualenv\) )]($style)'
[custom.git_host]
command = """
URL=$(command git ls-remote --get-url 2> /dev/null)
if [[ "$URL" =~ "github" ]]; then
ICON=" "
elif [[ "$URL" =~ "gitlab" ]]; then
ICON=" "
elif [[ "$URL" =~ "bitbucket" ]];then
ICON=" "
elif [[ "$URL" =~ "kernel" ]];then
ICON=" "
elif [[ "$URL" =~ "archlinux" ]];then
ICON=" "
elif [[ "$URL" =~ "gnu" ]];then
ICON=" "
elif [[ "$URL" =~ "git" ]];then
ICON=" "
else
ICON=" "
URL="localhost"
fi
for PATTERN in "https" "http" "git" "://" "@"; do
[[ "$URL" == "$PATTERN"* ]] && URL="${URL##$PATTERN}"
done
for PATTERN in "/" ".git"; do
[[ "$URL" == *"$PATTERN" ]] && URL="${URL%%$PATTERN}"
done
printf "%s%s" "$ICON" "$URL"
"""
directories = [".git"]
when = 'git rev-parse --is-inside-work-tree 2> /dev/null'
shell = ["bash","--norc","--noprofile"]
style = "bright-yellow bold"
format = " at [$output]($style)"
[hostname]
disabled = true
@fitrh
Copy link
Author

fitrh commented Jun 14, 2021

You can try this

function get_git_host
    set -l URL (git ls-remote --get-url 2> /dev/null)
    set -l ICON ""

    if string match -eq "github" $URL
        set ICON ""
    else if string match -eq "gitlab" $URL
        set ICON ""
    else if string match -eq "bitbucker" $URL
        set ICON ""
    else if string match -eq "kernel" $URL
        set ICON ""
    else if string match -eq "archlinux" $URL
        set ICON ""
    else if string match -eq "gnu" $URL
        set ICON ""
    else if string match -eq "git" $URL
        set ICON ""
    else
        set URL "localhost"
    end

    for PATTERN in "git" "https" "http" "@" "://"
        if string match -eq $PATTERN $URL
            set URL (string trim -lc $PATTERN $URL)
        end
    end
    
    for PATTERN in "/" ".git"
        if string match -eq $PATTERN $URL
            set URL (string trim -rc $PATTERN $URL)
        end
    end

    printf "%s%s\n" $ICON $URL
end

but i think fish shell will slow down your starship startup process unless you know --norc --noprofile equivalent in fish

@fitrh
Copy link
Author

fitrh commented Jun 14, 2021

@fitrh will this custom git host code run on fish shell?

Yes, of course, all you need is adding the [custom.git_host] in your config

Copy link

ghost commented Jun 14, 2021

@fitrh will this custom git host code run on fish shell?

Yes, of course, all you need is adding the [custom.git_host] in your config

thanks for your help
your default config is enough

according to this discussion starship can run the code by using its built-in bash regardless of the interactive shell
in my case, I use fish not bash

@fitrh
Copy link
Author

fitrh commented Jun 14, 2021

Yeah, that's what shell variable do, specify which shell that run the command variable, and not only the interactive shell, even if you use fish shell as your login shell (which i do), the command will still use what inside shell variable

Copy link

ghost commented Jun 22, 2021

Hi @fitrh
I used your code in my starship.toml
when i open my kitty terminal, it shows this
[ERROR] - (starship::config): Unable to parse the config file: invalid escape character in string: / at line 67 column 72
indicates this line
[[ "$REMOTE_URL" == "https://"* ]] && REMOTE_URL="${REMOTE_URL//https:\/\/}"
I didn't change any directory or any .git containing one folder
regards

@fitrh
Copy link
Author

fitrh commented Jun 23, 2021

Yes, i didn't notice that because i use it as separate script, you can try the new gist now, i have updated it

Copy link

ghost commented Jun 25, 2021

@fitrh I have used your updated script and it shows this
though i have nerd font installed
PS: i dmed you in discord but you didn't reply

terminal_problem (1)

@fitrh
Copy link
Author

fitrh commented Jun 26, 2021

can you share your full config ?

Copy link

ghost commented Jun 26, 2021

@fitrh my starship.toml combining with your latest code

# Disable to insert a blank line between shell prompts
# add_newline = false

# make prompt a single line instead of two lines
# [line_break]
# disabled = true

[character]
# Replace the "❯" symbol in the prompt with "➜"
# success_symbol = "[➜](bold green)"
error_symbol = "[✖](bold red)"

[custom.git_host]
command = """
URL=$(command git ls-remote --get-url 2> /dev/null)

if [[ "$URL" =~ "github" ]]; then
    ICON=" "
elif [[ "$URL" =~ "gitlab" ]]; then
    ICON=" "
elif [[ "$URL" =~ "bitbucket" ]];then
    ICON=" "
elif [[ "$URL" =~ "kernel" ]];then
    ICON=" "
elif [[ "$URL" =~ "archlinux" ]];then
    ICON=" "
elif [[ "$URL" =~ "gnu" ]];then
    ICON=" "
elif [[ "$URL" =~ "git" ]];then
    ICON=" "
else
    ICON=" "
    URL="localhost"
fi

for PATTERN in "https" "http" "git" "://" "@"; do
    [[ "$URL" == "$PATTERN"* ]] && URL="${URL##$PATTERN}"
done

for PATTERN in "/" ".git"; do
    [[ "$URL" == *"$PATTERN" ]] && URL="${URL%%$PATTERN}"
done
printf "%s%s" "$ICON" "$URL"
"""
directories = [".git"]
when = 'git rev-parse --is-inside-work-tree 2> /dev/null'
shell = ["sh","--norc","--noprofile"]
style = "bright-yellow bold"
format = " at [$output]($style)"

@fitrh
Copy link
Author

fitrh commented Jun 26, 2021

I have tested your config and the result is fine, did you follow my format variable ? don't forget to add ${custom.git_host} there.
here is the result, the first one use my config and the second one use yours

20210626_165401_full

Copy link

ghost commented Jun 26, 2021

@fitrh i couldn't check all the remote urls
but the offline version doesn't work
I still don't get it though

Should i use previous version standalone script or the latest one of your code?
Could you give me some advice
(keeping the timing, which doesn't slow down terminal)

PS: default format variable is given in starship configuration.. it's similar to yours
https://starship.rs/config/#prompt

@fitrh
Copy link
Author

fitrh commented Jun 26, 2021

What do you mean by offline version? did you mean git repo without remote url ? if so, it should output localhost instead, and your config works for it.

Can you try changing the shell variable from sh to bash ? some systems may not have sh or not symlinked their shell to sh

Copy link

ghost commented Jun 26, 2021 via email

@fitrh
Copy link
Author

fitrh commented Jun 26, 2021

i don't know what do you mean by "offline version", but here is the result when i use your config for a repo without remote
20210627_003937_full

Copy link

ghost commented Jun 27, 2021

it worked finally @fitrh
changing sh to bash solved my problem

Could you please tell me which you are using exa or lsd with nerdfont ?
I see your terminal ui folder & file icons

@fitrh
Copy link
Author

fitrh commented Jun 27, 2021

It's exa

Copy link

ghost commented Jun 27, 2021

@fitrh what will you use.. the previous standalone script or the latest one?

@fitrh
Copy link
Author

fitrh commented Jun 27, 2021

the standalone bash script, because it's make the config cleaner

@j0k4rX
Copy link

j0k4rX commented Jun 27, 2021

the standalone bash script, because it's make the config cleaner

@fitrh how can i add standalone bash script?
I saw your previous code

@fitrh
Copy link
Author

fitrh commented Jun 27, 2021

@j0k4rX just take the command value into a bash file, make the file executable, and put the file into a directory inside your $PATH (so you can call it everywhere from your shell) or where ever you like, then change the command value to the path of your script.
For me, i created a bash script called getgithost and put it inside ~/.local/script, so my command variable look like this
command = "~/.local/script/getgithost"

Copy link

ghost commented Jun 27, 2021 via email

@fitrh
Copy link
Author

fitrh commented Jun 27, 2021

@backermanbd there is no "first", because the only thing that get executed is what inside command variable, and you can not have multiple command for a custom module

Copy link

ghost commented Jun 27, 2021

@j0k4rX just take the command value into a bash file, make the file executable, and put the file into a directory inside your $PATH (so you can call it everywhere from your shell) or where ever you like, then change the command value to the path of your script.
For me, i created a bash script called getgithost and put it inside ~/.local/script, so my command variable look like this
command = "~/.local/script/getgithost"

@fitrh can you be more specific?

should i add this line in .bashrc
command = "~/.local/script/getgithost"

and the getgithost is the file name or getgithost.bashrc something like this under ~/.local/script

@fitrh
Copy link
Author

fitrh commented Jun 27, 2021

should i add this line in .bashrc
command = "~/.local/script/getgithost"

no, it's for your starship config

and the getgithost is the file name or getgithost.bashrc something like this under ~/.local/script

just the getgithost

@fitrh
Copy link
Author

fitrh commented Jun 27, 2021

replace this

[custom.git_host]
command = """
URL=$(command git ls-remote --get-url 2> /dev/null)

if [[ "$URL" =~ "github" ]]; then
    ICON=" "
elif [[ "$URL" =~ "gitlab" ]]; then
    ICON=" "
elif [[ "$URL" =~ "bitbucket" ]];then
    ICON=" "
elif [[ "$URL" =~ "kernel" ]];then
    ICON=" "
elif [[ "$URL" =~ "archlinux" ]];then
    ICON=" "
elif [[ "$URL" =~ "gnu" ]];then
    ICON=" "
elif [[ "$URL" =~ "git" ]];then
    ICON=" "
else
    ICON=" "
    URL="localhost"
fi

for PATTERN in "https" "http" "git" "://" "@"; do
    [[ "$URL" == "$PATTERN"* ]] && URL="${URL##$PATTERN}"
done

for PATTERN in "/" ".git"; do
    [[ "$URL" == *"$PATTERN" ]] && URL="${URL%%$PATTERN}"
done
printf "%s%s" "$ICON" "$URL"
"""

with this

[custom.git_host]
command = "path/to/your/script"

Copy link

ghost commented Jun 28, 2021

got it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment