Skip to content

Commit

Permalink
prompt: support "bleopt prompt_command_changes_layout"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jan 24, 2022
1 parent 711c69f commit e199bee
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
13 changes: 13 additions & 0 deletions blerc
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@
#bleopt prompt_ruler=$'\e[94m-' # blue line


## "prompt_command_changes_layout" specifies whether the commands called from
## the blehook PRECMD or the variable PROMPT_COMMAND output texts to the
## terminal and changes the layout. When a non-empty value is specified,
## ble.sh resets the layout before running the hooks PRECMD and PROMPT_COMMAND
## and restores the layout after running the hooks. When a empty value is
## specified, ble.sh assumes that these hooks do not output texts to the
## terminal and do not changes the cursor positions and skip the special
## treatment.

#bleopt prompt_command_changes_layout= # PRECMD/PROMPT_COMMAND not output
#bleopt prompt_command_changes_layout=1 # PRECMD/PROMPT_COMMAND may output


## "exec_errexit_mark" specifies the format of the mark to show the exit status
## of the command when it is non-zero. If this setting is an empty string the
## exit status will not be shown. The value can contain ANSI escape sequences.
Expand Down
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
- syntax: fix completion and highlighting of `declare` with assignment arguments `#D1704` `#D1705` e12bae4
- cmdspec: refactor `{mandb => cmdspec}_opts` `#D1706` `#D1707` 0786e92
- complete (menu-style:align): refactor `complete_menu_align => menu_align_{min,max}` (motivated by banoris) `#D1717` 22a2449
- prompt: support `bleopt prompt_command_changes_layout` `#D1750` 0000000

## Changes

Expand Down
44 changes: 44 additions & 0 deletions note.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5894,6 +5894,50 @@ bash_tips

2022-01-24

