Skip to content

Instantly share code, notes, and snippets.

@ivan
Last active June 26, 2022 22:42
Show Gist options
  • Select an option

  • Save ivan/13edbae669481308c08ae5a4db2cb686 to your computer and use it in GitHub Desktop.

Select an option

Save ivan/13edbae669481308c08ae5a4db2cb686 to your computer and use it in GitHub Desktop.
Print an outline of Rust code in the current directory
#!/usr/bin/env bash
# Print an outline of Rust code in the current directory
GRAY=$(tput setaf 7)
PURPLE=$(tput setaf 5)
BOLD=$(tput bold)
CLEAR=$(tput sgr0)
rg --sort path --heading --no-line-number --glob '*.rs' '\b(fn|struct|enum|trait|impl|mod|macro_rules)\b' |\
# Indent each line
sed -r -s 's,^,\t,g' |\
# Dedent each filename and make it bold purple
sed -r -s "s,^\t(.*\.rs)$,$BOLD$PURPLE\1$CLEAR,g" |\
# Bold the identifier and make everything else gray
sed -r -s "s,\b((fn|struct|enum|trait|impl|mod|macro_rules!) +)([^ <\(]+)(.*),$GRAY\1$CLEAR$BOLD\3$CLEAR$GRAY\4$CLEAR,g" |\
# Convert each tab to 4 spaces
sed -r -s 's,\t, ,g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment