-
Notifications
You must be signed in to change notification settings - Fork 667
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
Add fzf history search feature #1170
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a2dd664
fzf history search feature
lazmond3 a8495bd
Add me to Contributors and write changelog.md
lazmond3 b21dfbf
Remove redundant method definition load_history_strings
lazmond3 d11dc90
Fix ci: setuptools version fixed to <= 71.1.0 for testing
lazmond3 3acf2b0
Update changelog.md
lazmond3 e766643
Fix changelog.md
lazmond3 6b2838e
Fix CI and solve #1146
lazmond3 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 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
This file contains 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 |
---|---|---|
|
@@ -97,6 +97,7 @@ Contributors: | |
* Zhanze Wang | ||
* Houston Wong | ||
* Mohamed Rezk | ||
* Ryosuke Kazami | ||
|
||
|
||
Created by: | ||
|
This file contains 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
This file contains 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
This file contains 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
Empty file.
This file contains 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,45 @@ | ||
from shutil import which | ||
|
||
from pyfzf import FzfPrompt | ||
from prompt_toolkit import search | ||
from prompt_toolkit.key_binding.key_processor import KeyPressEvent | ||
|
||
from .history import FileHistoryWithTimestamp | ||
|
||
|
||
class Fzf(FzfPrompt): | ||
def __init__(self): | ||
self.executable = which("fzf") | ||
if self.executable: | ||
super().__init__() | ||
|
||
def is_available(self) -> bool: | ||
return self.executable is not None | ||
|
||
|
||
def search_history(event: KeyPressEvent): | ||
buffer = event.current_buffer | ||
history = buffer.history | ||
|
||
fzf = Fzf() | ||
|
||
if fzf.is_available() and isinstance(history, FileHistoryWithTimestamp): | ||
history_items_with_timestamp = history.load_history_with_timestamp() | ||
|
||
formatted_history_items = [] | ||
original_history_items = [] | ||
for item, timestamp in history_items_with_timestamp: | ||
formatted_item = item.replace('\n', ' ') | ||
timestamp = timestamp.split(".")[0] if "." in timestamp else timestamp | ||
formatted_history_items.append(f"{timestamp} {formatted_item}") | ||
original_history_items.append(item) | ||
|
||
result = fzf.prompt(formatted_history_items, fzf_options="--tiebreak=index") | ||
|
||
if result: | ||
selected_index = formatted_history_items.index(result[0]) | ||
buffer.text = original_history_items[selected_index] | ||
buffer.cursor_position = len(buffer.text) | ||
else: | ||
# Fallback to default reverse incremental search | ||
search.start_search(direction=search.SearchDirection.BACKWARD) |
This file contains 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,52 @@ | ||
import os | ||
from typing import Iterable, Union, List, Tuple | ||
|
||
from prompt_toolkit.history import FileHistory | ||
|
||
_StrOrBytesPath = Union[str, bytes, os.PathLike] | ||
|
||
|
||
class FileHistoryWithTimestamp(FileHistory): | ||
""" | ||
:class:`.FileHistory` class that stores all strings in a file with timestamp. | ||
""" | ||
|
||
def __init__(self, filename: _StrOrBytesPath) -> None: | ||
self.filename = filename | ||
super().__init__(filename) | ||
|
||
def load_history_with_timestamp(self) -> List[Tuple[str, str]]: | ||
""" | ||
Load history entries along with their timestamps. | ||
|
||
Returns: | ||
List[Tuple[str, str]]: A list of tuples where each tuple contains | ||
a history entry and its corresponding timestamp. | ||
""" | ||
history_with_timestamp: List[Tuple[str, str]] = [] | ||
lines: List[str] = [] | ||
timestamp: str = "" | ||
|
||
def add() -> None: | ||
if lines: | ||
# Join and drop trailing newline. | ||
string = "".join(lines)[:-1] | ||
history_with_timestamp.append((string, timestamp)) | ||
|
||
if os.path.exists(self.filename): | ||
with open(self.filename, "rb") as f: | ||
for line_bytes in f: | ||
line = line_bytes.decode("utf-8", errors="replace") | ||
|
||
if line.startswith("#"): | ||
# Extract timestamp | ||
timestamp = line[2:].strip() | ||
elif line.startswith("+"): | ||
lines.append(line[1:]) | ||
else: | ||
add() | ||
lines = [] | ||
|
||
add() | ||
|
||
return list(reversed(history_with_timestamp)) |
This file contains 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 |
---|---|---|
|
@@ -14,4 +14,4 @@ pyperclip>=1.8.1 | |
importlib_resources>=5.0.0 | ||
pyaes>=1.6.1 | ||
sqlglot>=5.1.3 | ||
setuptools | ||
setuptools<=71.1.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this version restricted? |
This file contains 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
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.
Can you elaborate a little bit on what this
mysqlx
is meant to do?