]> Sergey Matveev's repositories - nnn.git/commitdiff
Support file name copier
authorArun Prakash Jana <engineerarun@gmail.com>
Thu, 30 Mar 2017 15:47:00 +0000 (21:17 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Thu, 30 Mar 2017 17:30:15 +0000 (23:00 +0530)
Add keybind 'Ctrl-k' to copy the absolute path of the file under the cursor if environment variable NOICE_COPIER is set.

README.md
config.def.h
noice.c

index 775fd6d5b4f9a2e2b1cb181a6d8cbd31ba4e364a..5912a98284d0aa7b53b64f8d50de7f5638a4531e 100644 (file)
--- a/README.md
+++ b/README.md
@@ -13,6 +13,7 @@ A fork of the [noice](http://git.2f30.org/noice/) file browser to make it more f
 - [Keyboard shortcuts](#keyboard-shortcuts)
 - [File type abbreviations](#file-type-abbreviations)
 - [Help](#help)
+- [Copy current file path to clipboard](#copy-current-file-path-to-clipboard)
 - [Change file associations](#change-file-associations)
 
 ### Introduction
@@ -54,8 +55,9 @@ I chose to fork noice because:
   - Case-insensitive alphabetic content listing instead of upper case first
   - Roll over at the first and last entries of a directory (with Up/Down keys)
   - Sort entries by file size (largest to smallest)
+  - Shortcut to invoke file name copier (set using environment variable `NOICE_COPIER`)
 - File associations
-  - Environment variable `NOICE_OPENER` to override all associations and open all files with your desktop environments default file opener. Examples:
+  - Environment variable `NOICE_OPENER` to override all associations and open all files with your desktop environment's default file opener. Examples:
 
         export NOICE_OPENER=xdg-open
         export NOICE_OPENER=gnome-open
@@ -113,6 +115,7 @@ Start noice (default: current directory):
 | `e` | edit entry in `vim` |
 | `p` | open entry with `less` pager |
 | `z` | run `top` |
+| `Ctrl-k` | invoke file name copier |
 | `Ctrl-l` | redraw window |
 | `q` | quit noice |
 
@@ -134,6 +137,22 @@ The following abbreviations are used in the detail view:
 
     $ man noice
 
+### Copy current file path to clipboard
+
+noice can pipe the absolute path of the current file to a copier script. For example, you can use `xsel` on Linux or `pbcopy` on OS X.
+
+Sample Linux copier script:
+
+    #!/bin/sh
+
+    echo -n $1 | xsel --clipboard --input
+
+export `NOICE_OPENER`:
+
+    export NOICE_COPIER="/home/vaio/copier.sh"
+
+Start noice and use `Ctrl-k` to copy the absolute path (from `/`) of the file under the cursor to clipboard.
+
 ### Change file associations
 
 If you want to set custom applications for certain mime types, or change the ones set already (e.g. vim, fmedia, zathura), modify the `assocs` structure in [config.def.h](https://github.com/jarun/noice/blob/master/config.def.h) (it's easy). Then re-compile and install.
index 9c6ae7e81f23eaac292afad8221a484977ca4052..7ec98effbdebcc153a02bb5613185975cdfc8b8e 100644 (file)
@@ -71,6 +71,8 @@ struct key bindings[] = {
        /* Toggle sort by time */
        { 't',            SEL_MTIME },
        { CONTROL('L'),   SEL_REDRAW },
+       /* Copy currently selected file path */
+       { CONTROL('K'),   SEL_COPY },
        /* Run command */
        { 'z',            SEL_RUN, "top" },
        { '!',            SEL_RUN, "sh", "SHELL" },
diff --git a/noice.c b/noice.c
index 08bca9ea4fc4cad417047026a5b0d45f31cb7d3a..6f64b5e821d801bfce6e4eacdf8bcb5f3f69f5bb 100644 (file)
--- a/noice.c
+++ b/noice.c
@@ -68,6 +68,7 @@ enum action {
        SEL_FSIZE,
        SEL_MTIME,
        SEL_REDRAW,
+       SEL_COPY,
        SEL_RUN,
        SEL_RUNARG,
 };
@@ -94,6 +95,7 @@ int ndents, cur;
 int idle;
 char *opener = NULL;
 char *fallback_opener = NULL;
+char *copier = NULL;
 char size_buf[12]; /* Buffer to hold human readable size */
 const char* size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
 
@@ -884,6 +886,19 @@ nochange:
                        if (ndents > 0)
                                mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
                        goto begin;
+               case SEL_COPY:
+                       if (copier && ndents) {
+                               char abspath[PATH_MAX];
+
+                               if (strcmp(path, "/") == 0)
+                                       snprintf(abspath, PATH_MAX, "/%s", dents[cur].name);
+                               else
+                                       snprintf(abspath, PATH_MAX, "%s/%s", path, dents[cur].name);
+                               spawn(copier, abspath, NULL);
+                               printmsg(abspath);
+                       } else if (!copier)
+                                       printmsg("NOICE_COPIER is not set");
+                       goto nochange;
                case SEL_RUN:
                        run = xgetenv(env, run);
                        exitcurses();
@@ -950,6 +965,9 @@ main(int argc, char *argv[])
        /* Get the fallback desktop mime opener, if set */
        fallback_opener = getenv("NOICE_FALLBACK_OPENER");
 
+       /* Get the default copier, if set */
+       copier = getenv("NOICE_COPIER");
+
        signal(SIGINT, SIG_IGN);
 
        /* Test initial path */