diff --git a/scripts/main.sh b/scripts/main.sh index bb81eae..345e296 100755 --- a/scripts/main.sh +++ b/scripts/main.sh @@ -9,6 +9,7 @@ source "./utils/clipboard.sh" source "./utils/cmd.sh" source "./utils/op.sh" source "./utils/prompt.sh" +source "./utils/recent.sh" source "./utils/semver.sh" source "./utils/spinner.sh" source "./utils/tmux.sh" @@ -20,6 +21,7 @@ source "./options.sh" main() { local -r ACTIVE_PANE="$1" + local op_items local items local selected_item local selected_item_name @@ -45,7 +47,8 @@ main() { fi spinner::start "Fetching items" - items="$(op::get_all_items)" + op_items="$(op::get_all_items)" + items=$(recent::get_all_items "$op_items") spinner::stop synchronize_panes_reset_value=$(tmux::disable_synchronize_panes) @@ -78,6 +81,8 @@ main() { ;; esac + recent::add "$selected_item_name" + if options::copy_to_clipboard; then # Copy password to clipboard diff --git a/scripts/utils/recent.sh b/scripts/utils/recent.sh new file mode 100755 index 0000000..886443a --- /dev/null +++ b/scripts/utils/recent.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +# ------------------------------------------------------------------------------ + +declare -r OP_RECENT_FILE=~/.op_tmux_recent + +recent::file() { + echo "${OP_RECENT_FILE}_$(options::op_account)" +} + +recent::add() { + local recent=$(tmux::get_option "@1password-recent" "0") + local recents + + if [[ $recent -eq 0 ]]; then + rm -f "$(recent::file)" + else + echo $1 >> "$(recent::file)" + recents=$(cat "$(recent::file)") + echo "$recents" | tac | awk '!seen[$0]++' | tac | tail -n $recent > "$(recent::file)" + fi +} + +recent::get() { + local recent=$(tmux::get_option "@1password-recent" "0") + + if [[ $recent -eq 0 ]]; then + rm -f "$(recent::file)" + elif [ -f "$(recent::file)" ]; then + cat "$(recent::file)" | tac + fi +} + +recent::get_all_items() { + local op_items="$1" + local recent_item + local rencent_items=$(recent::get) + + if ! [[ -z "$rencent_items" ]]; then + while IFS= read recent_item; do + echo $(echo "$op_items" | grep "^$recent_item,") + op_items=$(echo "$op_items" | grep -v "^$recent_item,") + done <<< "$(echo -e "$rencent_items")" + fi + + echo "$op_items" +}