跳到正文

科技·工程

cwdiff

Psq2416约 9 分钟
原文

这是 2026年7月12日 存档的第 3 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年7月10日
收录
2026年7月11日
更新
2026年11月18日
篇幅
约 9 分钟

版本

  1. v5当前
  2. v4
  3. v3
  4. v2
更早的 1 个版本
  1. v1

cwdiff 1.0.1

  • 修复了一些已知问题

源代码(POSIX Shell 脚本)

Bash
#!/bin/sh
readonly escape="$(printf "\033")"
readonly help="Usage: ${0##*/} [OPTION]... [FILE1] [FILE2]
Compares words in two files and report differences, with color support.

Mandatory arguments to long options are mandatory for short options too.
  -h, --help         display this help and exit
  -v, --version      output version information and exit
      --color[=WHEN] color output; WHEN is 'never', 'always', or 'auto';
                       plain --color means --color='auto'

Other options are the same as for 'wdiff'."
readonly version="cwdiff 1.0.1"
status=0 wdiff_options= color=never default_sd=1 default_ed=1 default_si=1 default_ei=1 sd='[-' ed='-]' si='{+' ei='+}'
options="$(getopt -n "${0##*/}" -o "hvC123isapltw:x:y:z:nd" -l "help,version,copyright,color::,no-deleted,no-inserted,no-common,ignore-case,statistics,auto-pager,printer,less-mode,terminal,start-delete:,end-delete:,start-insert:,end-insert:,avoid-wraps,diff-input" -- "$@")"
if [ $? -ne 0 ]
then
    status=2
fi
eval set -- "$options"
while [ "$1" != -- ]
do
    case "$1" in
    -h|--help)
        echo "$help"
        exit 0
        ;;
    -v|--version)
        echo "$version"
        exit 0
        ;;
    --color)
        case "$2" in
        never)
            color=never
            ;;
        ''|auto)
            color=auto
            ;;
        always)
            color=always
            ;;
        *)
            echo "${0##*/}: invalid color '$2'" >&2
            status=2
            ;;
        esac
        shift 2
        ;;
    -C|--copyright)
        wdiff -C
        exit 0
        ;;
    -1|--no-deleted)
        wdiff_options="${wdiff_options}1"
        shift
        ;;
    -2|--no-inserted)
        wdiff_options="${wdiff_options}2"
        shift
        ;;
    -3|--no-common)
        wdiff_options="${wdiff_options}3"
        shift
        ;;
    -i|--ignore-case)
        wdiff_options="${wdiff_options}i"
        shift
        ;;
    -s|--statistics)
        wdiff_options="${wdiff_options}s"
        shift
        ;;
    -a|--auto-paper)
        wdiff_options="${wdiff_options}a"
        if [ -t 1 ]
        then
            if [ $default_sd -eq 1 ]
            then
                sd=
            fi
            if [ $default_ed -eq 1 ]
            then
                ed=
            fi
            if [ $default_si -eq 1 ]
            then
                si=
            fi
            if [ $default_ei -eq 1 ]
            then
                ei=
            fi
        fi
        shift
        ;;
    -p|--printer)
        wdiff_options="${wdiff_options}p"
        if [ $default_sd -eq 1 ]
        then
            sd=
        fi
        if [ $default_ed -eq 1 ]
        then
            ed=
        fi
        if [ $default_si -eq 1 ]
        then
            si=
        fi
        if [ $default_ei -eq 1 ]
        then
            ei=
        fi
        shift
        ;;
    -l|--less-mode)
        wdiff_options="${wdiff_options}l"
        if [ $default_sd -eq 1 ]
        then
            sd=
        fi
        if [ $default_ed -eq 1 ]
        then
            ed=
        fi
        if [ $default_si -eq 1 ]
        then
            si=
        fi
        if [ $default_ei -eq 1 ]
        then
            ei=
        fi
        shift
        ;;
    -t|--terminal)
        wdiff_options="${wdiff_options}t"
        if [ $default_sd -eq 1 ]
        then
            sd=
        fi
        if [ $default_ed -eq 1 ]
        then
            ed=
        fi
        if [ $default_si -eq 1 ]
        then
            si=
        fi
        if [ $default_ei -eq 1 ]
        then
            ei=
        fi
        shift
        ;;
    -w|--start-delete)
        default_sd=0
        sd="$2"
        shift 2
        ;;
    -x|--end-delete)
        default_ed=0
        ed="$2"
        shift 2
        ;;
    -y|--start-insert)
        default_si=0
        si="$2"
        shift 2
        ;;
    -z|--end-insert)
        default_ei=0
        ei="$2"
        shift 2
        ;;
    -n|--avoid-wraps)
        wdiff_options="${wdiff_options}n"
        shift
        ;;
    -d|--diff-input)
        wdiff_options="${wdiff_options}d"
        shift
        ;;
    esac
done
if [ $status -ne 0 ]
then
    echo "${0##*/}: Try '${0##*/} --help' for more information." >&2
    exit 2
fi
if [ -n "$wdiff_options" ]
then
    wdiff_options="-$wdiff_options"
fi
if [ $color = always -o \( $color = auto -a -t 1 \) ]
then
    exec wdiff $wdiff_options -w "$escape[31m$sd" -x "$ed$escape[m" -y "$escape[32m$si" -z "$ei$escape[m" "$@"
else
    exec wdiff $wdiff_options -w "$sd" -x "$ed" -y "$si" -z "$ei" "$@"
fi

cwdiff 1.0.0