Skip to content
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

Multiple search terms in context #522

Open
BnGx opened this issue Jan 7, 2020 · 0 comments
Open

Multiple search terms in context #522

BnGx opened this issue Jan 7, 2020 · 0 comments

Comments

@BnGx
Copy link

BnGx commented Jan 7, 2020

I propose to extend the behavior of the program in the following ways:

  • The search should also include multiple terms (Feature 1);
  • When you search a phrase in the dotfiles using the new context option (example: -x), the search should include the comments associated (Feature 2);
  • Shell variable interpolation in the search output, if variable exists and if using a new option (example: -n) (Feature 3);
  • Any entry shoud have an ID (Autoadded during the search phase), so you can execute easily the command using a new option (example: -r) (Feature 4).

Example of dotfiles.

cat tar

# To create an uncompressed archive:
tar -cvf /path/to/foo.tar /path/to/foo/

# To extract an uncompressed archive:
tar -xvf /path/to/foo.tar

cat my_tar

# To extract an archive.
# Tag: archive, extract
tar xf ${ARCHIVE_1}

# To extract an archive, verbose output.
# Tag: archive, extract
tar xvf ${ARCHIVE_2}

Example of search command

Set variable for shell variable interpolation
ARCHIVE_1='myfile.tar'

so, calls cheat program:
cheat -n -x tar extract

Expected output:

# To extract an uncompressed archive:
# ID: 143
tar -xvf /path/to/foo.tar

# Extracts an archive.
# Tag: archive, extract
# ID: 151
tar xf myfile.tar

# Extracts an archive, verbose output.
# Tag: archive, extract
# ID: 152
tar xvf ${ARCHIVE_2}

Note:

  • Dynamic ID appears in the output. The ID doesn't change if the search context (tag and loading path) remains the same;
  • Shell variable interpolarion is performed using variable defined previously.

Example of run command

To recall and execute a cheat:
cheat -r 151

Workaround

I wrote a small Python3 script to accomplish the described operations (Features 1 and 2), but it's dirty, without color, etc... It would be better if integrated into official Python and GO projects following the business logic of official projects.

cat cs.py

# Requirements:
# - Install cheat (available at link: https://github.com/cheat/cheat) into 'bin' directory.

# Import Python libraries.
import sys, subprocess, re

# Set variables.
terms = sys.argv[1:]
re_flags = re.MULTILINE + re.IGNORECASE

# Regular expressions.
line_re = r'(?:^.+\n)*'
term_start_re = r'^.*'
term_end_re = r'.*$\n'
filename_re = r'^\w+(?=:$)'

# Calls cheat.
data = subprocess.run(['cheat', '-s', terms[0]], capture_output=True).stdout.decode('utf-8')
dbs = re.findall(filename_re, data, re_flags)

data = ""
for db in dbs:
    data += '\n' + subprocess.run(['cheat', db], capture_output=True).stdout.decode('utf-8')

    # Filters output.
    for term in terms:
        term_re = term_start_re + term + term_end_re
        search_re = line_re + term_re + line_re
        data = re.findall(search_re, data, re_flags)
        data = '\n'.join(data)

# Prints result.
data = data.strip()
print(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants