Skip to content

Commit

Permalink
Merge pull request #3 from Mange/xsel-support
Browse files Browse the repository at this point in the history
Support xsel in addition to xclip
  • Loading branch information
mattydebie committed Jan 2, 2019
2 parents 4ace861 + 8bc7e08 commit 97ba520
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BitWarden Rofi Menu

This is a work in progress to get the BitWarden cli functionality in an easy Rofi menu.
This is a work in progress to get the BitWarden cli functionality in an easy Rofi menu.
On selecting an entry, the password is copied to your clipboard for 5 seconds. During those 5 seconds, a notification is shown indicating which password you are copying at that time.

![bitwarden-rofi](img/screenshot1.png)
Expand All @@ -24,4 +24,4 @@ You can either execute the script from a terminal or by binding it to a key comb

- bitwarden-cli
- jq
- xclip
- xclip or xsel
73 changes: 64 additions & 9 deletions bwmenu
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ BW_HASH_FILE=~/.bwhash
# Holds the available items in memory
ITEMS=

# Stores which command will be used to deal with clipboards
CLIPBOARD_MODE=

# source the hash file to gain access to the BitWarden CLI
# Pre fetch all the items
load_items() {
Expand All @@ -15,14 +18,21 @@ load_items() {
if ! ITEMS=$(bw list items 2>/dev/null); then
mpw=$(rofi -dmenu -p "Master Password" -password) || exit $?
bw unlock "$mpw" | grep -e 'export.*=="' -o > $BW_HASH_FILE || {
rofi -e "Failed to unlock BitWarden.
exit_error 1 "Failed to unlock BitWarden.
Please try again."
exit 1
}
load_items
fi
}

exit_error() {
local code="$1"
local message="$2"

rofi -e "$message"
exit "$code"
}

# Show the Rofi menu with options
# Reads items from stdin
rofi_menu() {
Expand Down Expand Up @@ -84,10 +94,7 @@ show_folders() {

# re-sync the BitWarden items with the server
sync_bitwarden() {
bw sync &>/dev/null || {
rofi -e "Failed to sync bitwarden"
exit 1
}
bw sync &>/dev/null || exit_error 1 "Failed to sync bitwarden"

load_items
show_items
Expand All @@ -104,6 +111,53 @@ on_rofi_exit() {
esac
}

# Set $CLIPBOARD_MODE to a command that will put stdin into the clipboard.
select_copy_command() {
if hash xclip 2>/dev/null; then
CLIPBOARD_MODE=xclip
elif hash xsel 2>/dev/null; then
CLIPBOARD_MODE=xsel
else
exit_error 1 "No clipboard command found. Please install either xclip or xsel."
fi
}

clipboard-set() {
clipboard-${CLIPBOARD_MODE}-set
}

clipboard-get() {
clipboard-${CLIPBOARD_MODE}-get
}

clipboard-clear() {
clipboard-${CLIPBOARD_MODE}-clear
}

clipboard-xclip-set() {
xclip -selection clipboard -r
}

clipboard-xclip-get() {
xclip -selection clipboard -o
}

clipboard-xclip-clear() {
echo -n "" | xclip -selection clipboard -r
}

clipboard-xsel-set() {
xsel --clipboard --input
}

clipboard-xsel-get() {
xsel --clipboard
}

clipboard-xsel-clear() {
xsel --clipboard --delete
}

# Output the password
# copy to clipboard and give the user feedback that the password is copied
# $1: json item
Expand All @@ -113,11 +167,11 @@ show_password() {
# send a notification for 5 seconds (-t 5000)
# not sure if icon will be present everywhere, /usr/share/icons is default icon location
notify-send "<b>$(echo "$1" | jq -r '.name')</b> copied" "${pass:0:4}****" -t 5000 -i /usr/share/icons/hicolor/64x64/apps/bitwarden.png
echo "$pass" | xclip -selection clipboard -r
echo -n "$pass" | clipboard-set

sleep 5
if [ "$(xclip -selection clipboard -o)" = "$pass" ]; then
echo '' | xclip -selection clipboard -r
if [[ "$(clipboard-get)" == "$pass" ]]; then
clipboard-clear
fi
}

Expand All @@ -126,5 +180,6 @@ if ! [ -f $BW_HASH_FILE ]; then
chmod 600 $BW_HASH_FILE
fi

select_copy_command
load_items
show_items

0 comments on commit 97ba520

Please sign in to comment.