updated to use env for finding less app
[feisty_meow.git] / scripts / files / lesser.sh
1 #!/bin/bash
2
3 # runs the less command but with some additional options.
4 #
5 # there is a "code" option (-c) that formats the incoming data as if it were source code before
6 # displaying the output.  this works great on files that are actually recognized as source code,
7 # and not so great on other things like simple lists or error output.
8 function lesser()
9 {
10   local code_view_flag="$1"
11
12   EXTRA_OPTIONS=
13
14   if [ "$code_view_flag" == "-c" ]; then
15     # saw the 'code' flag, which means show the file with source highlighting.
16     shift
17
18     # we always pass the raw flag (-r) when in code view mode so special formatting is not lost.
19     # this may not work for some text terminals.
20     EXTRA_OPTIONS=-r
21
22   else
23     # drop the value and emptiness will mean don't show code.
24     unset code_view_flag
25   fi
26
27   if [ ! -z "$code_view_flag" ]; then
28     # establish a pipe for less to see our beloved syntax highlighting.
29     export LESSOPEN="| source-highlight -f esc -o STDOUT -i %s"
30   fi
31
32   # run the source highlighter first if needed.
33   /usr/bin/env less $EXTRA_OPTIONS "${@}" 
34 }
35
36 ##############
37
38 # now that we have our function, just use it on all the input parameters.
39 # this is done to avoid adding yet another function to the core.
40 lesser "${@}" 
41