summaryrefslogtreecommitdiffstats
path: root/clerk_fzf
diff options
context:
space:
mode:
authorRasmus Steinke <rasi@xssn.at>2016-07-21 12:36:40 +0200
committerRasmus Steinke <rasi@xssn.at>2016-07-21 12:36:40 +0200
commit26cf418509d002133cff33e57b4b7a4496c6f13f (patch)
treeffc888fa0535079ab95b1ee004de510b15d9cc31 /clerk_fzf
parentdbe6e11f569debcc213aa8154f5d4431832ad1bf (diff)
downloadperl-app-clerk-26cf418509d002133cff33e57b4b7a4496c6f13f.tar.gz
perl-app-clerk-26cf418509d002133cff33e57b4b7a4496c6f13f.tar.xz
add tmux+fzf variant
Diffstat (limited to 'clerk_fzf')
-rwxr-xr-xclerk_fzf/clerk_fzf208
-rwxr-xr-xclerk_fzf/clerk_help25
-rwxr-xr-xclerk_fzf/clerk_starter21
-rwxr-xr-xclerk_fzf/clerk_update31
-rw-r--r--clerk_fzf/tmux.clerk130
5 files changed, 415 insertions, 0 deletions
diff --git a/clerk_fzf/clerk_fzf b/clerk_fzf/clerk_fzf
new file mode 100755
index 0000000..60604ea
--- /dev/null
+++ b/clerk_fzf/clerk_fzf
@@ -0,0 +1,208 @@
+#!/usr/bin/env bash
+
+TMUX_TMPDIR="/tmp/clerk/tmux"
+
+script="$0 $1"
+random_artist="albumartist"
+shuf=$([[ "$OSTYPE" == "darwin"* ]] && echo 'gshuf' || echo 'shuf')
+value=20
+# table customization
+track_width="2"
+title_width="40"
+artist_width="50"
+date_width="6"
+# album is much longer in order to keep filename off screen
+album_width="200"
+
+pl_title_width="110"
+
+
+albums () {
+ script="$0 --albums"
+ tracklist="$(cat ~/.config/clerk/albums.cache | fzf --no-sort -m -e --reverse -i --with-nth=1,2,3 -d '\t' --tabstop=4 +s --ansi --bind "ctrl-a:select-all,ctrl-n:deselect-all")"
+ val=$?
+ if [[ $val -eq 130 ]]; then
+ exit
+ fi
+ track="$(echo "${tracklist}" | gawk -F "\t" '{print $NF}')"
+ actions
+}
+
+latest () {
+ script="$0 --latest"
+ tracklist=$(cat ~/.config/clerk/latest.cache | fzf --no-sort -m -e --reverse -i --with-nth=1,2,3 -d '\t' --tabstop=4 +s --ansi --bind "ctrl-a:select-all,ctrl-n:deselect-all")
+ val=$?
+ if [[ $val -eq 130 ]]; then
+ exit
+ fi
+ track="$(echo "${tracklist}" | gawk -F "\t" '{print $NF}')"
+ actions
+}
+
+tracks () {
+ script="$0 --tracks"
+ tracklist=$(cat ~/.config/clerk/tracks.cache | fzf -m --reverse -i -e --with-nth=1,2,3,4,5 -d '\t' --tabstop=4 +s --ansi --bind "ctrl-a:select-all,ctrl-n:deselect-all")
+ val=$?
+ if [[ $val -eq 130 ]]; then
+ exit
+ fi
+ if [[ $tracklist == "< Go to Albumlist"* ]]; then
+ latest
+ fi
+ track="$(echo "${tracklist}" | gawk -F "\t" '{print $NF}')"
+ actions
+}
+
+actions () {
+ action=$(echo -e "1. Replace\n2. Add\n3. Insert" | fzf --reverse -i --header="Choose Action or quit with ESC" --inline-info)
+
+ if [[ $action == "1. Replace" ]]; then
+ mpc clear
+ echo "${track}" | while read line; do
+ mpc add "${line}"
+ done
+ mpc play > /dev/null
+ elif [[ $action == "2. Add" ]]; then
+ echo "${track}" | while read line; do
+ mpc add "${line}"
+ done
+ elif [[ $action == "3. Insert" ]]; then
+ echo "${track}" | while read line; do
+ mpc insert "${line}"
+ done
+ fi
+ tmux findw -t music queue
+ $(${script})
+}
+
+playRandomAlbum () {
+ mpc clear > /dev/null
+ artist="$(mpc list "albumartist" | $shuf -n 1)"
+ album="$(mpc list album "albumartist" "$artist" | $shuf -n 1)"
+ mpc findadd album "$album" "albumartist" "$artist";
+ mpc play > /dev/null
+ tmux findw -t music queue
+}
+
+# same for tracks, no artist should be preferred because it has more tracks.
+playRandomTracks () {
+ mpc clear > /dev/null
+ artist="$(mpc list "$random_artist" | $shuf -n 1)"
+ album="$(mpc list album "$random_artist" "$artist" | $shuf -n 1)"
+ title="$(mpc list title album "$album" "$random_artist" "$artist" | $shuf -n 1)"
+ mpc findadd album "$album" "$random_artist" "$artist" title "$title";
+ mpc play > /dev/null
+
+ n=0; while (( n++ < $value -1 )); do
+ artist="$(mpc list "$random_artist" | $shuf -n 1)"
+ album="$(mpc list album "$random_artist" "$artist" | $shuf -n 1)"
+ title="$(mpc list title album "$album" "$random_artist" "$artist" | $shuf -n 1)"
+ mpc findadd album "$album" "$random_artist" "$artist" title "$title"
+ done
+ mpc play > /dev/null
+ tmux findw -t music queue
+}
+
+fix_date_format() {
+ # provided by Thorsten Wißmann:
+ # https://github.com/t-wissmann/dotfiles/blob/master/menu/rofi-mpd.sh
+ # reformats the date given in column $1 from format %c of locale LC_TIME=C
+ # (see man strftime) to the format specified in argument $2 column indices
+ # start with 1
+ column_index="$1"
+ gawk -F $'\t' '
+ # parse a date which was formated using %c to unix time
+ BEGIN {
+ # generated by the following bash one-liner:
+ # for i in {1..12} ; do LC_TIME=C date -d "1972-$i-01" +month2num[\"%b\"]\ =\ %_m ; done
+ month2num["Jan"] = 1
+ month2num["Feb"] = 2
+ month2num["Mar"] = 3
+ month2num["Apr"] = 4
+ month2num["May"] = 5
+ month2num["Jun"] = 6
+ month2num["Jul"] = 7
+ month2num["Aug"] = 8
+ month2num["Sep"] = 9
+ month2num["Oct"] = 10
+ month2num["Nov"] = 11
+ month2num["Dec"] = 12
+ }
+ function reformat_c_date(str) {
+ monthname = gensub(/^([^ ]*)[ ]+([^ ]*)[ ]+([^ ]*)[ ]+([^ ]*)[ ]+([^ ]*)$/, "\\2", "g", str)
+ monthnum = month2num[monthname]
+ nicedate = gensub(/^([^ ]*)[ ]+([^ ]*)[ ]+([^ ]*)[ ]+([^ ]*)[ ]+([^ ]*)$/, "\\5 " monthnum " \\3 \\4", "g", str)
+ nicedate = gensub(/:/, " ", "g", nicedate)
+ return nicedate
+ }
+ {
+ # modify the $column_index-th field
+ $'"$1"' = strftime("'"$2"'", mktime(reformat_c_date($'"$1"')))
+ print $0
+ }
+ '
+}
+
+
+updateCache () {
+ cd $HOME/.config/clerk
+ rm -f *.cache
+
+ mpc --format '%mtime%\t[%albumartist%|%artist%] ○ (%date%) ○ %album% [(CD %disc%)] ○ %file%' \
+ search filename '' \
+ | fix_date_format 1 '%Y-%m-%d-%H:%M:%S' \
+ | sort -nr \
+ | gawk '{for (i=2; i<NF; i++) printf $i " "; print $NF }' \
+ | perl -ne 'print unless $seen{$_}++' \
+ | sed 's:/[^/]*$::' | gawk -F ' ○ ' '!seen[$1 $2 $3 $4]++' \
+ | uniq \
+ | gawk -F ' ○ ' '{ printf "%."'${artist_width}'"s\t%."'${date_width}'"s\t%."'${album_width}'"s\t%.300s\n", $1, $2, $3, $4 }' \
+ | column -s $'\t' -t -o $'\t' > $HOME/.config/clerk/latest.cache
+ mpc --format '[%albumartist%|%artist%]\t(%date%)\t%album% [(CD %disc%)]\t%file%' \
+ search filename '' \
+ | sed 's:/[^/]*$::' \
+ | gawk -F '\t' '!seen[$1 $2 $3 $4]++' \
+ | sort \
+ | gawk -F '\t' '{ printf "%."'${artist_width}'"s\t%."'${date_width}'"s\t%."'${album_width}'"s\t%.300s\n", $1, $2, $3, $4 }' \
+ | column -t -s $'\t' -o $'\t' > $HOME/.config/clerk/albums.cache
+ mpc --format '%track%\t%title%\t%artist%\t(%date%)\t%album%\t%file%' \
+ search filename '' \
+ | gawk -F '\t' '{ printf "%."'${track_width}'"s\t%."'${title_width}'"s\t%."'${artist_width}'"s\t%."'${date_width}'"s\t%."'${album_width}'"s\t%.500s\n", $1, $2, $3, $4, $5, $6 }' \
+ | column -s $'\t' -t -o $'\t' \
+ > $HOME/.config/clerk/tracks.cache
+ date=$(mpc stats | grep 'DB Updated: ')
+ file="$HOME/.config/clerk/.lastupdate"
+ echo "${date}" > "${file}"
+# | gawk -F '\t' '{ printf "%.2s\t%.40s\t%.40s\t%.200s\t%.500s\n", $1, $2, $3, $4, $5, $6 }' \
+ if [[ $forceupdate == "true" ]]; then
+ exit
+ fi
+}
+if [[ $1 == "--update" ]]; then
+ :
+else
+ date=$(mpc stats | grep 'DB Updated: ')
+ file="$HOME/.config/clerk/.lastupdate"
+ if [ "$(< $file)" = "$date" ] && [ -f "$file" ] ; then
+ :
+ else
+ updateCache
+ date=$(mpc stats | grep 'DB Updated: ')
+ echo "${date}" > "${file}"
+ fi
+fi
+
+
+if [[ $1 == "--albums" ]]; then
+ albums
+elif [[ $1 == "--tracks" ]]; then
+ tracks
+elif [[ $1 == "--latest" ]]; then
+ latest
+elif [[ $1 == "--random_album" ]]; then
+ playRandomAlbum
+elif [[ $1 == "--random_tracks" ]]; then
+ playRandomTracks
+elif [[ $1 == "--update" ]]; then
+ forceupdate=true updateCache
+fi
diff --git a/clerk_fzf/clerk_help b/clerk_fzf/clerk_help
new file mode 100755
index 0000000..1b7a478
--- /dev/null
+++ b/clerk_fzf/clerk_help
@@ -0,0 +1,25 @@
+#!/bin/bash
+
+echo "Clerk Hotkeys"
+echo "-------------"
+echo " "
+echo "F1: show album list"
+echo "F2: show track list"
+echo "F3: show latest list"
+echo "F4: show queue"
+echo "F5: previous track"
+echo "F6: toggle playback"
+echo "F7: stop playback"
+echo "F8: next track"
+echo "F9: play random album"
+echo "F10: play random tracks"
+echo "F12: reload caches"
+echo "Shift+F21: re-create caches"
+echo "Ctrl+h: this help"
+echo " "
+echo " "
+echo "Hotkeys for lists"
+echo "-----------------"
+echo " "
+echo "Tab: mark selection"
+echo "Return: show menu for selection"
diff --git a/clerk_fzf/clerk_starter b/clerk_fzf/clerk_starter
new file mode 100755
index 0000000..811262f
--- /dev/null
+++ b/clerk_fzf/clerk_starter
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+export TMUX_TMPDIR="/tmp/clerk/tmux"
+
+if [[ ! -d "${TMUX_TMPDIR}" ]]; then
+ mkdir -p "${TMUX_TMPDIR}"
+fi
+
+tmux has-session -t music
+if [ $? != 0 ]; then
+ tmux -f $HOME/.config/clerk/tmux.conf new-session -s music -n albums -d
+ tmux send-keys -t music 'clerk_fzf --albums' C-m
+ tmux new-window -t music -n tracks
+ tmux send-keys -t music 'clerk_fzf --tracks' C-m
+ tmux new-window -t music -n latest
+ tmux send-keys -t music 'clerk_fzf --latest' C-m
+ tmux new-window -t music -n queue
+ tmux send-keys -t music 'ncmpcpp' C-m
+fi
+
+tmux attach -t music
diff --git a/clerk_fzf/clerk_update b/clerk_fzf/clerk_update
new file mode 100755
index 0000000..8466b45
--- /dev/null
+++ b/clerk_fzf/clerk_update
@@ -0,0 +1,31 @@
+#!/bin/bash
+
+TMUX_TMPDIR="/tmp/clerk/tmux"
+
+screen=$(tmux list-panes -F "#W")
+if [[ $1 == "--create-caches" ]]; then
+ tmux new-session -s notify -n 'Updating Cache Files. Please wait a moment' -d
+ tmux switchc -t notify
+ clerk_fzf --update &
+ pid=$!
+ tmux send-keys -t notify 'sl' C-m
+ wait $pid
+fi
+
+tmux new-session -s music2 -n albums -d
+tmux send-keys -t music2 'clerk_fzf --albums' C-m
+tmux new-window -t music2 -n tracks
+tmux send-keys -t music2 'clerk_fzf --tracks' C-m
+tmux new-window -t music2 -n latest
+tmux send-keys -t music2 'clerk_fzf --latest' C-m
+tmux new-window -t music2 -n queue
+tmux send-keys -t music2 'ncmpcpp' C-m
+
+tmux rename-session -t music music_old
+tmux rename-session -t music2 music
+tmux switchc -t music
+tmux findw -t music ${screen}
+tmux kill-session -t music_old
+if [[ $1 == "--create-caches" ]]; then
+ tmux kill-session -t notify
+fi
diff --git a/clerk_fzf/tmux.clerk b/clerk_fzf/tmux.clerk
new file mode 100644
index 0000000..5468707
--- /dev/null
+++ b/clerk_fzf/tmux.clerk
@@ -0,0 +1,130 @@
+###########################
+# Configuration
+###########################
+# set titles
+set -g set-titles on
+set -g set-titles-string '#T'
+
+# use 256 term for pretty colors
+set -g default-terminal "screen-256color"
+
+# increase scroll-back history
+set -g history-limit 5000
+
+# use vim key bindings
+setw -g mode-keys vi
+
+# disable mouse
+#setw -g mode-mouse off
+
+# decrease command delay (increases vim responsiveness)
+set -sg escape-time 1
+
+# increase repeat time for repeatable commands
+set -g repeat-time 1000
+
+# start window index at 1
+set -g base-index 1
+
+# start pane index at 1
+setw -g pane-base-index 1
+
+# highlight window when it has new activity
+#setw -g monitor-activity on
+#set -g visual-activity on
+
+# re-number windows when one is closed
+set -g renumber-windows on
+
+###########################
+# Key Bindings
+###########################
+
+# tmux prefix
+unbind C-b
+set -g prefix C-a
+
+# create 'v' alias for selecting text
+bind-key -t vi-copy v begin-selection
+
+# paste
+unbind C-p
+bind C-p paste-buffer
+
+# window splitting
+unbind %
+bind | split-window -h
+unbind '"'
+bind - split-window -v
+
+# resize panes
+bind -r H resize-pane -L 5
+bind -r J resize-pane -D 5
+bind -r K resize-pane -U 5
+bind -r L resize-pane -R 5
+
+# quickly switch panes
+unbind ^J
+bind ^J select-pane -t :.+
+
+# force a reload of the config file
+unbind r
+bind r source-file ~/.tmux.conf \; display "Reloaded!"
+
+###########################
+# Status Bar
+###########################
+set-option -g status-position top
+
+# enable UTF-8 support in status bar
+#set -g status-utf8 on
+
+# set refresh interval for status bar
+set -g status-interval 30
+
+# center the status bar
+set -g status-justify centre
+
+# show session, window, pane in left status bar
+set -g status-left-length 40
+set -g status-left ''
+set -g status-right ''
+
+###########################
+# Colors
+###########################
+
+# color status bar
+set -g status-bg colour235
+#set -g status-fg white
+
+#set -g status-bg default
+set -g status-fg default
+setw -g window-status-current-bg default
+setw -g window-status-current-fg default
+setw -g window-status-current-attr dim
+setw -g window-status-bg default
+setw -g window-status-fg white
+setw -g window-status-attr bright
+setw -g window-status-format ' #[fg=colour243,bold]#W '
+setw -g window-status-current-format ' #[fg=yellow,bold]#[bg=colour235]#W '
+
+# set color of active pane
+#set -g pane-border-fg colour235
+#set -g pane-border-bg black
+#set -g pane-active-border-fg green
+#set -g pane-active-border-bg black
+
+bind-key -n F1 findw albums
+bind-key -n F2 findw tracks
+bind-key -n F3 findw latest
+bind-key -n F4 findw queue
+bind-key -n F5 run-shell 'mpc prev --quiet'
+bind-key -n F6 run-shell 'mpc toggle --quiet'
+bind-key -n F7 run-shell 'mpc stop --quiet'
+bind-key -n F8 run-shell 'mpc next --quiet'
+bind-key -n S-F12 run-shell 'clerk_fzf --create-caches'
+bind-key -n F12 run-shell 'clerk_update'
+bind-key -n F9 run-shell 'clerk_fzf --random_album'
+bind-key -n F10 run-shell 'clerk_fzf --random_tracks'
+bind-key -n C-h run-shell 'clerk_help'