How can I use âsource-highlightâ with âgit showâ?
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
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.
git colors syntax source-highlight
add a comment |Â
up vote
-1
down vote
favorite
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.
git colors syntax source-highlight
@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 at 12:22
You can't both programs are independent of each other!
â TNT
Sep 19 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 colourisegit show
's output
â jaygooby
Sep 19 at 13:41
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
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.
git colors syntax source-highlight
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.
git colors syntax source-highlight
git colors syntax source-highlight
edited Sep 19 at 12:24
asked Sep 19 at 10:45
jaygooby
1256
1256
@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 at 12:22
You can't both programs are independent of each other!
â TNT
Sep 19 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 colourisegit show
's output
â jaygooby
Sep 19 at 13:41
add a comment |Â
@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 at 12:22
You can't both programs are independent of each other!
â TNT
Sep 19 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 colourisegit show
's output
â jaygooby
Sep 19 at 13:41
@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 at 12:22
@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 at 12:22
You can't both programs are independent of each other!
â TNT
Sep 19 at 12:45
You can't both programs are independent of each other!
â TNT
Sep 19 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 at 13:41
@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 at 13:41
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
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:
- reads the entirety of the input and saves it in a temporary file
- calls
source-highlight ...
on that file - 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 forsrc-hilite-lesspipe.sh
so I can pass a--lang-def
as part of the$@
args that are fed to it, and then tosource-highlight
itself
â jaygooby
Sep 19 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 at 14:08
I ended up extending src-hilite-lesspipe.sh, so it now works with piped files. Thanks :)
â jaygooby
Sep 21 at 11:27
add a comment |Â
up vote
1
down vote
accepted
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() file -
# 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
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
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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:
- reads the entirety of the input and saves it in a temporary file
- calls
source-highlight ...
on that file - 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 forsrc-hilite-lesspipe.sh
so I can pass a--lang-def
as part of the$@
args that are fed to it, and then tosource-highlight
itself
â jaygooby
Sep 19 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 at 14:08
I ended up extending src-hilite-lesspipe.sh, so it now works with piped files. Thanks :)
â jaygooby
Sep 21 at 11:27
add a comment |Â
up vote
1
down vote
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:
- reads the entirety of the input and saves it in a temporary file
- calls
source-highlight ...
on that file - 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 forsrc-hilite-lesspipe.sh
so I can pass a--lang-def
as part of the$@
args that are fed to it, and then tosource-highlight
itself
â jaygooby
Sep 19 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 at 14:08
I ended up extending src-hilite-lesspipe.sh, so it now works with piped files. Thanks :)
â jaygooby
Sep 21 at 11:27
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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:
- reads the entirety of the input and saves it in a temporary file
- calls
source-highlight ...
on that file - 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.
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:
- reads the entirety of the input and saves it in a temporary file
- calls
source-highlight ...
on that file - 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.
answered Sep 19 at 13:08
JigglyNaga
2,958624
2,958624
I'm veering towards creating a shell function wrapper forsrc-hilite-lesspipe.sh
so I can pass a--lang-def
as part of the$@
args that are fed to it, and then tosource-highlight
itself
â jaygooby
Sep 19 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 at 14:08
I ended up extending src-hilite-lesspipe.sh, so it now works with piped files. Thanks :)
â jaygooby
Sep 21 at 11:27
add a comment |Â
I'm veering towards creating a shell function wrapper forsrc-hilite-lesspipe.sh
so I can pass a--lang-def
as part of the$@
args that are fed to it, and then tosource-highlight
itself
â jaygooby
Sep 19 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 at 14:08
I ended up extending src-hilite-lesspipe.sh, so it now works with piped files. Thanks :)
â jaygooby
Sep 21 at 11:27
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 at 13:50
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 at 13:50
1
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 at 14:08
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 at 14:08
I ended up extending src-hilite-lesspipe.sh, so it now works with piped files. Thanks :)
â jaygooby
Sep 21 at 11:27
I ended up extending src-hilite-lesspipe.sh, so it now works with piped files. Thanks :)
â jaygooby
Sep 21 at 11:27
add a comment |Â
up vote
1
down vote
accepted
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() file -
# 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
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
add a comment |Â
up vote
1
down vote
accepted
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() file -
# 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
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
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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() file -
# 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
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
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() file -
# 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
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
edited Sep 21 at 11:33
answered Sep 21 at 11:26
jaygooby
1256
1256
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f469982%2fhow-can-i-use-source-highlight-with-git-show%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
@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 at 12:22
You can't both programs are independent of each other!
â TNT
Sep 19 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 at 13:41