Skip to content

feat(tui): Add Regex search support with toggle (Ctrl+g) - #337

Open
hongyi-zhao wants to merge 3 commits into
ddworken:masterfrom
hongyi-zhao:master
Open

feat(tui): Add Regex search support with toggle (Ctrl+g)#337
hongyi-zhao wants to merge 3 commits into
ddworken:masterfrom
hongyi-zhao:master

Conversation

@hongyi-zhao

Copy link
Copy Markdown

Closes #336

This PR introduces support for Regex-based searching in hishtory. It allows users to perform precise matching using regular expressions instead of the default fuzzy matching.

It also integrates this feature into the TUI with a new toggle mode.

  • Added support for regex: and re: prefixes in search queries.

  • Implemented extractRegexPatterns to correctly parse regex patterns from the query string, preserving escape characters (e.g., \.sh$).

  • Implemented filterByRegex to filter history entries using Go's regexp package after fetching results from the database.

    • Note: This approach avoids dependencies on SQLite CGO extensions for REGEXP support, ensuring compatibility.
  • New Mode: Added a regexMode state to the TUI model.

  • Visual Indicator: When Regex mode is enabled, the prompt changes to Search Query: [REGEX] >.

  • Automatic Prefixing: When in Regex mode, the user's input is automatically treated as a regex pattern (internally wrapped with re:).

  • Keybinding: Added Ctrl+g (default) to toggle Regex mode on/off.

  • Help Menu: Updated both ShortHelp (footer) and FullHelp (Ctrl+h) to display the new keybinding.

  • Layout: Adjusted the FullHelp layout to include ToggleRegex in the second column for better visibility.

  1. Rebuild and install: go build . && ./hishtory install
  2. Open the TUI via Ctrl+r.
  3. Press Ctrl+g. You should see the [REGEX] indicator appear.
  4. Try regex queries:
    • ^git (Commands starting with git)
    • \.sh$ (Commands ending with .sh)
    • docker.*run
  5. Press Ctrl+h to verify the keybinding ctrl+g is listed in the help menu.
  • Logic for regex filtering
  • TUI toggle implementation
  • Keybinding configuration (Ctrl+g)
  • Help menu updates

Closes ddworken#336

This PR introduces support for **Regex-based searching** in hishtory. It allows users to perform precise matching using regular expressions instead of the default fuzzy matching.

It also integrates this feature into the TUI with a new toggle mode.

- Added support for `regex:` and `re:` prefixes in search queries.
- Implemented `extractRegexPatterns` to correctly parse regex patterns from the query string, preserving escape characters (e.g., `\.sh$`).
- Implemented `filterByRegex` to filter history entries using Go's `regexp` package after fetching results from the database.
  - *Note: This approach avoids dependencies on SQLite CGO extensions for REGEXP support, ensuring compatibility.*

- **New Mode**: Added a `regexMode` state to the TUI model.
- **Visual Indicator**: When Regex mode is enabled, the prompt changes to `Search Query: [REGEX] >`.
- **Automatic Prefixing**: When in Regex mode, the user's input is automatically treated as a regex pattern (internally wrapped with `re:`).
- **Keybinding**: Added `Ctrl+g` (default) to toggle Regex mode on/off.
- **Help Menu**: Updated both `ShortHelp` (footer) and `FullHelp` (Ctrl+h) to display the new keybinding.
- **Layout**: Adjusted the `FullHelp` layout to include `ToggleRegex` in the second column for better visibility.

1. Rebuild and install: `go build . && ./hishtory install`
2. Open the TUI via `Ctrl+r`.
3. Press `Ctrl+g`. You should see the `[REGEX]` indicator appear.
4. Try regex queries:
   - `^git` (Commands starting with git)
   - `\.sh$` (Commands ending with .sh)
   - `docker.*run`
5. Press `Ctrl+h` to verify the keybinding `ctrl+g` is listed in the help menu.

- [x] Logic for regex filtering
- [x] TUI toggle implementation
- [x] Keybinding configuration (`Ctrl+g`)
- [x] Help menu updates
- Removed `m.regexMode` from the update condition in `runQueryAndUpdateTable` to prevent database re-queries on every keypress (e.g., Up/Down arrows).
- Forced a table update (`forceUpdateTable=true`) only when toggling the regex mode via `Ctrl+g`.

This fixes the issue where cursor navigation would reset the view and freeze scrolling while in Regex mode.
@midnightconman

Copy link
Copy Markdown

This doesn't cover redact, does it?

…layout

This PR introduces several improvements to the `query` subcommand:

1. **Manual Flag Parsing for `query` command**:
   Since `queryCmd` has `DisableFlagParsing: true`, the standard `--limit` flag was being ignored. Added manual argument parsing to correctly capture the limit value and strip it from the search query.

2. **Enhanced Regex Search**:

   * Improved `extractRegexPatterns` to support whole-query regex patterns (e.g., `re:^git commit`), allowing spaces in regex.
   * Fixed a bug where regex tokens (like `.*`) were passed to the SQL layer, leading to zero results. Now, when a regex is detected, the SQL query is cleared to fetch raw entries for proper filtering in Go.
   * Increased the default search limit when using regex to ensure a higher recall rate before Go-side filtering.

3. **TUI Table Layout Fix**:
   Sanitized multi-line commands in the ASCII table output by replacing newlines with spaces. This prevents the table layout from breaking when displaying multi-line scripts while preserving the full command content.

4. **Configuration**:
   The `--limit` flag is now correctly respected in both the data retrieval and display phases.
@hongyi-zhao

hongyi-zhao commented Dec 19, 2025

Copy link
Copy Markdown
Author

This doesn't cover redact, does it?

Yes.

At present, only the CLI and TUI search functions are mainly considered, but there are also some unresolved problems, such as:

  1. The search results for the code displayed under CLI cannot be directly copied and used:
werner@x13dai-t:~$ python3 << 'EOF'
print("hello")
print("world")
EOF
hello
world
werner@x13dai-t:~$ hishtory query 're:^python3' --limit 1
CWD  Command                                             ExitCode  timestamp                 
~/   python3 << 'EOF' print("hello") print("world") EOF  0         Dec 19 2025 15:57:44 CST 

On TUI, you can rerun the code snippet by pressing the enter key:
Selection_1813

  1. Even worse, it seems that Hishtory Redact mistakenly deletes things that should not be deleted, and I don't know if this is related to the PR here:
$ hishtory redact 're:^python3'
This will permanently delete 1873 entries, are you sure? [y/N] n
Aborting redact per user response of "n"

Copilot AI added a commit to iteria-systems/hishtory that referenced this pull request Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Support for Regex-based search queries for precise matching.

2 participants