Giter Club home page Giter Club logo

pss's Introduction

pss

Introduction: what is pss?

pss is a power-tool for searching inside source code files. pss searches recursively within a directory tree, knows which extensions and file names to search and which to ignore, automatically skips directories you wouldn't want to search in (for example .git or .svn), colors its output in a helpful way, and does much more.

If you're familiar with the ack tool, then you will find pss very similar (see https://github.com/eliben/pss/wiki/pss-and-ack).

Pre-requisites

pss needs only Python to run. It's being tested with several of the latest Python 3.x versions on Linux, Mac OS and Windows. Earlier versions of Python 3.x may work as well, but this isn't guaranteed. Python 2 is no longer supported (but you may download pss version 1.43 or older if you need it to work with Python 2).

Installing

pss can be installed from PyPI (Python Package Index):

> pip install pss

Alternatively, you can download the source distribution either from PyPI or from the main Github project page. When you unzip the source distribution, run:

> python setup.py install

Running without installing

pss supports direct invocation even without installing it. This may be useful if you're on a machine without administrator rights, or want to experiment with a source distribution of pss.

Just unzip the pss distribution into some directory. Let's assume its full path is /path/to/pss. You can now run:

> /path/to/python /path/to/pss

And this will invoke pss as expected. This command can also be tied to an alias or placed in a shell (or batch) script for convenience.

How to use it?

pss is meant to be executed from the command line. Running it with no arguments or with -h will print a detailed usage message.

For some detailed usage examples, check out the Usage wiki page - https://github.com/eliben/pss/wiki/Usage-samples

License

pss is open-source software. Its code is in the public domain. See the LICENSE file for more details.

pss's People

Contributors

adamatan avatar adimit avatar benhoyt avatar bernh avatar bobotig avatar bunyk avatar c-f-h avatar crazymerlyn avatar denrou avatar eliben avatar gremmie avatar jaywink avatar krillegh avatar macro avatar mdealencar avatar osyvokon avatar paul-ollis avatar pekkaklarck avatar pfmoore avatar rdburns avatar sherzberg avatar tobiaspleyer avatar yaheath 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pss's Issues

A simple optimization

Hi,

Two things to consider:

in contentmatcher.py: implement _pattern_is_simple as:

def _pattern_is_simple(self, pattern):
""" A "simple" pattern that can be matched with str.find and doesn't
require a regex engine.
"""
return re.escape(pattern) == pattern

Tests do not run under Python3.6

I noticed, on my machine and TravisCI, that the test test_whole_words in the suite test_contentmatcher.py does not succeed. From the error message it appears that some changes have been made in Python36's regular expression library that cause these issues.

Redirected output has two line breaks between each line on Windows

Because the input files are opened in binary mode and stdout is opened as text, on Windows when redirecting the output to a file you end up with CR CR LF line breaks, which is incorrect, and most editors show as two line breaks (extra blank line in between every line).

The "proper" fix would be to open the file in text mode, but as you're doing binary stuff on it too, it's easiest just to replace CR LF with LF before outputting (which the text-opened stdout will convert back to CR LF).

Fixed in this commit

Add "searchindex.js" to the default exclude list

I mostly use pss to search CPython clones, and it frequently gets hits on the searchindex.js file generated by Sphinx.

Would it be worth adding "searchindex.js" to the default exclusion regexes?

Excluding it with "--nojs" is easy enough for my particular use case, but it wouldn't work so well for a web programming project that actually had also had real Javascript files to search.

TestFileFinder.test_ignore_dirs fails on Windows

I'm running the current eliben/pss master on Windows 7 64-bit, and running test/all_tests.py gives this:

..F.........................
======================================================================
FAIL: test_ignore_dirs (test_filefinder.TestFileFinder)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Hoyt\Downloads\pss\test\test_filefinder.py", line 120, in test_ignore_dirs
    'simple_filefinder/partialignored/found.c'])
  File "C:\Users\Hoyt\Downloads\pss\test\test_filefinder.py", line 49, in assertPathsEqual
    list(sorted(map(os.path.normpath, second))))
