]> Sergey Matveev's repositories - nnn.git/commitdiff
Add plugin cmusq (#1010)
authorKabouik <7107523+Kabouik@users.noreply.github.com>
Sat, 15 May 2021 18:06:46 +0000 (18:06 +0000)
committerArun Prakash Jana <engineerarun@gmail.com>
Sat, 15 May 2021 18:45:48 +0000 (00:15 +0530)
* Fix conflict with #1006

* Queue/play in cmus player

* Remove leftover comments

* start_cmus function, optional xdotool dependency, better process waiting

* start_cmus function, better process waiting, optional xdotool dep

* Merge conflicts

* Better reporting of past actions

* Discriminate newly started queue and existing queue

* Harmonize descriptions, rename cmusqueue to cmusq, clean cmusq code

* Remove cmusqueue

* Exit if cmus missing and style changes

Co-authored-by: luukvbaal <31730729+luukvbaal@users.noreply.github.com>
plugins/README.md
plugins/cmusq [new file with mode: 0755]
plugins/fzplug
plugins/unmount-parent

index 7e7254271440d732b62c0a5f4dce70ff724ee356..f217495c5d9ae0cadc70a333fc174e042815dbcd 100644 (file)
@@ -20,6 +20,7 @@ Plugins extend the capabilities of `nnn`. They are _executable_ scripts (or bina
 | [cdpath](cdpath) | `cd` to the directory from `CDPATH` | sh | fzf |
 | [chksum](chksum) | Create and verify checksums [✓] | sh | md5sum,<br>sha256sum |
 | [cleanfilename](cleanfilename) | Clean filename to be more shell-friendly [✓] | bash | sed |
+| [cmusq](cmusq) | Queue files/dirs in cmus player | sh | cmus, pgrep | 
 | [diffs](diffs) | Diff for selection (limited to 2 for directories) [✓] | sh | vimdiff, mktemp |
 | [dragdrop](dragdrop) | Drag/drop files from/into nnn | sh | [dragon](https://github.com/mwh/dragon) |
 | [dups](dups) | List non-empty duplicate files in current dir | bash | find, md5sum,<br>sort uniq xargs |
diff --git a/plugins/cmusq b/plugins/cmusq
new file mode 100755 (executable)
index 0000000..aa67cb6
--- /dev/null
@@ -0,0 +1,79 @@
+#!/usr/bin/env sh
+
+# Description: Add selection or hovered file/directory to cmus queue
+#
+# Dependencies: cmus, pgrep, xdotool (optional)
+#
+# Notes:
+#   1. If adding selection, files/dirs are added in the same order they were selected in nnn
+#   2. A new window will be opened if cmus is not running already, playback will start immediately
+#   3. If cmus is already running, files will be appended to the queue with no forced playback
+#
+# TODO:
+#   1. Add cava and cmus-lyrics as optional dependencies
+#   2. Start cava and/or cmus-lyrics in tmux or kitty panes next to cmus
+#
+# Shell: POSIX compliant
+# Author: Kabouik
+
+# (Optional) Set preferred terminal emulator for cmus if not set in your env,
+# or leave commented out to use OS default
+#TERMINAL="kitty"
+
+if ! type cmus >/dev/null; then
+    printf "cmus missing"
+    read -r _
+fi
+
+selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
+
+start_cmus() {
+    type xdotool >/dev/null && nnnwindow="$(xdotool getactivewindow)"
+    case "$TERMINAL" in
+        kitty | gnome-terminal | st)
+            nohup "$TERMINAL" -- cmus & ;;
+        havoc)
+            nohup "$TERMINAL" cmus & ;;
+        "")
+            nohup x-terminal-emulator -e cmus & ;;
+        *)
+            nohup "$TERMINAL" -e cmus & ;;
+    esac
+    # Give the new terminal some time to open
+    until pidof cmus >/dev/null; do sleep 0.1; done
+    [ -n "$nnnwindow" ] && xdotool windowactivate "$nnnwindow"
+} >/dev/null 2>&1
+
+fill_queue() {
+  if [ "$REPLY" = "s" ]; then
+      xargs < "$selection" -0 cmus-remote -q
+  elif [ -n "$1" ]; then
+      cmus-remote -q "$1"
+  fi
+}
+
+# If active selection,then ask what to do
+if [ -s "$selection" ]; then
+    printf "Queue [s]election or [c]urrently hovered? [default=c]: "
+    read -r REPLY
+fi
+
+# If cmus is not running, start and play queue
+if ! pgrep cmus >/dev/null; then
+    printf "cmus is not running, starting it in a new %s window.\n" "$TERMINAL"
+    start_cmus
+    fill_queue "$1"
+    cmus-remote -p
+    printf "Files added to cmus queue.\n"
+else # Append to existing queue if cmus is already running
+    fill_queue "$1"
+    printf "Files appended to current cmus queue.\n"
+fi
+
+# Change view
+cmus-remote -C "view 4"
+
+# Clear selection
+if [ -p "$NNN_PIPE" ]; then
+    printf "-" > "$NNN_PIPE"
+fi
index c2ad762211f303fa0b6750ab211a768757e9a5f1..6fcff416b5fe1fd237dd0e43a649a52990bc25a3 100755 (executable)
@@ -17,6 +17,7 @@
 # Author: Kabouik
 
 # Optional scripts sources
+
 # Leave blank or fill with the absolute path of a folder containing executable
 # scripts other than nnn plugins (e.g., "$HOME/.local/share/nautilus/scripts",
 # since there are numerous Nautilus script git repositories).
@@ -29,7 +30,7 @@ CUSTOMDIR2=""
 nnnpluginsdir="$HOME/.config/nnn/plugins"
 
 # Preview with bat if installed
-if [ -z "$(command -v bat)" ]; then
+if type bat >/dev/null; then
     plugin=$(find "$nnnpluginsdir" "$CUSTOMDIR1" "$CUSTOMDIR2" \
         -maxdepth 3 -perm -111 -type f 2>/dev/null | fzf --ansi --preview \
         "cat {}" \
@@ -52,7 +53,7 @@ if ! [ "$plugin" = "" ]; then
 fi
 
 # If attempt with hovered file fails, try without any target
-# (nnn selections should still be passed to the script inhat case)
+# (nnn selections should still be passed to the script in that case)
 if [ "$err" -eq "1" ]; then
     clear && "$plugin" || err=2
 fi
index 3a89f650671113e11720d27f9fb6e19793e99194..d148fca165d6b8f8297a03fb8a83153771cf8469 100755 (executable)
@@ -8,12 +8,13 @@
 #
 # Dependencies: fusermount
 #
+
 # Shell: POSIX compliant
 # Authors: Kabouik & 0xACE
 #
 # TODO:
 # - Avoid lazy unmount by forcing nnn context to leave the subfolder before fusermount.
-#   Tried `printf "%s" "0c$m" > "$NNN_PIPE"` but it breaks the nnn interfacee, see #854.
+#   Tried `printf "%s" "0c$m" > "$NNN_PIPE"` but it breaks the nnn interface, see #854.
 
 err=0
 m=$HOME/.config/nnn/mounts