Skip to content

Instantly share code, notes, and snippets.

@Dimfred
Last active September 9, 2022 09:17
Show Gist options
  • Select an option

  • Save Dimfred/d644335d7028ff5428668bf4f52d6d78 to your computer and use it in GitHub Desktop.

Select an option

Save Dimfred/d644335d7028ff5428668bf4f52d6d78 to your computer and use it in GitHub Desktop.
FZF for lets.yaml and Makefile in one command
#!/usr/bin/env zsh
DISTANCE_COMMAND_DESCRIPTION=15
cmd_parse_lets="
import yaml
with open('lets.yaml', 'r') as f:
commands = yaml.safe_load(f)
commands = commands['commands'].items()
lmax = max(len(k) for k, _ in commands)
commands = [(k + ' ' * ($DISTANCE_COMMAND_DESCRIPTION + lmax - len(k)), v) for k, v in commands]
commands = [f\"{k} {v['description']}\" for k, v in commands]
print('\n'.join(commands))
"
cmd_parse_make="
import re
with open('Makefile', 'r') as f:
commands = [l.rstrip() for l in f.readlines()]
commands = [
t for t in commands
if (
':' in t
and not '%' in t
and not '\$' in t
and not t.startswith(' ')
and not t.startswith('\t')
and not t.startswith('.')
and not t.startswith('#')
)
]
commands = [t for t in commands if re.findall('(\\w+/\\w+)|(\\w+):', t)]
commands = [re.sub(':.*', '', t) for t in commands]
print('\n'.join(commands))
"
executor() {
file=$1
executor_cmd=$2
parse_cmd="$3"
args=${@:4}
if [ -f $file ]; then
# first script argument
if [ -z $4 ]; then
cmd=$(python3 -c "$parse_cmd" | fzf | cut -d" " -f1)
if [ -z $cmd ]; then
exit 0
fi
$executor_cmd $cmd
exit 0
fi
$executor_cmd $args
exit 0
fi
}
main() {
executor lets.yaml lets "$cmd_parse_lets" $@
executor Makefile make "$cmd_parse_make" $@
echo "Neither lets.yaml nor Makefile found"
}
main $@
@Dimfred
Copy link
Copy Markdown
Author

Dimfred commented Sep 9, 2022

Very basic fzf script for lets.yaml and Makefile in one command.

Checks if lets.yaml exists, if so parses the commands + description and puts it to fzf.
If no lets there, then does the same for a Makefile.

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