Skip to content

Instantly share code, notes, and snippets.

@Efreak
Last active January 9, 2022 18:25
Show Gist options
  • Save Efreak/c2fad032110e2b65dcf625c20f889a2f to your computer and use it in GitHub Desktop.
Save Efreak/c2fad032110e2b65dcf625c20f889a2f to your computer and use it in GitHub Desktop.
list packages installed via cargo with colored columns for package name, version, and installed binaries
#!/usr/bin/env bash
if [ "$1" = "csearch" ]; then shift; fi #remove subcommand if called via `cargo csearch`
c33=$'\033[33m'
c34=$'\033[34m'
c35=$'\033[35m'
c0=$'\033[0m'
list="$(cargo search $@)"
function colorize(){
sed -E 's/^([^ ]+) = "([^"]+)"\s*\# /'"${c33}\1${c35}\t\2$c0\t/g"
}
function tablize(){
column --table -s $'\t' -W 3 -c $((${COLUMNS:-80} + 6))
}
function pager(){
${PAGER:-less -R}
}
function listcrates(){
echo "$list"|grep -vE '^\.\.\. and [0-9]+ crates more' | colorize | tablize
echo "$list"|grep -E '^\.\.\. and [0-9]+ crates more'
}
listcrates | pager
#!/usr/bin/env bash
c33=$'\033[33m'
c34=$'\033[34m'
c35=$'\033[35m'
list="$(cargo install --list)"
function removesources(){
sed -E 's/\s*[(].*?\/.*?[)]//g'
}
function mergelines(){
awk '
/^ / { # indented line
sub(/^ /, ", ") # trim leading whitespace
merged = merged $0 # join to previous lines
next
}
merged { # previous merged line exists?
print merged # print previous
}
{ # line starts with non-whitespace char.
merged = $0 # start new merged line
}
END {
print merged # print last merged line
}' |sed -E 's/^([^,]+), /\1/g'
# no, i dont't really know how to use awk. yes, this is shitty code.
}
function colorize(){
sed -E 's/^([^ ]+) v([^\:]+)\:/'"${c33}\1|${c35}\2|${c34}/g"
}
function tablize(){
column --table -s'|'
}
function pager(){
${PAGER:-less -R}
}
echo "$list"$'\n\n'Total crates: $(echo "$list"|wc -l) | removesources | mergelines | colorize | tablize | pager
@Efreak
Copy link
Author

Efreak commented Dec 20, 2021

Yes, this is shitty code.

The functions/pipes are because I fairly regularly make changes to the way it works, and this way I can leave out of modify things without removing them entirely.

Save as cargo-installed in ~/.local/bin or somewhere in path, chmod +x, and you can use cargo installed
Alternatively:

git clone https://gist.github.com/Efreak/c2fad032110e2b65dcf625c20f889a2f ~/src/cargo-installed
ln -s ~/src/cargo-installed/cargo-installed.bsh ~/.local/bin/cargo-installed

And then you can update via git. Not that I expect there will be many updates here...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment