diff --git a/files/cclip b/files/cclip index 0b0f1f4..9256da9 100755 --- a/files/cclip +++ b/files/cclip @@ -2,59 +2,65 @@ set -e _help() { - SELF=$( basename "$0" ) - echo "Console Clipboard manager - Note: Copying happens only to a list. If source file is deleted, it - can not be copied anymore. + echo "${HE}Console Clipboard${NO} - List clipboard names: - $SELF l/list [clipboard] +Note: Copying happens only to a list. If source file is deleted, it +can not be pasted anymore. Default clipboard is named: 0 - Make a link to clipboard: - $SELF c/copy filename[s] [clipboard] +List clipboard names: + ${HL}$SELF l/list [clipboard]${NO} - Paste files using clipboard: - $SELF p/paste [switches] [clipboardname[s]] [folder/] - Default folder: . - Switches: +Make a link to clipboard: + ${HL}$SELF c/copy filename[s] [clipboard]${NO} + +Paste files using clipboard: + ${HL}$SELF p/paste [switches] [clipboardname[s]] [folder/]${NO} + Default folder: . + Switches: -s Use soft link instead of file copying -l Use hardlink instead of file copying -k Keep clipboard link -m Move instead of copy (can not be used with -h, can not -k) - Delete source files using clipboard links (WARNING): - $SELF rm/remove clipboardname[s] +Delete source files using clipboard links (WARNING): + ${HL}$SELF rm/remove clipboardname[s]${NO} - Edit links manually: - $SELF e/edit clipboardname[s] - Clear clipboard links: - $SELF d/del clipboardname[s] - Clear all clipboard links: - $SELF D/Del +Edit links manually: + ${HL}$SELF e/edit clipboardname[s]${NO} +Clear clipboard links: + ${HL}$SELF d/del clipboardname[s]${NO} +Clear all clipboard links: + ${HL}$SELF D/Del${NO} - Get autocomplete: +Get autocomplete: $SELF autocomplete - Config: - CCLIP_HOME environment variable sets clipboard storage folder, - defauls to ~/.cache/cclip +Config: + CCLIP_HOME environment variable sets clipboard storage folder, + defauls to ~/.cache/cclip " exit } _load_config() { + # colors + HL=$( _qCol W 2>/dev/null || true ) + HE=$( _qCol Y U bk 2>/dev/null || true ) + HERR=$( _qCol R bk 2>/dev/null || true ) + NO=$( _qCol z 2>/dev/null || true ) + SELF=$( basename "$0" ) STORAGE=${CCLIP_HOME:-~/.cache/cclip} [[ -d "$STORAGE" ]] || { - _msg "Creating $STORAGE folder" + echo "Creating $STORAGE folder" mkdir -p "$STORAGE" } } _list() { if [[ ! $( ls -A "${STORAGE}" ) ]]; then - _msg "No clipboard entries" + _err "No clipboard entries" return fi if [[ $# -gt 1 ]]; then @@ -69,11 +75,8 @@ _list() { longest=${#n} fi done - hl=$( _qCol W 2>/dev/null || true ) - he=$( _qCol y U 2>/dev/null || true ) - no=$( _qCol z 2>/dev/null || true ) now=$( date -Idate ) - printf "%s%-${longest}s %-10s %s%s\n" "$he" Name Added/Size Path "$no" + printf "%s%-${longest}s %-10s %s%s\n" "$HE" Name Added/Size Path "$NO" IFS=$'\n' while read name; do if [[ -n "$list_only" ]]; then @@ -102,8 +105,8 @@ _list() { printf "%-${longest}s %5s %s%s%s\n" \ " " \ "$size" \ - "$hl" "${f}" \ - "$no" + "$HL" "${f}" \ + "$NO" done < "${STORAGE}/$name" done < <( ls "${STORAGE}" -t ) } @@ -122,7 +125,7 @@ _copy() { if [[ ! -e "$i" ]]; then CLIPBOARD=$( echo "$i" | tr -d './' | tr -cd '[[:print:]]' ) if [[ -z "$CLIPBOARD" ]]; then - _msg "Invalid clipboard name: '$i'" + _err "Invalid clipboard name: '$i'" exit 1 fi numargs=$(( numargs - 1 )) @@ -150,7 +153,7 @@ _edit() { for (( i=2; i<=$numargs; i++ )); do CLIPBOARD=$( echo "${!i}" | tr -d './' | tr -cd '[[:print:]]' ) if [[ ! -f "$STORAGE/${!i}" ]]; then - echo "No such clipboard: ${!i}" + _err "No such clipboard: ${!i}" continue fi ${VISUAL:-vim} "$STORAGE/${CLIPBOARD}" @@ -180,7 +183,7 @@ _paste() { continue fi fi - echo No such clipboard: ${!i} + _err "No such clipboard: ${!i}" return fi ;; @@ -206,7 +209,7 @@ _paste() { _paste_clipboard() { if [[ ! -f "$STORAGE/$1" ]];then - _msg "No such clipboard: '$1'" + _err "No such clipboard: '$1'" return fi while read f; do @@ -229,7 +232,7 @@ _paste_clipboard() { _paste_single() { if [[ ! -e "$1" ]]; then - _msg "No such path: $1" + _err "No such path: $1" return fi target=$( basename "$1" ) @@ -263,7 +266,7 @@ _paste_single() { fi if [[ "$softlink$hardlink" = "11" ]]; then - _msg "Can not do both soft and hard link" + _err "Can not do both soft and hard link" exit 1 fi @@ -294,7 +297,7 @@ _remove() { CLIPBOARD=$( echo "${!i}" | tr -d './' | tr -cd '[[:print:]]' ) if [[ ! -e "$STORAGE/${CLIPBOARD}" ]];then - _msg "No such clipboard: '${CLIPBOARD}'" + _err "No such clipboard: '${CLIPBOARD}'" continue fi _list l "$CLIPBOARD" @@ -302,7 +305,7 @@ _remove() { read foo while read f; do if [[ ! -e "$f" ]]; then - _msg "No such path: '$f'" + _err "No such path: '$f'" continue fi echo "Removing SOURCE data: ${f}" @@ -321,7 +324,7 @@ _delete() { # name for (( i=2; i<=$#; i++ )); do name=$( basename "${!i}" ) if [[ ! -e "$STORAGE/$name" ]]; then - _msg "No such clipboard '$name'" + _err "No such clipboard '$name'" EC=1 continue fi @@ -343,8 +346,8 @@ _delete_all() { mkdir "$STORAGE" } -_msg() { - echo "$@" +_err() { + echo "${HERR}$@${NO}" } _gui() { @@ -355,11 +358,16 @@ _gui() { CLIPBOARD=mc fileargs=$(( $# - 1 )) + softlink=0 + hardlink=0 + keeplink=1 + movelink=0 + while :; do _list l $CLIPBOARD echo "$fileargs files selected" - read -s -p "(q)uit/(c)opy/(p)aste/(d)elete/(e)dit/(b)oard" -n 1 key + read -s -p "(q)uit/(c)opy/(p)aste/(d)elete/(e)dit/(b)oard/(o)pts" -n 1 key echo $'\n' if [[ "$key" = "q" ]]; then break @@ -369,12 +377,11 @@ _gui() { break fi if [[ "$key" = "p" ]]; then - _paste + _paste_clipboard "$CLIPBOARD" break fi if [[ "$key" = "d" ]]; then _delete d mc - break fi if [[ "$key" = "e" ]]; then _edit e mc @@ -385,9 +392,36 @@ _gui() { echo Change board: read -e -i "$CLIPBOARD" CLIPBOARD fi + if [[ "$key" = "o" ]]; then + _gui_opts + fi done } +_gui_opts() { + + while :; do + printf "============= +Options: + m move files: %s + s soft link: %s + l hard link: %s + d keep clipboard: %s + e exit + +Toggle option:\n" "$movelink" "$softlink" "$hardlink" "$keeplink" + read -s -n 1 optkey + + case "$optkey" in + m) movelink=$(( 1 - movelink )) ;; + s) softlink=$(( 1 - softlink )) ;; + l) hardlink=$(( 1 - hardlink )) ;; + d) keeplink=$(( 1 - keeplink )) ;; + e) break;; + esac + done + +} _get_completer() { self=$( basename $( readlink -f "$0" ) ) echo '_CCLIP_EXEC_complete() { @@ -415,6 +449,62 @@ complete -F _CCLIP_EXEC_complete CCLIP_EXEC ' | sed "s,CCLIP_EXEC,$self,g" exit 0 } +_qCol() { + # print "easier" mapping of ANSI colors and controls + local K="\033[1;30m" + local R="\033[1;31m" + local G="\033[1;32m" + local B="\033[1;34m" + local Y="\033[1;33m" + local M="\033[1;35m" + local C="\033[1;36m" + local W="\033[1;37m" + + local k="\033[2;30m" + local r="\033[2;31m" + local g="\033[2;32m" + local b="\033[2;34m" + local y="\033[2;33m" + local m="\033[2;35m" + local c="\033[2;36m" + local w="\033[2;37m" + + local bk="\033[40m" + local br="\033[41m" + local bg="\033[42m" + local by="\033[43m" + local bb="\033[44m" + local bm="\033[45m" + local bc="\033[46m" + local bw="\033[47m" + + local S='\033[1m' #strong + local s='\033[2m' #strong off + local U='\033[4m' #underline + local u='\033[24m' #underline off + local z='\033[0m' #zero colors + local Z='\033[0m' #zero colors + local ic='\033[7m' #inverse colors + local io='\033[27m' #inverse off + local st='\033[9m' #strike on + local so='\033[29m' #strike off + local CLR='\033[2J' # Clear screen + local CLREND='\033[K' # Clear to end of line + local CLRBEG='\033[1K' # Clear to beginning of line + local CLRLNE='\033[2K' # Clear the line + local CLRSCR="$CLR"'\033[0;0H' # Clear screen, reset cursor + + local color_keys=" K R G B Y M C W k r g b y m c w S s U u z Z \ + ic io st so bk br bg by bb bm bc bw \ + CLR CLREND CLRBEG CLRLNE CLRSCR " + + local arg val + for ((arg=1;arg<=$#;arg++)) { + val=${!arg} + [[ ${color_keys} = *" $val "* ]] || { echo "No such color code '${val}'" >&2; return 1; } + printf ${!val} + } +} for (( i=1; i<=$#; i++ )); do @@ -422,7 +512,6 @@ for (( i=1; i<=$#; i++ )); do [[ "${!i}" = "--help" ]] && _help done _load_config -source qolop &>/dev/null || true CMD="${1:-list}" CLIPBOARD=0 OUTPUT=.