* prompt: wezterm shell-integration が PRECMD で何かを出力するがずれる [#D1750]

先ず precmd を呼び出す瞬間のカーソル位置が前回のプロンプトの後になっている
ので、この時点でずれが生じる。これは bash に倣って次のプロンプトの最初にカー
ソルを移動してから呼び出すべきという事なのだろうか。

x ok: 次に、precmd の直前で flush する様にしても座標位置がずれてしまう。特
にコマンドを実行した直後の状態が怪しい。これは別の dock にいたりする事が
原因なのだろうか。。と思ったが、うーん→これは分かった。info を表示してい
る為に内部座標原点にいないのが原因であった。内部座標原点に移動してから
flush したら、表示位置がずれる問題は解決した。

x 然し依然として precmd の中から出力した事自体によってずれが生じる問題につ
いてはそのままである。うーん。info を表示するよりも前に precmd は実行する
べきという事なのだろうか。

? ok: そもそも precmd と prompt_command は分けて考えるべきなのかもしれない。

コマンド実行ではなくて別の理由で (SIGWINCH などで) プロンプトの再計算が走
る場合に本当に precmd や PROMPT_COMMAND も一緒に呼び出してしまって良いの
かというのも疑問である。端末サイズに応じてプロンプトを更新したいという事
を考えれば PROMPT_COMMAND は WINCH に対して改めて実行したほうが良い気もす
る。然し、PRECMD の意味を考えたら本当にコマンド入力を開始した時にだけ呼び
出したい様な気もする。然し、コマンド入力を開始したい時はやはり
PROMPT_COMMAND なのではという気もする。

その様に色々考えると PROMPT_COMMAND と一緒に実行する現在の設計で問題ない
気もする。そして SIGWINCH でも呼び出して良い。前のプロンプトを捨てて新し
いプロンプトを構築すると考えれば良い。

* ble/prompt/update の呼び出しはより外側に移動しても良いのではないか。

うーん。然し leave in opts のテストがある。これは textarea#render に渡さ
れる引数である。呼び出し元は以下の二箇所。何れも
ble/widget/.insert-newline からの呼び出しである。

./src/edit.sh:5713: ble/textarea#render leave
./src/edit.sh:5724: ble/textarea#render leave

うーん。leave の時には改めてその場で ble/prompt/update を呼び出す。少し書
き換えてみる。

実装してみたは良いがよく考えたら現在の実装だとプロンプトの更新が滞る。

* main: /dev/tty が割り当てられていない時にも ble.sh は初期化しない [#D1749]
https://github.com/oilshell/oil/issues/1069#issuecomment-1017189089

Expand Down
42 changes: 37 additions & 5 deletions src/edit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ bleopt/declare -v prompt_status_line ''
bleopt/declare -n prompt_status_align $'justify=\r'
ble/color/defface prompt_status_line fg=231,bg=240

bleopt/declare -v prompt_command_changes_layout ''

function bleopt/check:prompt_status_align {
case $value in
(left|right|center|justify|justify=?*)
Expand Down Expand Up @@ -277,6 +279,8 @@ function ble/application/render {
local render=$_ble_app_render_mode
case $render in
(panel)
local _ble_prompt_update=owner
ble/prompt/update
ble/edit/leave-command-layout # ble/edit 特有
ble/canvas/panel/render ;;
(forms:*)
Expand Down Expand Up @@ -1427,20 +1431,46 @@ function ble/prompt/update/.eval-prompt_command {
## leave ... 次行に行く直前の最後の表示である事を示します。
## これが指定された時 transient prompt 等の処理を実行します。
##
## @var[in,out] _ble_prompt_update_dirty
## @var[in,out] _ble_prompt_rps1_enabled
##
## @var[in] _ble_edit_PS1
## 構築されるプロンプトの内容を指定します。
## @var[out] _ble_prompt_ps1_data
## 構築したプロンプトの情報を格納します。
_ble_prompt_update=
_ble_prompt_update_dirty=
_ble_prompt_rps1_enabled=
function ble/prompt/update {
local opts=:$1: dirty=

local version=$COLUMNS:$_ble_edit_lineno:$_ble_history_count
if [[ :$opts: == *:check-dirty:* && $_ble_prompt_update == owner ]]; then
if [[ $_ble_prompt_update_dirty && :$opts: != *:leave:* && $_ble_prompt_hash == "$version" ]]; then
[[ $_ble_prompt_update_dirty == dirty ]]; local ext=$?
_ble_prompt_update_dirty=done
return "$ext"
fi
fi

ble/prompt/timeout/check

_ble_prompt_rps1_enabled=

# Update PS1 in PROMPT_COMMAND / PRECMD
local version=$COLUMNS:$_ble_edit_lineno:$_ble_history_count
if ((_ble_textarea_panel==0)); then # 補助プロンプトに対しては PROMPT_COMMAND は実行しない
if [[ $_ble_prompt_hash != "$version" && $opts != *:leave:* ]]; then
if ble/prompt/update/.has-prompt_command || blehook/has-hook PRECMD; then
# #D1750 PROMPT_COMMAND 及び PRECMD が何か出力する時は表示が乱れるので
# クリアする。点滅などを避ける為、既定では off にしておく。
if [[ $bleopt_prompt_command_changes_layout ]]; then
ble/edit/enter-command-layout
local -a DRAW_BUFF=()
ble/canvas/panel#goto.draw 0 0 0 sgr0
ble/canvas/bflush.draw
ble/util/buffer.flush >&2
fi

ble-edit/restore-PS1
ble-edit/exec:gexec/invoke-hook-with-setexit PRECMD
ble/prompt/update/.eval-prompt_command
Expand Down Expand Up @@ -1488,12 +1518,12 @@ function ble/prompt/update {
# bleopt prompt_rps1
if [[ :$opts: == *:leave:* && ! $rps1f && $bleopt_prompt_rps1_transient ]]; then
# prompt_rps1_transient による消去 (以前の大きさを保持)
[[ ${_ble_prompt_rps1_data[10]} ]] && dirty=1 rps1_enabled=erase
[[ ${_ble_prompt_rps1_data[10]} ]] && dirty=1 _ble_prompt_rps1_enabled=erase

else
[[ $prompt_rps1 || ${_ble_prompt_rps1_data[10]} ]] &&
ble/prompt/unit#update _ble_prompt_rps1 && dirty=1
[[ ${_ble_prompt_rps1_data[10]} ]] && rps1_enabled=1
[[ ${_ble_prompt_rps1_data[10]} ]] && _ble_prompt_rps1_enabled=1
fi

# bleopt prompt_xterm_title
Expand Down Expand Up @@ -1521,6 +1551,7 @@ function ble/prompt/update {
[[ $bleopt_prompt_status_line || ${_ble_prompt_status_data[10]} ]] &&
ble/prompt/unit#update _ble_prompt_status && dirty=1

[[ $dirty ]] && _ble_prompt_update_dirty=dirty
[[ $dirty ]]
}
function ble/prompt/clear {
Expand Down Expand Up @@ -2891,8 +2922,8 @@ function ble/textarea#render {
local ble_textarea_render_flag=1 # ble/textarea#panel::onHeightChange から参照する
local caret_state=$_ble_textarea_version:$_ble_edit_ind:$_ble_edit_mark:$_ble_edit_mark_active:$_ble_edit_line_disabled:$_ble_edit_overwrite_mode

local dirty= rps1_enabled=
if ble/prompt/update "$opts"; then
local dirty=
if ble/prompt/update "check-dirty:$opts"; then
dirty=1
elif ((_ble_edit_dirty_draw_beg>=0)); then
dirty=1
Expand All @@ -2916,6 +2947,7 @@ function ble/textarea#render {

local cols=${COLUMNS-80}
local rps1_width=${_ble_prompt_rps1_data[11]}
local rps1_enabled=$_ble_prompt_rps1_enabled
_ble_prompt_rps1_data[12]=$rps1_enabled
if [[ $rps1_enabled ]]; then
((cols-=rps1_width+1,_ble_term_xenl||cols--))
Expand Down

0 comments on commit e199bee

Please sign in to comment.