amber

repository·master·Indexed 21 days ago

https://github.com/dalance/amber

A high-performance code search and replace tool written in Rust. It provides two primary commands: `ambs` for recursive searching and `ambr` for interactive search and replace. Features include multi-threaded execution, regular expression support, TOML-based configuration, and the ability to skip VCS directories or .gitignore files.

Tokens
8K
Snippets
28
Records
36
Agent score
76%

What's inside amber

  1. Install amber

    master

    You can install amber using several methods depending on your platform:

    Arch Linux

    Install the amber-search-git package from the AUR:

    yay -S amber-search-git

    Cargo

    Install via Cargo:

    cargo install amber

    Manual

    Download the latest release from the GitHub releases page and extract the binary to a directory in your PATH.

    cargo install amber
  2. How regex replacement works in PipelineReplacer

    master

    When regex is set to true, PipelineReplacer uses the regex crate to perform replacements.

    1. The keyword is treated as the regex pattern.
    2. The replacement string supports capture group expansion (e.g., using $1, $2).
    3. The tool automatically trims leading/trailing \b (word boundary) markers from the keyword to ensure compatibility with the matched segment.

    Example logic:

    • Keyword: \buser_(\d+)\b
    • Replacement: id_$1
    • Match: user_123 $\rightarrow$ id_123
  3. Configure amber

    master

    You can customize amber's behavior using a TOML configuration file.

    Configuration File Locations

    For ambs (search):

    • Linux: ~/.config/amber/ambs.toml or /etc/amber/ambs.toml
    • macOS: ~/Library/Preferences/com.github.dalance.amber/ambs.toml or /etc/amber/ambs.toml
    • Windows: ~/AppData/Roaming/dalance/amber/config/ambs.toml
    • Compatibility Note: If ~/.ambs.toml exists, it takes precedence over the OS-specific locations.

    For ambr (replace):

    • Use ambr.toml located in the same directory as the command.

    Configurable Keys

    Available entries in the TOML file include:

    • regex (bool, default: false)
    • column (bool, default: false)
    • row (bool, default: false)
    • binary (bool, default: false)
    • statistics (bool, default: false)
    • skipped (bool, default: false)
    • interactive (bool, default: true)
    • recursive (bool, default: true)
    • symlink (bool, default: true)
    • color (bool, default: true)
    • file (bool, default: true)
    • skip_vcs (bool, default: true)
    • skip_gitignore (bool, default: true)
    • fixed_order (bool, default: true)
    • parent_ignore (bool, default: true)
    • line_by_match (bool, default: false)
    column = true
  4. Use the ambr CLI for searching and replacing

    master

    The ambr command is a high-performance tool for searching and replacing text patterns within files across specified paths. It supports multiple matching engines (QuickSearch, Regex, and TBM), multi-threaded execution, and interactive replacement modes.

    Basic Usage:

    ambr <KEYWORD> <REPLACEMENT> <PATHS...>

    Key Features:

    • Search Engines: Use --regex for regular expressions or --tbm for the experimental TBM matcher.
    • Input Sources: Use --key-from-file or --rep-from-file to read the search keyword or replacement string from a file instead of command-line arguments.
    • Concurrency: Control performance using --max-threads and --size-per-thread (bytes per thread).
    • File Handling: Supports recursive directory search (--recursive), following symbolic links (--symlink), and skipping VCS directories (--skip-vcs) or .gitignore files (--skip-gitignore).
    ambr "old_text" "new_text" ./src ./tests
  5. Use the ambs CLI for searching

    master

    The ambs command is a high-performance search tool. It accepts a search keyword and one or more paths to search through. By default, it searches the current directory recursively.

    # Basic search for 'keyword' in the current directory
    ambs KEYWORD
    
    # Search for 'keyword' in specific paths
    ambs KEYWORD /path/to/search /another/path
    
    # Use the contents of a file as the search keyword
    ambs --key-from-file keyword_file.txt .
  6. Configure ambs via ambs.toml

    master

    The ambs tool looks for a configuration file named ambs.toml to set default flags. This allows you to persist preferences like whether to use regex, color, or recursive search without typing flags every time.

    Supported configuration keys (based on DefaultFlags) include:

    • regex (bool)
    • column (bool)
    • row (bool)
    • binary (bool)
    • statistics (bool)
    • skipped (bool)
    • recursive (bool)
    • symlink (bool)
    • color (bool)
    • file (bool)
    • skip_vcs (bool)
    • skip_gitignore (bool)
    • fixed_order (bool)
    • parent_ignore (bool)
    • line_by_match (bool)
  7. Configure PipelineMatcher settings

    master

    The PipelineMatcher<T> struct is used to perform keyword searches across files within a pipeline. You can configure several behaviors to optimize performance or visibility:

    • skip_binary: If true, the matcher will attempt to detect and skip binary files. Defaults to true.
    • print_skipped: If true, information about skipped binary files is collected and sent via the pipeline.
    • print_search: If true, debug messages indicating the start and finish of a search for a specific path are sent.
    • binary_check_bytes: The number of leading bytes to inspect to determine if a file is binary. Defaults to 128.
    • mmap_bytes: The file size threshold. Files larger than this value will be memory-mapped (mmap) instead of read into a buffer for better performance. Defaults to 1024 * 1024 (1 MiB).
    let mut matcher = PipelineMatcher::new(matcher, &keyword);
    matcher.skip_binary = false;
    matcher.print_search = true;
    matcher.mmap_bytes = 2 * 1024 * 1024; // 2 MiB threshold
  8. Configure PipelineReplacer replacement behavior

    master

    The PipelineReplacer struct provides several configuration fields to control how replacements are displayed and executed:

    FieldTypeDescription
    is_colorboolEnables/disables colored console output.
    is_interactiveboolEnables interactive mode (prompts user for each match).
    preserve_timeboolIf true, attempts to preserve the original file's access and modification times.
    print_fileboolIf true, prints the filename in the interactive prompt.
    print_columnboolIf true, prints the column number in the interactive prompt.
    print_rowboolIf true, prints the row number in the interactive prompt.
    all_replaceboolInternal flag used during interactive mode to switch to 'replace all' mode.
    regexboolIf true, treats the keyword as a regex pattern and supports capture group expansion in the replacement string.

    Note: keyword and replacement are stored as Vec<u8> (byte vectors).

  9. Configure ambr via ambr.toml

    master

    The ambr tool looks for a configuration file named ambr.toml to set default flags. These settings are merged with command-line arguments, where command-line flags typically override the configuration.

    Supported configuration keys (based on DefaultFlags) include:

    • regex: Enable regular expression search.
    • column: Enable column output format.
    • row: Enable row output format.
    • binary: Enable binary file search.
    • statistics: Enable statistics output.
    • skipped: Enable skipped file output.
    • interactive: Enable interactive replace mode.
    • recursive: Enable recursive directory search.
    • symlink: Enable symbolic link follow.
    • color: Enable colored output.
    • file: Enable filename output.
    • skip_vcs: Enable skipping VCS directories (e.g., .git, .hg).
    • skip_gitignore: Enable skipping files matched by .gitignore.
    • fixed_order: Enable output order guarantee.
    • parent_ignore: Enable searching for .*ignore files in parent directories.
    • preserve_time: Enable preserving file timestamps.
  10. Configure PipelinePrinter output formatting

    master

    The PipelinePrinter struct controls how search results and matches are displayed in the console. You can customize the output by setting the following public fields:

    • is_color: Enables or disables ANSI color output (defaults to true).
    • print_file: If true, includes the filename in the output (defaults to true).
    • print_column: If true, includes the column number in the output (defaults to false).
    • print_row: If true, includes the row (line) number in the output (defaults to false).
    • print_line_by_match: If true, prints each match on its own line. If false, it attempts to group matches on the same line to provide a more compact view (defaults to false).
  11. Use ambs for searching

    master

    The ambs command is used for recursive searching. By default, it searches for a literal keyword (not a regular expression). To use regular expressions, you must include the --regex flag.

    Usage patterns:

    • ambs keyword: Recursively search for keyword starting from the current directory.
    • ambs keyword path: Recursively search for keyword starting from the specified path.
    ambs keyword                  // recursively search 'keyword' from the current directory.
    ambs keyword path             // recursively search 'keyword' from 'path'.
  12. Use ambr for interactive replacing

    master

    The ambr command is used for recursive search and replace. It is interactive by default, prompting you for every match found.

    Interactive Prompt Options:

    • y, Y, Yes: Replace the current match.
    • n, N, No: Skip the current match.
    • a, A, All: Replace all remaining matches non-interactively.
    • q, Q, Quit: Stop the process.

    Usage patterns:

    • ambr keyword replacement: Search and replace keyword with replacement in the current directory.
    • ambr keyword replacement path: Search and replace keyword with replacement in the specified path.

    Regex and Captures: If the --regex flag is enabled, you can use regex captures in the replacement string. You can also use the --no-interactive flag to skip prompts entirely.

    ambr keyword replacement      // recursively search 'keyword' from the current directory, and replace to 'replacement' interactively.
    ambr keyword replacement path // recursively search 'keyword' from 'path', and replace to 'replacement' interactively.
    
    # Example with regex captures:
    $ ambr --no-interactive --regex '(aaa) (?<pat>bbb)' '$1 $pat ${1} ${pat}' test.txt