openSUSE / osc

The Command Line Interface to work with an Open Build Service
http://openbuildservice.org/
GNU General Public License v2.0
169 stars 179 forks source link

bash prompt #1379

Open mcepl opened 11 months ago

mcepl commented 11 months ago

Is your feature request related to a problem? Please describe. When using osc as a VCS I would like to have a similar prompt indicating the status of my checkout as I have for git. With fish shell it was a trivial to add a function:

function fish_osc_prompt
    [ -d .git ] && return
    set -l osc_binary (type -p osc)
    if [ -n "$osc_binary" -a -x "$osc_binary" -a -f .osc/_package ]
        set -l osc_str (osc status 2>/dev/null |cut -d' ' -f 1|sort|uniq -c|tr -d ' \n')
        # if [ -n "$osc_str" ]
            printf ' (%s)' $osc_str
        # end
    end
end

but I don’t know how to do it with osc.

Describe the solution you'd like Similar short snippet I would source in ~/.bashrc.

mcepl commented 11 months ago

Hmm, this could work:

#!/bin/bash

__osc_prompt() {
    # Git has a precedence
    if [ -d .git ] ; then
        # Test for the existence of bash function
        declare -F __git_ps1 >/dev/null && printf "%s" "$(__git_ps1 "$@")"
        return
    fi
    # Are we even in the OSC checkout?
    [ -d .osc ] || return

    local osc_binary osc_pattern osc_str; 
    osc_binary=$(type -p osc)
    if [ -n "$1" ] ; then osc_pattern="${*}" ; else osc_pattern="(%s)" ; fi
    if [ -n "$osc_binary" ] && [ -x "$osc_binary" ] && [ -f .osc/_package ] ; then
        osc_str="$(osc status 2>/dev/null |cut -d' ' -f 1|sort|uniq -c|tr -d ' \n')"
        # shellcheck disable=SC2059
        printf " ${osc_pattern}" $osc_str
    fi
}

What do you think?

dmach commented 3 months ago

Sorry it took me so long to look into this. It seems to be working somehow, but I'd prefer to make it faster (it adds about 1s delay before printing a new prompt line). It probably requires some work on my end to provide desired functionality from osc and do it sufficiently fast.