Difference between revisions of "Default viewer script"

From Vifm Wiki
Jump to navigation Jump to search
(Initial version of defviewer script)
 
(Add syntax highlighting)
Line 3: Line 3:
 
This script can be used to view files for which no viewer is registered. Here is its source code:
 
This script can be used to view files for which no viewer is registered. Here is its source code:
  
    #!/bin/bash
+
<syntaxhighlight lang="bash">
   
+
#!/bin/bash
    if [ "$#" != 1 ]; then
+
 
        echo "Usage: $0 filename"
+
if [ "$#" -ne 1 ]; then
        exit 1
+
    echo "Usage: $0 filename"
    fi
+
    exit 1
   
+
fi
    # upper limit of lines to display for text files
+
 
    nlines=150
+
# upper limit of lines to display for text files
    # upper limit of bytes to display for binary files
+
nlines=150
    nbytes=2048
+
# upper limit of bytes to display for binary files
    # language of text files
+
nbytes=2048
    language=russian
+
# language of text files
    # output encoding for text files
+
language=russian
    encoding=utf-8
+
# output encoding for text files
   
+
encoding=utf-8
    info=$(head -$nlines "$1" | file --mime -)
+
 
    charset=${info#*=}
+
info=$(head -$nlines "$1" | file --mime -)
   
+
charset=${info#*=}
    if [ "x$charset" == "xbinary" ]; then
+
 
        hexdump -e '"%08_ax: "' -e '8/1 "%02x " "  " 8/1 "%02x "' -e '" |" 16/1 "%_p"' -e '"\n"' -v -n $nbytes "$1"
+
if [ "x$charset" == "xbinary" ]; then
    else
+
    hexdump -e '"%08_ax: "' -e '8/1 "%02x " "  " 8/1 "%02x "' -e '" |" 16/1 "%_p"' -e '"\n"' -v -n $nbytes "$1"
        head -$nlines "$1" | enconv -g -L $language -x $encoding
+
else
    fi
+
    head -$nlines "$1" | enconv -g -L $language -x $encoding
 +
fi
 +
</syntaxhighlight>
  
 
The script uses:
 
The script uses:
Line 41: Line 43:
 
# Save the script as <code>defviewer</code> at one of directories listed in your <code>$PATH</code> environment variable or under <code>$VIFM/scripts</code>(most likely that it's <code>~/.vifm/scripts</code>).
 
# Save the script as <code>defviewer</code> at one of directories listed in your <code>$PATH</code> environment variable or under <code>$VIFM/scripts</code>(most likely that it's <code>~/.vifm/scripts</code>).
 
# On Unix-like operating systems make the script file executable by running<br><code>[https://en.wikipedia.org/wiki/Chmod chmod] +x defviewer</code>
 
# On Unix-like operating systems make the script file executable by running<br><code>[https://en.wikipedia.org/wiki/Chmod chmod] +x defviewer</code>
 +
 
# Add the following line to your *vifmrc*:<br><code>fileview * defviewer %c</code>
 
# Add the following line to your *vifmrc*:<br><code>fileview * defviewer %c</code>
  

Revision as of 11:09, 17 July 2014

Default viewer script

This script can be used to view files for which no viewer is registered. Here is its source code:

#!/bin/bash

if [ "$#" -ne 1 ]; then
    echo "Usage: $0 filename"
    exit 1
fi

# upper limit of lines to display for text files
nlines=150
# upper limit of bytes to display for binary files
nbytes=2048
# language of text files
language=russian
# output encoding for text files
encoding=utf-8

info=$(head -$nlines "$1" | file --mime -)
charset=${info#*=}

if [ "x$charset" == "xbinary" ]; then
    hexdump -e '"%08_ax: "' -e '8/1 "%02x " "  " 8/1 "%02x "' -e '" |" 16/1 "%_p"' -e '"\n"' -v -n $nbytes "$1"
else
    head -$nlines "$1" | enconv -g -L $language -x $encoding
fi

The script uses:

  1. head utility to process only first $nlines of file.
  2. file tool to determine whether file is of binary format or not.
  3. hexdump tool to display binary files in classic hex dump form.
  4. enconv program for automatic conversion to the $encoding charset for text files trying to guess source encoding of $language.

Plugging the script into vifm

To use it in vifm:

  1. Save the script as defviewer at one of directories listed in your $PATH environment variable or under $VIFM/scripts(most likely that it's ~/.vifm/scripts).
  2. On Unix-like operating systems make the script file executable by running
    chmod +x defviewer
  1. Add the following line to your *vifmrc*:
    fileview * defviewer %c

Here is what it looks like:

Hex-dump-screenshot.png