Git Product home page Git Product logo

ogrep-rs's Introduction

Outline grep

Featureful tool for searching in indentation-structured text files.

Inspired by ogrep by Matt Brubeck. That ogrep is compact and beautiful, but not featureful.

See also ogrep — port of this tool written in Python (to be truly, it was first).

Brief

ogrep is much like grep, both can search for matches and display their context. But context in grep is “N lines before/after match”, and in ogrep it is “lines above matched one with lower indentation”.

Let me explain. I use this tool mostly when working with GN build files, so I'll use some large BUILD.gn file as an example. Usual task is to search for source file name and understand which target includes this file and under which conditions.

Let's find mentions of “arena.cc” file:

# grep arena.cc BUILD.gn
      "base/arena.cc",

Ok, now we now that our file is here, but don't know target. Let's ask for some context:

# grep -C2 arena.cc BUILD.gn
      "base/address_tracker_linux.cc",
      "base/address_tracker_linux.h",
      "base/arena.cc",
      "base/arena.h",
      "base/backoff_entry.cc",

Nope, not that useful. Let's try ogrep:

ogrep arena.cc BUILD.gn
 102: component("net") {
 385:   if (!is_nacl) {
 386:     sources += [
 409:       "base/arena.cc",

Now that's useful! We immediately know that file in included into “net“ target under “!is_nacl” condition.

It is even better, because ogrep can use colors, here is a picture:

Installation

Using Cargo (any platform)

Install Rust and Cargo, if you haven't yet, then

cargo install ogrep

MacOS

Install Homebrew, then

brew install kriomant/ogrep-rs/ogrep-rs

Other platforms

Sorry, not yet, but I'm working on it. Use Cargo for now.

Options

There are plently of available options, run with --help to list them.

Tool is useful not only for strict indentation-based files (like Python source) or GN build files, but for wide range of text files, because even not-indentation based ones are usually formatted for convenience.

There are even some C-related hacks built-in.

Here is brief feature list:

  • Pattern is fixed text by default, but you may use arbitrary regular expression with -e.

  • Usual -w (match whole words) and -i (case-insensitive search) are available.

  • Tool preserve some blank lines between matches, because it helps to visually separate groups of related matches, you can turn it off with --no-breaks.

  • Sometimes it is useful to see whether there were other lines between matched ones. Use --ellipsis for that.

  • If you integrate otool with external tools, --print-filename options may be useful, it tells to print filename if any match found.

  • By default “if-else” branches are treated specially: if-branches are preserved so you know conditions even when match is found in “else” branch:

  • Traditional context (displaying N leading and/or trailing lines around matched one) is also supported with --context/-C, --before-context/-B and --after-context/-A options.

# ./ogrep filename_util_icu BUILD.gn
 102: component("net") {
2106:   if (!is_nacl) {
2210:     if (use_platform_icu_alternatives) {
2222:     } else {
2228:       sources += [
2229:         "base/filename_util_icu.cc",

This can be turned off with --no-smart-branches.

  • Preprocessor instructions in C files are often written without any indentation (or indentation is inserted after “#”). So tool ignores preprocessor instructions by default until --no-ignore-preprocessor is given.

More intelligent handling of preprocessor instructions (parallel context) is planned.

Searching in directory

Integration with external tools

otool in intended to search in single file only. And it is not so fast to be used for searching through many files. But you can integrate it with other search tools like this:

grep -l cache_used -r . --include='*.cc' | xargs -n1 ogrep --print-filename cache_used

Builtin git grep support

ogrep has builtin integration with git grep: when -g option is given, second argument is passed to git grep as path specification. All relevant options (-w, -i, etc.) are also passed to git grep automatically, --print-filename is forced.

ogrep -g cache_used '*.cc'

ogrep-rs's People

Contributors

kriomant avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

nyanshell icodein

ogrep-rs's Issues

Update published version on crates.io

Currently available version (which is 0.2.1) is broken:

$ cargo install ogrep
    Updating crates.io index
  Installing ogrep v0.2.1
   Compiling libc v0.2.62
   Compiling memchr v2.2.1
   Compiling bitflags v1.1.0
   Compiling ucd-util v0.1.5
   Compiling unicode-width v0.1.6
   Compiling regex v0.2.11
   Compiling lazy_static v1.4.0
   Compiling utf8-ranges v1.0.4
   Compiling ansi_term v0.11.0
   Compiling strsim v0.8.0
   Compiling either v1.5.3
   Compiling vec_map v0.8.1
   Compiling ansi_term v0.10.2
   Compiling regex-syntax v0.5.6
   Compiling textwrap v0.11.0
   Compiling thread_local v0.3.6
   Compiling itertools v0.7.11
   Compiling aho-corasick v0.6.10
   Compiling atty v0.2.13
   Compiling clap v2.33.0
   Compiling ogrep v0.2.1
error[E0283]: type annotations required: cannot resolve `std::borrow::Cow<'_, str>: std::convert::From<&_>`
   --> /home/tosh/.cargo/registry/src/github.com-1ecc6299db9ec823/ogrep-0.2.1/src/main.rs:541:17
    |
541 |                 Cow::from(options.pattern.as_ref())
    |                 ^^^^^^^^^
    |
    = note: required by `std::convert::From::from`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0283`.
error: failed to compile `ogrep v0.2.1`, intermediate artifacts can be found at `/tmp/cargo-installduKz53`

Caused by:
  Could not compile `ogrep`.

To learn more, run the command again with --verbose.
$ cargo --version
cargo 1.37.0
$ rustc --version
rustc 1.37.0

setup failure with cargo

Hi, I encounter the following error message. Please help. Thanks!!!
I used "cargo install ogrep-rs"

could not find ogrep-rs in registry https://github.com/rust-lang/crates.io-index with version *

Some matches do not show in results

@kriomant
Hi, as title
Here is the file I used: link (utf-8 encoded, contents are in traditional Chinese)
If you search hashtag symbol (#), it should have 15 matches (I checked in notepad++ and grep)
However in the latest ogrep (0.4.0), it only shows 6 results.
I also tested in python version ogrep, it works fine (also with 15 results).
You may confirm if it is a bug specific in rust. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.