Skip to content

Commit

Permalink
Add support for zsh shell completions
Browse files Browse the repository at this point in the history
This also fixes `rr ls` not to list pre-installed files as traces.
  • Loading branch information
jyn514 authored and rocallahan committed Nov 30, 2024
1 parent 679bbd4 commit 84b7de5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,11 @@ install(PROGRAMS scripts/signal-rr-recording.sh
install(PROGRAMS scripts/rr_completion
DESTINATION ${CMAKE_INSTALL_DATADIR}/bash-completion/completions RENAME rr)

# Note that this works fine when installing to /usr/local, but zsh by default doesn't autoload *any* completions in HOME,
# so people will still have to manually set `FPATH=~/.local/share/zsh/site-functions`.
install(PROGRAMS scripts/rr_completion.zsh
DESTINATION ${CMAKE_INSTALL_DATADIR}/zsh/site-functions RENAME _rr)

set(RR_INSTALL_LIBS rrpreload rrpage rr_exec_stub)
if(RTLD_AUDIT)
set(RR_INSTALL_LIBS ${RR_INSTALL_LIBS} rraudit)
Expand Down
39 changes: 39 additions & 0 deletions scripts/rr_completion.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#compdef rr

_rr() {
# allow overridding rr with another command (e.g. if multiple versions are installed)
zstyle -s :completion${curcontext}options command rr
: ${rr:=rr}

_rr_subcommands() {
$rr --list-commands | cut -s -d ' ' -f 3
}
_rr_traces() {
$rr ls | grep -v '^cpu_lock$'
}

_arguments -C \
'1:subcommand:($(_rr_subcommands))' \
'*::arg:->args'

case $state in
args) ;;
*) return;;
esac

# different subcommands can have different options. show the appropriate options for each.
# this is not ideal; `reply=` forces zsh to rerun `rr help` each time you hit tab.
# the alternative though is rewriting half the code in _arguments.
zstyle -e ':completion:*:*:rr:*:options' command \
'reply=( '${(q)service}' help ${words:#-*} )'

case $line[1] in
# complete a command, then delegate to that command's completion script
# -A means "don't use _normal until we've completed a non-option"
record) _arguments -A '-*' '1:command: _precommand' '*:: :_normal -p $service' --;;
replay|rerun|ps|sources|traceinfo|pack|dump) _arguments '*:trace:($(_rr_traces))' --;;
help) _arguments ':subcommand:($(_rr_subcommands))' --;;
explicit-sources|filename) _gnu_generic;;
*) _arguments --;
esac
}
8 changes: 6 additions & 2 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,10 @@ bool remove_latest_trace_symlink() {
return true;
}

static bool ends_with(std::string_view str, std::string_view suffix) {
return str.size() >= suffix.size() && str.compare(str.size()-suffix.size(), suffix.size(), suffix) == 0;
}

bool is_valid_trace_name(const string& entry, std::string* reason) {
// filename corresponds to dirname
const string name = filename(entry.c_str());
Expand All @@ -2310,9 +2314,9 @@ bool is_valid_trace_name(const string& entry, std::string* reason) {
}
return false;
}
if (name == "cpu_lock") {
if (name == "cpu_lock" || name == "src" || ends_with(name, ".xml")) {
if (reason) {
*reason = "Name cpu_lock is reserved";
*reason = "Name " + name + " is reserved";
}
return false;
}
Expand Down

0 comments on commit 84b7de5

Please sign in to comment.