-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[lldb] Add fzf_history command to examples #128571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kastiglione
merged 7 commits into
llvm:main
from
kastiglione:lldb-Add-fzf_history-command-to-examples
Feb 25, 2025
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
57638e9
[lldb] Add fzf_history command to examples
kastiglione d1106bc
Fix code formatting
kastiglione 7fb2bfc
Add docstring for fzf_history
kastiglione eff3367
Run featureless fzf when no copy and paste is available
kastiglione fcdf670
Fix basic fzf invocation
kastiglione 4d04747
Fix a variable name typo
kastiglione 4f9570d
Automatically run selected command on non-darwin platforms
kastiglione File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import os | ||
| import re | ||
| import sys | ||
| import subprocess | ||
|
|
||
| import lldb | ||
|
|
||
|
|
||
| @lldb.command() | ||
| def fzf_history(debugger, cmdstr, ctx, result, _): | ||
| """Use fzf to search and select from lldb command history.""" | ||
| if sys.platform != "darwin": | ||
| result.SetError("fzf_history supports macOS only") | ||
| return | ||
|
|
||
| # Capture the current pasteboard contents to restore after overwriting. | ||
| paste_snapshot = subprocess.run("pbpaste", text=True, capture_output=True).stdout | ||
|
|
||
| # On enter, copy the selected history entry into the pasteboard. | ||
| fzf_command = ( | ||
| "fzf", | ||
| "--no-sort", | ||
| f"--query={cmdstr}", | ||
| "--bind=enter:execute-silent(echo -n {} | pbcopy)+close", | ||
| ) | ||
|
|
||
| history_file = os.path.expanduser("~/.lldb/lldb-widehistory") | ||
| if not os.path.exists(history_file): | ||
| result.SetError("history file does not exist") | ||
| return | ||
|
|
||
| history_commands = _load_history(history_file) | ||
| fzf_input = "\n".join(history_commands) | ||
| completed = subprocess.run(fzf_command, input=fzf_input, text=True) | ||
| # 130 is used for CTRL-C or ESC. | ||
| if completed.returncode not in (0, 130): | ||
| result.SetError(f"fzf failed: {completed.stderr}") | ||
| return | ||
|
|
||
| # Get the user's selected history entry. | ||
| selected_command = subprocess.run("pbpaste", text=True, capture_output=True).stdout | ||
| if selected_command == paste_snapshot: | ||
| # Nothing was selected, no cleanup needed. | ||
| return | ||
|
|
||
| _handle_command(debugger, selected_command) | ||
|
|
||
| # Restore the pasteboard's contents. | ||
| subprocess.run("pbcopy", input=paste_snapshot, text=True) | ||
|
|
||
|
|
||
| def _handle_command(debugger, command): | ||
| """Try pasting the command, and failing that, run it directly.""" | ||
| if not command: | ||
| return | ||
|
|
||
| # Use applescript to paste the selected result into lldb's console. | ||
| paste_command = ( | ||
| "osascript", | ||
| "-e", | ||
| 'tell application "System Events" to keystroke "v" using command down', | ||
| ) | ||
| completed = subprocess.run(paste_command, capture_output=True) | ||
|
|
||
| if completed.returncode != 0: | ||
| # The above applescript requires the "control your computer" permission. | ||
| # Settings > Private & Security > Accessibility | ||
| # If not enabled, fallback to running the command. | ||
| debugger.HandleCommand(command) | ||
|
|
||
|
|
||
| def _load_history(history_file): | ||
| """Load, decode, and parse an lldb history file.""" | ||
| with open(history_file) as f: | ||
| history_contents = f.read() | ||
|
|
||
| history_decoded = re.sub(r"\\0([0-7][0-7])", _decode_char, history_contents) | ||
| history_lines = history_decoded.splitlines() | ||
|
|
||
| # Skip the header line (_HiStOrY_V2_) | ||
| del history_lines[0] | ||
| # Reverse to show latest first. | ||
| history_lines.reverse() | ||
|
|
||
| history_commands = [] | ||
| history_seen = set() | ||
| for line in history_lines: | ||
| line = line.strip() | ||
| # Skip empty lines, single character commands, and duplicates. | ||
| if line and len(line) > 1 and line not in history_seen: | ||
| history_commands.append(line) | ||
| history_seen.add(line) | ||
|
|
||
| return history_commands | ||
|
|
||
|
|
||
| def _decode_char(match): | ||
| """Decode octal strings ('\0NN') into a single character string.""" | ||
| code = int(match.group(1), base=8) | ||
| return chr(code) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that fzf_history here is using
pbcopy/pbpasteandapplescripthere for the clipboard operations but is it possible to just say that "clipboard operations are only supported on macOS" and still run the script?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#128571 (comment)