Created
June 27, 2019 22:59
-
-
Save jorsn/9cb109b3192b03016e6a0914cbcb1bd8 to your computer and use it in GitHub Desktop.
A convenience shell wrapper around the nix function unsafeGetAttrPos.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| pname="$(basename "$0")" | |
| read -d '' usage <<EOF | |
| Usage: $pname -h|--help | |
| $pname [-n <number of context lines>] [attrset (default: nixpkgs)] <attr name> | |
| EOF | |
| attrset="import <nixpkgs> {}" | |
| n=3 | |
| case "$1" in | |
| -h | --help ) echo "$usage" >&2; exit;; | |
| -n ) n="$2"; shift 2;; | |
| esac | |
| if [ "$#" -gt 1 ]; then | |
| attrset="$1" | |
| shift | |
| fi | |
| attr="$1" | |
| read -d '' expr <<EOF | |
| let | |
| inherit (builtins.unsafeGetAttrPos "${attr}" (${attrset})) file line; | |
| in "\${file}:\${builtins.toString line}" | |
| EOF | |
| attrPos="$(nix eval --raw "($expr)")" | |
| file="${attrPos%:*}" | |
| line="${attrPos##*:}" | |
| if [ $n -gt 0 ]; then | |
| context="-e $((line-n)),$((line-1))p -e $((line+1)),$((line+n))p" | |
| fi | |
| echo "${attrPos}:" | |
| sed -n $context -e ${line}'s/^.\+$/[1;31m&[0m/gp' "$file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment