Skip to content

Instantly share code, notes, and snippets.

@uchida
Last active December 15, 2015 23:59
Show Gist options
  • Save uchida/5344419 to your computer and use it in GitHub Desktop.
Save uchida/5344419 to your computer and use it in GitHub Desktop.
convert anything to UTF-8 for LESSOPEN
#!/usr/bin/env bash
# convert anything to UTF-8 for LESSOPEN
LESSPIPE=lesspipe.sh
conv_utf8() {
if [ -x "$(which nkf 2> /dev/null)" ]; then
nkf -w -Lw "$1"
elif [ -x "$(which ruby 2> /dev/null)" ]; then
ruby -r nkf -pe '$_=NKF.nkf "-w -Lw",$_' "$1"
else
cat "$1"
fi
}
if [ $# -eq 0 -o "$1" == "-" ]; then # pipe
conv_utf8 - 2> /dev/null
else # file
if [ -x "$(which $LESSPIPE 2> /dev/null)" -a $($LESSPIPE "$1" | head | wc -c) -gt 0 ]; then
$LESSPIPE "$1" | conv_utf8 - 2> /dev/null
else
conv_utf8 "$1" 2> /dev/null
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment