From 91b36b48a7c6766050889fa684998f4b9f423301 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Tue, 23 Aug 2022 12:05:02 +0900 Subject: [PATCH] aliases/general (cp,mv,mkdir): check the support for non-POSIX "-v" https://github.com/ohmybash/oh-my-bash/issues/351 --- aliases/general.aliases.sh | 36 +++++++++++++++++++++++++++++++++--- lib/utils.sh | 9 +++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/aliases/general.aliases.sh b/aliases/general.aliases.sh index 90b3ddc..9721f19 100644 --- a/aliases/general.aliases.sh +++ b/aliases/general.aliases.sh @@ -21,9 +21,39 @@ # 1. MAKE TERMINAL BETTER # ----------------------------- -alias cp='cp -iv' # Preferred 'cp' implementation -alias mv='mv -iv' # Preferred 'mv' implementation -alias mkdir='mkdir -pv' # Preferred 'mkdir' implementation +# Determines the use of the option `-v' on the first call +# Ref. https://github.com/ohmybash/oh-my-bash/issues/351 +function _omb_alias_general_cp_init { + if (tmp=$(_omb_util_mktemp); trap 'rm -f "$tmp"{,.2}' EXIT; command cp -v "$tmp" "$tmp.2" &>/dev/null); then + alias cp='cp -iv' && unset -f "$FUNCNAME" + command cp -iv "$@" + else + alias cp='cp -i' && unset -f "$FUNCNAME" + command cp -i "$@" + fi +} +function _omb_alias_general_mv_init { + if (tmp=$(_omb_util_mktemp); trap 'rm -f "$tmp.2"' EXIT; command mv -v "$tmp" "$tmp.2" &>/dev/null); then + alias mv='mv -iv' && unset -f "$FUNCNAME" + command mv -iv "$@" + else + alias mv='mv -i' && unset -f "$FUNCNAME" + command mv -i "$@" + fi +} +function _omb_alias_general_mkdir_init { + if command mkdir -pv . &>/dev/null; then + alias mkdir='mkdir -pv' && unset -f "$FUNCNAME" + command mkdir -pv "$@" + else + alias mkdir='mkdir -p' && unset -f "$FUNCNAME" + command mkdir -p "$@" + fi +} + +alias cp='_omb_alias_general_cp_init' # Preferred 'cp' implementation +alias mv='_omb_alias_general_mv_init' # Preferred 'mv' implementation +alias mkdir='_omb_alias_general_mkdir_init' # Preferred 'mkdir' implementation alias ll='ls -lAFh' # Preferred 'ls' implementation alias less='less -FSRXc' # Preferred 'less' implementation alias nano='nano -W' # Preferred 'nano' implementation diff --git a/lib/utils.sh b/lib/utils.sh index ae86230..9e8750c 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -400,3 +400,12 @@ _omb_util_alias() { esac alias -- "$1" } + +function _omb_util_mktemp { + local template=tmp.oh-my-bash.XXXXXXXXXX + if type -t mktemp &>/dev/null; then + mktemp -t "$template" + else + m4 -D template="${TMPDIR:-/tmp}/$template" <<< 'mkstemp(template)' + fi +}