Skip to content

Instantly share code, notes, and snippets.

@KEINOS
Last active June 5, 2017 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KEINOS/a5e7cca996b4f01e88bf7c71fe5824f2 to your computer and use it in GitHub Desktop.
Save KEINOS/a5e7cca996b4f01e88bf7c71fe5824f2 to your computer and use it in GitHub Desktop.
[IN PROGRESS] Bash file's comments translation in Japanese at '/etc/bashrc_Apple_Terminal'. [macOS Sierra( OSX 10.12 )]
# bashの「ターミナル」対応
# 作業ディレクトリ(ワーキング・ディレクトリ)
#
# ターミナルに現在の作業ディレクトリを、プロンプトごとに伝える
if [ -z "$INSIDE_EMACS" ]; then
update_terminal_cwd() {
# "file:"のURLスキームを使ってディレクトリを特定する。
# ホスト名も含めて特定することで、ローカルなのかリモートなのかの曖昧回避を行う。
# パス名をURLエンコード(パーセントエンコード)する
local url_path=''
{
# 入力テキストをバイトごとに処理するために'LC_CTYPE=C'を使う。
# また、干渉が発生しないように'LC_ALL'がセットされていないことを確認。
local i ch hexch LC_CTYPE=C LC_ALL=
for ((i = 0; i < ${#PWD}; ++i)); do
ch="${PWD:i:1}"
if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
url_path+="$ch"
else
printf -v hexch "%02X" "'$ch"
# 'printf'は127以上の数値を負の値として扱われ'FF'で
# 引き延ばされる(埋められる)ため、切り捨て処理をする
url_path+="%${hexch: -2:2}"
fi
done
}
printf '\e]7;%s\a' "file://$HOSTNAME$url_path"
}
PROMPT_COMMAND="update_terminal_cwd${PROMPT_COMMAND:+; $PROMPT_COMMAND}"
fi
# レジュームのサポート: シェルの状態の保存とリストア
#
# ターミナルは各ターミナルのセッションごとにユニーク(一意)な識別子を
# 割り当て、環境変数'TERM_SESSION_ID'を通して、やりとりを行います。
# それにより、レジュームが有効になっているターミナルの場合は、ターミナ
# ルが閉じられた際に実行されていたプログラムの状態を保存し再度ター
# ミナルが開かれた時に読み込みを行うことでレジュームを可能にしていま
# す。
#
# 以下のコードはシェルの保存と読み込みを定義する仕組みです。ユーザー
# は'shell_session_save_user_state'関数を使うことで、ユーザー
# 独自の状態保存を定義し、セッション・ファイルに保存・読み込みを行えます。
# 例)ユーザー変数を保存したい場合
#
# shell_session_save_user_state() { echo MY_VAR="'$MY_VAR'" >> "$SHELL_SESSION_FILE"; }
#
# シェルの起動時にセッション・ファイルが実行され、古いファイルは周期的
# に削除されていきます。
#
# デフォルトでの動作は、ターミナルが起動されると、bashのコマンド履歴の
# 保存および再読み込みを行いターミナル・セッションが復元されます。また、
# 新しいセッションの場合もコマンド履歴がグーローバル履歴に追加されます。
# そのため"HISTSIZE"と"HISTFILESIZE"をより大きい値にすることが
# 推奨されています。
#
# You may disable this behavior and share a single history by setting
# SHELL_SESSION_HISTORY to 0. There are some common user customizations
# that arrange to share new commands among running shells by
# manipulating the history at each prompt, and they typically include
# 'shopt -s histappend'; therefore, if the histappend shell option is
# enabled, per-session history is disabled by default. You may
# explicitly enable it by setting SHELL_SESSION_HISTORY to 1.
#
# The implementation of per-session command histories in combination
# with a shared global command history is incompatible with the
# HISTTIMEFORMAT variable--the timestamps are applied inconsistently
# to different parts of the history; therefore, if HISTTIMEFORMAT is
# defined, per-session history is disabled by default.
#
# Note that this uses PROMPT_COMMAND to enable per-session history
# the first time for each new session. If you customize PROMPT_COMMAND
# be sure to include the previous value. e.g.,
#
# PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }your_code_here"
#
# Otherwise, the per-session history won't take effect until the first
# restore.
#
# The save/restore mechanism is disabled if the following file exists:
#
# ~/.bash_sessions_disable
if [ ${SHELL_SESSION_DID_INIT:-0} -eq 0 ] && [ -n "$TERM_SESSION_ID" ] && [ ! -e "$HOME/.bash_sessions_disable" ]; then
# Do not perform this setup more than once (which shouldn't occur
# unless the user's ~/.bash_profile executes /etc/profile, which
# is normally redundant).
SHELL_SESSION_DID_INIT=1
# Set up the session directory/file.
SHELL_SESSION_DIR="$HOME/.bash_sessions"
SHELL_SESSION_FILE="$SHELL_SESSION_DIR/$TERM_SESSION_ID.session"
mkdir -m 700 -p "$SHELL_SESSION_DIR"
#
# Restore previous session state.
#
if [ -r "$SHELL_SESSION_FILE" ]; then
. "$SHELL_SESSION_FILE"
rm "$SHELL_SESSION_FILE"
fi
#
# Arrange for per-session shell command history.
#
shell_session_history_allowed() {
# Return whether per-session history should be enabled.
if [ -n "$HISTFILE" ]; then
# If this defaults to off, leave it unset so that we can
# check again later. If it defaults to on, make it stick.
local allowed=0
if shopt -q histappend || [ -n "$HISTTIMEFORMAT" ]; then
allowed=${SHELL_SESSION_HISTORY:-0}
else
allowed=${SHELL_SESSION_HISTORY:=1}
fi
if [ $allowed -eq 1 ]; then
return 0
fi
fi
return 1
}
if [ ${SHELL_SESSION_HISTORY:-1} -eq 1 ]; then
SHELL_SESSION_HISTFILE="$SHELL_SESSION_DIR/$TERM_SESSION_ID.history"
SHELL_SESSION_HISTFILE_NEW="$SHELL_SESSION_DIR/$TERM_SESSION_ID.historynew"
SHELL_SESSION_HISTFILE_SHARED="$HISTFILE"
shell_session_history_enable() {
(umask 077; touch "$SHELL_SESSION_HISTFILE_NEW")
HISTFILE="$SHELL_SESSION_HISTFILE_NEW"
SHELL_SESSION_HISTORY=1
}
# If the session history already exists and isn't empty, start
# using it now; otherwise, we'll use the shared history until
# we've determined whether users have enabled/disabled this.
if [ -s "$SHELL_SESSION_HISTFILE" ]; then
history -r "$SHELL_SESSION_HISTFILE"
shell_session_history_enable
else
# At the first prompt, check whether per-session history should
# be enabled. Delaying until after user scripts have run allows
# users to opt in or out. If this doesn't get executed (because
# the user has replaced PROMPT_COMMAND instead of concatenating
# it), we'll check at shell exit; that works, but doesn't start
# the per-session history until the first restore.
shell_session_history_check() {
if [ ${SHELL_SESSION_DID_HISTORY_CHECK:-0} -eq 0 ]; then
SHELL_SESSION_DID_HISTORY_CHECK=1
if shell_session_history_allowed; then
shell_session_history_enable
fi
# Remove this check if we can; otherwise, we rely on the
# variable above to prevent checking more than once.
if [ "$PROMPT_COMMAND" = "shell_session_history_check" ]; then
unset PROMPT_COMMAND
elif [[ $PROMPT_COMMAND =~ (.*)(; *shell_session_history_check *| *shell_session_history_check *; *)(.*) ]]; then
PROMPT_COMMAND="${BASH_REMATCH[1]}${BASH_REMATCH[3]}"
fi
fi
}
PROMPT_COMMAND="shell_session_history_check${PROMPT_COMMAND:+; $PROMPT_COMMAND}"
fi
shell_session_save_history() {
# Save new history to an intermediate file so we can copy it.
shell_session_history_enable
history -a
# If the session history doesn't exist yet, copy the shared history.
if [ -f "$SHELL_SESSION_HISTFILE_SHARED" ] && [ ! -s "$SHELL_SESSION_HISTFILE" ]; then
echo -ne '\n...copying shared history...'
(umask 077; cp "$SHELL_SESSION_HISTFILE_SHARED" "$SHELL_SESSION_HISTFILE")
fi
# Save new history to the per-session and shared files.
echo -ne '\n...saving history...'
(umask 077; cat "$SHELL_SESSION_HISTFILE_NEW" >> "$SHELL_SESSION_HISTFILE_SHARED")
(umask 077; cat "$SHELL_SESSION_HISTFILE_NEW" >> "$SHELL_SESSION_HISTFILE")
: >| "$SHELL_SESSION_HISTFILE_NEW"
# If there is a history file size limit, apply it to the files.
if [ -n "$HISTFILESIZE" ]; then
echo -n 'truncating history files...'
HISTFILE="$SHELL_SESSION_HISTFILE_SHARED"
HISTFILESIZE="$HISTFILESIZE"
HISTFILE="$SHELL_SESSION_HISTFILE"
HISTFILESIZE="$size"
HISTFILE="$SHELL_SESSION_HISTFILE_NEW"
fi
echo -ne '\n...'
}
fi
#
# Arrange to save session state when exiting the shell.
#
shell_session_save() {
# Save the current state.
if [ -n "$SHELL_SESSION_FILE" ]; then
echo -n 'Saving session...'
(umask 077; echo 'echo Restored session: "$(date -r '$(date +%s)')"' >| "$SHELL_SESSION_FILE")
declare -F shell_session_save_user_state >/dev/null && shell_session_save_user_state
shell_session_history_allowed && shell_session_save_history
echo 'completed.'
fi
}
# Delete old session files. (Not more than once a day.)
SHELL_SESSION_TIMESTAMP_FILE="$SHELL_SESSION_DIR/_expiration_check_timestamp"
shell_session_delete_expired() {
if ([ ! -e "$SHELL_SESSION_TIMESTAMP_FILE" ] || [ -z "$(find "$SHELL_SESSION_TIMESTAMP_FILE" -mtime -1d)" ]); then
local expiration_lock_file="$SHELL_SESSION_DIR/_expiration_lockfile"
if shlock -f "$expiration_lock_file" -p $$; then
echo -n 'Deleting expired sessions...'
local delete_count=$(find "$SHELL_SESSION_DIR" -type f -mtime +2w -print -delete | wc -l)
[ "$delete_count" -gt 0 ] && echo $delete_count' completed.' || echo 'none found.'
(umask 077; touch "$SHELL_SESSION_TIMESTAMP_FILE")
rm "$expiration_lock_file"
fi
fi
}
# Update saved session state when exiting.
shell_session_update() {
shell_session_save && shell_session_delete_expired
}
trap shell_session_update EXIT
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment