cheat.sh icon indicating copy to clipboard operation
cheat.sh copied to clipboard

Multiple search terms in context, interpolation and running

Open BnGx opened this issue 5 years ago • 3 comments

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

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

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 tar_2

# 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

Calls cheat from curl: curl cheat.sh/~tar+~extract/X/ARCHIVE_1='myfile.tar'

Or call cheat from cht.sh: Set variable for shell variable interpolation. ARCHIVE_1='myfile.tar' cht.sh -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: cht.sh -r 151

BnGx avatar Jan 10 '20 16:01 BnGx

Thank you for the ideas; all of them are really interesting, and I even already thought about some of them before.

By the way, the multiple search terms feature was even supported before, but it is disabled now. The syntax was: ~term1~term2~term3, where the terms were combined with AND.

I am not sure, that I've got the idea with the extended search, because currently comments are searched through, e.g.:

$ curl cht.sh/~subvolume
 btrfs 
# create the subvolume /mnt/sv1 in the /mnt volume
btrfs subvolume create /mnt/sv1

# list subvolumes
btrfs subvolume list /mnt

# mount subvolume without mounting the main filesystem
mount -o subvol=sv1 /dev/sdb /mnt

# delete subvolume
btrfs subvolume delete /mnt/sv1

# taking snapshot of a subvolume
btrfs subvolume snapshot /mnt/sv1 /mnt/sv1_snapshot

I very like your idea with interpolation, but I think we should experiment with it and find the best syntax for that. For example, maybe it should be possible to overwrite parameters without specifying their names, just according to their possitions:

curl cht.sh/~tar~extract=/path/to/foo.tar

I use here = as a separator; may be it would be better to use , instead. The command translates to

tar xf /path/to/foo.tar

Also, I think that it would be better just to paste the command into the bash prompt instead of automatically executing it, and use some rotate-through combination to go trough the list of fitting commands. And it would be even cooler to use already specified arguments from the command line as positional arguments.

Say, you write:

$ extract tar file ~ /abc/1.tar

and you press some combination (~ is a separator between the search query and the arguments) and you get some list of the possible commands. Then you can edit it and then run.

What do you think?

chubin avatar Jan 11 '20 22:01 chubin

Hi chubin, Why the multiple search terms feature is now disabled? It would be very nice to filter the results using the query ~term1~term2~term3 to achieve term1 AND term2 AND term3. There could be cases where the output is very verbose and confusing because there are many results, for example: $ curl cht.sh/~extract~7z

<... more, and more results ...>

# Extract a 7z file.
p7zip --decompress ~/archive.7z

# Extract a 7z file.
7za e archive.7z

<... more, and more results ...>

I also made the following considerations, if the output isn't filtered:

  • There is an increase in the amount of data to be transferred;
  • It is more difficult to use in a script by less experienced users;
  • A team may be bound to use only certain tools;
  • Clearer and more direct output increases productivity.

So, since the search is performed in the command and the associated description: $ curl cht.sh/~p7zip~extract~7z the output would be the following:

# Extract a 7z file.
p7zip --decompress ~/archive.7z

It would be:

  • Clear;
  • Simple;
  • For dummies;
  • For scripters;
  • For the team.
  • One word: WOW!

... and it opens the way for the copy into the clipboard of the only result obtained in order to paste it quickly ...

I also like the idea of interpolating variables because:

  • It get out of self-explanatory (fixed and unreal) file names (eg: /path/of/the/archive.tar, /path/to/extract);
  • It gives the user a clear idea of what to use and where to use it (eg: "${SOURCE}", "${DEST}").

The idea of ​​the positional arguments is VERY good and comfortable however is to be considered that scenarios may arise in which the user wants to interpolate only a specific variable because others are specified in some environment variables or for learning purposes, therefore the user would be forced to specify all the above arguments.

Furthermore, it should be considered that based on the sources used, especially in the on-premise cheat.sh installations, a cheatsheet could be implemented as: tar -C "${DEST}" -xf "${SOURCE}" and consequently produce this output: tar -C /home/user1/my_archive -xf myfile.tar

and another cheatsheet could be implemented as: tar -xf "${SOURCE}" -C "${DEST}" and consequently produce this output: tar -xf myfile.tar -C /home/user1/my_archive

In this case, positional interpolation could create unwanted effects, especially if the output is used in a script.

I also like the idea of rotating results, and perhaps the A command-line fuzzy finder project (link: https://github.com/junegunn/fzf) might be useful, even if I haven't understood the syntax well, are you saying: $ cht.sh extract tar file ~ /abc/1.tar ?

Please, you also consider to replace ~ for cht.sh client using shell style options: $ cht.sh -s extract tar file -v /abc/1.tar

I await your opinion.

BnGx avatar Jan 12 '20 12:01 BnGx

@BnGx I am really sorry for the long delay, it was pretty hard time at work, and then I tried to work through all the issues, comments, and mails, that gathered during this time. The cheat.sh project was on hold all this time, and there was no active development. I hope that it will be a little bit better now. Once again, please excuse me for the silence.

Back to your questions:

  1. Yes, I like the idea of automatic interpolation of the cheat sheets. I can't say at the moment how the workflow will look like, but probably it will be something like:
  • call without interpolation first to see the names of the parameters;
  • call with additional parameters once again (with interpolation).

@terminalforlife: what do you think about this feature? It is closely related to the topic, that we discussed at https://github.com/chubin/cheat.sheets/issues/101

  1. Why is the multi-words-search feature disabled? Short answer: because it is broken; I hope to fix it soon, and then activate it again.

  2. What I think about fzf usage in cht.sh? I think it is a good idea; I even experimented with it several months ago, and I found the results very very promising (especially in combination with the preview mode of fzf), so I think it should be integrated and used.

chubin avatar Apr 14 '20 21:04 chubin