AssertionError: Lists differ: ['simple_filefinder\\a.c', 'si... != ['simple_filefinder\\a.c', 'si...

First list contains 1 additional elements.
First extra element 3:
simple_filefinder\partialignored\thisoneisignored\notfound.c

  ['simple_filefinder\\a.c',
   'simple_filefinder\\c.c',
-  'simple_filefinder\\partialignored\\found.c',
?                                              ^

+  'simple_filefinder\\partialignored\\found.c']
?                                              ^

-  'simple_filefinder\\partialignored\\thisoneisignored\\notfound.c']

----------------------------------------------------------------------
Ran 28 tests in 0.166s

FAILED (failures=1)

Support for pyproject.toml

Currently the pyproject.toml is the new way of configuring Python projects. Also tools like black, ruff and many other tools offer possibility to configure tools behavior by using pyproject.toml. Would it possible to offer similar support for pss? Example -A, -B, -C, --color, --ignore-dir and many others would be nice to have defaults from the toml file.

pss --smart-case problem with 143

$ pss 'yapf_cmd = '
./gdc-pyFormat.py
360:yapf_cmd = Ppf.yapf_api.FormatFile

$ pss --smart-case 'yapf_cmd = '
<<unexpected error: '>=' not supported between instances of 'int' and 'str'>>

Same effect whereever I use option '--smart-case', also combined with other otions.

NB: This is my first github-issue, so excuse if not appropriate
Best regards

Support ackrc

Migrated from issue #42 on bitbucket

I love pss as a drop-in replacement - who wants to install Perl? But it odesn't support config files yet - it'd be great it if supported the "standard" ackrc.

Thanks for a great clone!

by default line number is not colored

Not sure if this is a deliberate design decision. It's different compared to ack, ripgrep, silver-searcher.

diff --git a/psslib/defaultpssoutputformatter.py b/psslib/defaultpssoutputformatter.py
index 904a6b8..c1ef337 100644
--- a/psslib/defaultpssoutputformatter.py
+++ b/psslib/defaultpssoutputformatter.py
@@ -46,7 +46,8 @@ class DefaultPssOutputFormatter(OutputFormatter):
         self.style_filename = (decode_colorama_color(filename_color_str) or
                                colorama.Fore.MAGENTA + colorama.Style.BRIGHT)
         self.style_lineno = (decode_colorama_color(lineno_color_str) or
-                             colorama.Fore.WHITE)
+                            colorama.Fore.GREEN + colorama.Back.BLACK)
+        #                     colorama.Fore.WHITE)

         # colorama.init is doing something strange on Linux, by emitting a color
         # reset sequence when it shouldn't. This causes strange errors when

is ripgrep-like.

Running with no args shouldn't print help

Command-line tools should generally be safe to run with no arguments. Spewing help all over your terminal (and thus hiding your history/interrupting your flow) is quite unsafe. Please change it to just tell the user to use -h for usage in that case.

Syntax aware searcher

Hi Eli. Does pss tool support syntax-aware search that can look inside Python strings only? I need to find a specific string value, but unfortunately it is also a rather common word for my codebase. I could parse AST myself, but it is not practical for daily usage.

Support omitting line numbers in output

I'd like to copy-paste pss output elsewhere but line numbers in the beginning of each line are on a way. Easy to remove them afterwards, but using something like --noline would obviously be even easier.

--noline would be consistent with the current --column. For consistency/symmetry reasons it might be good idea to support also --line and --nocolum.

Easier support for custom file extensions

I'm working with custom file extension .robot (test data files for http://robotframework.org) and by default pss doesn't, as designed, look into them. I can simply use -a to look everywhere, but if I want to look only to those files I need to use -aG '.*\.robot'. Not so nice if you are using pss with such files a lot. There are obviously many other custom file types too, for example, .tmpl is mentioned issue #1 comments.

It would be convenient if there was an option to easily limit searching to a certain file extension. Basically I would like to use something like -E robot instead of -aG '.*\.robot'. Notice that I would like both to avoid writing a regexp when I don't really need it (robot vs. '.*\.robot') and would like -a to be implied. (I was rather surprised I needed to use it with -G, might be a good idea to mention that in its docs.)

I'm willing to implement this functionality myself. Mainly wanted to submit an issue first to hear what you think about it before starting to create a pull request. Should also decide what is a good short option for this usage and is long option needed too.

I'm also looking for a way to add these custom files into a list of files that pss looks by default. That's definitely a topic for a separate issue, most likely my needs are already covered by #1.

Easily iterate over and search git branches

This is a common problem I wish pss solved.

  • Imagine I have a python class instance method in git REPO_01. This repo has several git branches.

  • Imagine my python in git REPO_02 has many git branches, only some of them import the class from REPO_01

Assume I git clone both repos in a directory on my laptop; assume the directory is called ~/project_foo.

How can I iterate over and search all branches and all files of REPO_01 and REPO_02 (which have multiple levels of subdirectories) for a method string, "do_useful_thing_01"?

If I am right, pss can't do this today without installing xargs.

Add support for line number coloring like ack (--color-lineno)

I find ack's output with colored line numbers slightly easier to read, so I added support for that via --color-lineno in this commit -- feel free to take it if you like.

The default is unchanged (gray just like the standard pss output). Ideally it'd be bright yellow by default, but I'll leave that up to you.

FWIW, ack's bright green for filenames is much easier to read (on Windows? my machine?) than pss's magenta. But that's already overridable via --color-filename.

Add option to ignore minified javascript

Minified javascript is easy to detect, when search result is on the line whose length is longer than e.g 1000 characters (majority of style guides recommend less than 80) that result is probably from minified file.

Coloring doesn't work in Windows with certain command-line arguments

For example, coloring works as expected with no command-line arguments:

pss foo

But with certain command line arguments pss doesn't do any coloring:

pss --context=1 foo

However, some command line arguments do still color correctly, like pss -a foo.

I'm using pss on Python 2.7.5, Windows 7 64-bit.

Add support for ack's --type-add / --type-set or similar

Some projects I work on use a bunch of not-very-standard file extensions, such as .tmpl for mixed HTML templates. With ack I could use --type-add html=.tmpl to include this.

It would be great to have this feature in pss. It doesn't need to be permanent, because I always run pss via a customized script with my own options anyway (essentially an ackrc replacement).

One can kind of achieve this behaviour with a combination of -a and -G, but then it only searches .tmpl files, whereas I want to include them in all my searches by default. Basically --type-add would add to TYPE_MAP.

Let me know if you'd like a patch for this. I think it'd be pretty straight-forward to add these options and have them adjust TYPE_MAP.

Return values

I'm using pss in a shell script and want to do different actions depending are there matches or not. I can do that based on does pss write anything to stdout:

if [[ -n "$(pss pattern location)" ]]; then 
     echo match
fi

It would be a bit more convenient to do this based on the return value:

if pss patten location > /dev/null; then
    echo match
fi

Because pss returns 0 regardless does it match anything or not, the above always echos "match". I don't know about ack, but at least grep returns 0 if there is a match, 1 if there is no match, and 2 if there is an error. I would be convenient if pss followed these rules.

Command line parser doesn't support multiple occurrences of --include-pattern or --exclude-pattern

If --include-pattern or --exclude-pattern are given more than once only the last occurrence is taking effect, all others are effectively ignored.

This is sort of counter intuitive. I expected the following invocation:

pss --include-pattern=A --include-pattern=B

to be equivalent to

pss --include-pattern='(A|B)'

I know that since pss supports Python's regular expression syntax this seems like a mere question of preference, but there exist use cases where the combination behavior is desired.

Example
I use vim (neovim) as my editor of choice. Vim supports the grepprg option, that allows to specify an external program to be used for vim's grep command. I prefer to use pss for this, because I am more familiar with PCRE. In addition it is possible to provide the command with command line options that will always be used. Since I develop Qt applications I am dealing with a lot of auto-generated files of the form *moc_.cpp. These files are only cluttering the search results so I want them excluded. The problem is when I provide this exclude pattern as part of the default command invocation it is not possible to use --exclude-pattern again.
So I am left with one of two options:

  1. Write --exclude-pattern='moc_.*cpp' everytime I search something
  2. Accept that my search results are cluttered when I use another exclude pattern in a search

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.