2

Using source-highlight in conjunction with less works really well, but I'm struggling to see how I can use it with git show because there's no file extension to clue source-highlight about the language used in the git output...

less somefile.rb # result is syntax colourised
git show master:somefile.rb | less # no colouring

Using calling git show master:somefile.rb | less is effectively the same as calling less somefile (ie no .rb); because there's no extension, there's no way for source-highlight to guess the syntax.

Is there a non-extension way for source-highlight to guess, or can I pass a --lang-def option into the LESSOPEN variable somehow?

Edit 1 Ah, so it looks like source-highlight can use other methods to infer the language but I don't have any of those in my source files.

  • @goro thanks for the styling edits, but please don't try and copyedit the body of my question. I'm happy with the language I've used. – jaygooby Sep 19 '18 at 12:22
  • You can't both programs are independent of each other! – user90704 Sep 19 '18 at 12:45
  • @TNT, thanks I'm very aware that they are two different programs, but through the miraculous use of unix pipes, you can output the content of one, to the other. And in this case, I'd quite like to syntax colourise git show's output – jaygooby Sep 19 '18 at 13:41
1

source-highlight can guess the language without using the extension. See 6.1 How the input language is discovered.

Unfortunately, that feature requires that the input is a regular file. When I try to use it to recognize stdin, I get this error:

source-highlight: missing feature: language inference requires input file

So it's not available in the middle of a pipeline, which is how it would be invoked when formatting git output.

git does have some options for configuring its pager -- see the documentation for core.pager and pager.<cmd>. But there's no mechanism for passing on additional information (such as the filename) to the pager -- git-show only has formatting options when showing commits. The "pager command" you specify would have to be an additional wrapper script, that performs the following steps:

  1. reads the entirety of the input and saves it in a temporary file
  2. calls source-highlight ... on that file
  3. pipes the output to your actual pager

And this would still fail on files that don't have an appropriate "sha-bang" to identify them.

  • I'm veering towards creating a shell function wrapper for src-hilite-lesspipe.sh so I can pass a --lang-def as part of the $@ args that are fed to it, and then to source-highlight itself – jaygooby Sep 19 '18 at 13:50
  • 1
    src-hilite-lesspipe.sh is itself a simple wrapper, and also requires an input filename (not stdin). You may want to reuse the useful parts in your own wrapper, rather than making it another component in the pipeline. – JigglyNaga Sep 19 '18 at 14:08
  • I ended up extending src-hilite-lesspipe.sh, so it now works with piped files. Thanks :) – jaygooby Sep 21 '18 at 11:27
1

I ended up modifying gnu source-highlight's src-hilite-lesspipe.sh so it could work with piped files: https://gist.github.com/jaygooby/9494858d3d481a64819d227a9318f6c7

This means that as well invoking in a regular fashion:

less code.py

You can also get source highlighting for extensionless files that don't have any of the language inference features that source-highlight uses:

less /tmp/mycode

And (the original motivation for doing this) piped files:

cat /tmp/file.rb | less
git show master:obfusicated.perl # implicit pipe to less via git's pager

If you know your code uses a syntax that source-highlight doesn't have any definition files for, you can override the guess, by setting a similar language. c is often a good fallback:

SRCLANG=c git show master:app/views/layouts/application.html.erb

Here's a raw copy of the gist that lives at: https://gist.github.com/jaygooby/9494858d3d481a64819d227a9318f6c7

#! /bin/bash
#
# Based on http://git.savannah.gnu.org/cgit/src-highlite.git/tree/src/src-hilite-lesspipe.sh.in
# by Lorenzo Bettini
#
# Modified by Jay Caines-Gooby to support piped files
# jay@gooby.org
# @jaygooby
#
# Typically called by setting:
#
# export LESSOPEN="|-/path/to/src-hilite-lesspipe.sh %s"
# export LESS=-R
#
# If we're less-ing a file, %s will be replaced by the name of the file. If
# there's no file and we're reading from a pipe, then %s is set to -
#
# This script differs from the original src-hilite-lesspipe.sh
# in that it can handle pipes and files with no extensions and will
# attempt to guess their language using the file command.
#
# So as well as invoking on regular files:
#
# less some.rb
# less some.py
#
# It will should be able to work on:
#
# less no-extension-but-contains-perl
#
# and even with more complex examples (my original motivation
# https://unix.stackexchange.com/questions/469982/how-can-i-use-source-highlight-with-git-show)
#
# git show master:some.rb
#
# It uses bashisms to do this, so is no longer a pure POSIX sh script.
set -eu

# Users can override the guessed language by setting SRCLANG:
# SRCLANG=c git show master:app/views/layouts/application.html.erb
SRCLANG=${SRCLANG:-}

guess_language() {
  lang=$(echo -e ${1:-} | file - | cut -d" " -f2)
  echo $(tr [A-Z] [a-z] <<< "$lang")
}

# check if the language passed as $1 is known to source-highlight
# In an earlier version of this script I set a fallback (c.lang)
# but this causes issues with paging man pages etc
check_language_is_known() {
  fallback=""
  lang=$(source-highlight --lang-list | cut -d' ' -f1 | grep "${1:-}" || true)
  lang=${lang:-$fallback}
  echo $lang
}

for source in "$@"; do
  case $source in
    *ChangeLog|*changelog)
      source-highlight --failsafe -f esc --lang-def=changelog.lang --style-file=esc.style -i "$source" ;;
    *Makefile|*makefile)
      source-highlight --failsafe -f esc --lang-def=makefile.lang --style-file=esc.style -i "$source" ;;
    *.tar|*.tgz|*.gz|*.bz2|*.xz)
      lesspipe "$source" ;;
    *)

      # naive check for a file extension; let source-highlight infer language
      # but only when source isn't - (ie. from a piped file)
      if [[ "$source" != "-" && $(basename "$source") =~ \. ]]; then
        source-highlight --failsafe --infer-lang -f esc --style-file=esc.style -i "$source"
      else
        # We're being piped to, or the filename doesn't have an extension
        # so guess the language.

        # When we're being piped to, we cat stdin, but when it's a file
        # without an extension, we cat the file instead.

        # unset IFS so line breaks are preserved and capture the file's contents
        # (will only work for files up to bash's available memory). There should
        # be a better way to replicate this with tee or process substitution...
        IFS= file=$([ "source" = "-" ] && cat || cat "$source")
        lang=$(guess_language $file)
        lang=$(check_language_is_known $lang)

        # Don't call if source-highlight doesn't know the language
        # BUT also let users override the guessed lang if the environment
        # variable SRCLANG is set. This can help where you know e.g. your
        # source code is c-like, but source-highlight has no specific syntax
        # definition for your code
        [ -n "$SRCLANG" ] && lang="$SRCLANG"

        if [ -n "$lang" ]; then
          echo $file | source-highlight --failsafe -f esc --src-lang=$lang --style-file=esc.style
        else
          echo $file
        fi
      fi

      ;;
  esac
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.