フィッシュ シェル プロンプトで git ステータスを生成しようとしています。私が抱えている問題は、git ステータスの取得が少し遅いことです。そのため、現在の git ステータスをグローバル変数に保持し、ユーザーが特定のコマンド (「cd」や「git」など) を実行したときにのみ更新したいと考えています。「history」コマンドを使用してユーザーが最後に実行したコマンドを取得しようとしていますが、さまざまな結果が得られます。これが私のコードです:
function update_git_status --description 'Calculate new git current branch based on location and set __git_status'
set -g __git_status (python ~/.oh-my-fish/themes/oneself/gitstatus.py)
end
function fish_right_prompt
set -l git_color (set_color red)
set -l normal (set_color normal)
# Update git current branch when certain commands are run
switch $history[1]
case 'git *' 'cd *' 'cd'
update_git_status
end
# Git status
echo "$git_color$__git_status$normal"
end
このコードの主な問題は、履歴が何らかの理由で常に最後のコマンドをすぐに返さないことです。「cd」または「git」以外のコマンドを実行してから cd で git ディレクトリに移動すると、別のコマンドを実行して git_status を正しい文字列に更新する必要があります。
プロンプトを生成する必要がある直前にコマンドを実行する他の方法はありますか?
[アップデート]
問題を実際に試してみるための端末出力は次のとおりです。
~> history --clear
Are you sure you want to clear history ? (y/n)
read> y
History cleared!
~> echo $history[1]
~> ls
documents etc bin downloads Desktop media notes
~> echo $history[1]
ls
~> cd ~/.oh-my-fish/
~/oh-my-fish> echo $history[1]
cd ~/.oh-my-fish/
~/oh-my-fish> master⚡
git ディレクトリ (この場合は .oh-my-fish) に cd すると、ブランチ名がすぐに表示されます。ただし、最終的に表示されるのは、別のコマンドを実行した後でのみです。「echo $history[1]」はコマンドラインで正しい値を返しますが、プロンプトメソッド内から実行すると返されないと思います。
ちなみに、このコードをすべて保持している私の github リポジトリへのリンクは次のとおりです: https://github.com/oneself/oh-my-fish