Rofi Cheat Sheets

A Convenient Way to Access Programming Cheat Sheets.

Posted by Sebastián Valencia Sierra on April 23, 2023 · 6 mins read

If you're a programmer, you've probably encountered situations where you need to quickly look up a piece of syntax or a function. cheat.sh is a website that provides cheat sheets for many popular programming languages and tools, accessible through a simple curl command. However, typing out the full command every time you want to look something up can be tedious. That's where my tool comes in: a simple script that streamlines the process and makes it easier to access cheat.sh cheat sheets from a rofi menu.

Here's how it works: the first script takes in the user input (which should be a cheat.sh request space separated) and passes it to the curl command, which retrieves the cheat sheet from cheat.sh. The output is then piped to less, allowing you to scroll through it in the terminal window. The second script sets up a rofi menu that prompts you to enter a cheat.sh request, and then calls the first script to retrieve and display the cheat sheet in a separate terminal window of your choice (in my case, I'm using kitty).

Here's the code for the first script:

#!/usr/bin/env bash
i=1;
request=""
for param in "$@"
do
  if [ "$param" != "$1" ]; then
    request+="${param} ";
  fi
  i=$((i + 1));
done
request=$(echo "$request" | sed -r 's/[ ]+/+/g')
curl -s cheat.sh/"$1"/"$request" 2>&1 | \
less -RX

Here's a line-by-line explanation of the first script:

#!/usr/bin/env bash

This line specifies the shell to use for the script as bash. It is known as the "shebang" line and it tells the system what interpreter to use for executing the script.

i=1;
request=""

These lines initialize two variables - i and request. i is initialized to 1, and request is initialized to an empty string.

for param in "$@"
do

This starts a for loop that iterates over each command-line argument passed to the script.

if [ "$param" != "$1" ]; then

This line checks if the current argument being processed is not the first argument (i.e., the programming language for which the cheat sheet is being searched). The $1 variable refers to the first argument passed to the script.

request+="${param} ";

If the current argument is not the first argument, it is concatenated to the request variable with a space character.

  fi
  i=$((i + 1));
done

These lines close the if statement and increment the loop counter i.done marks the end of the for loop.

request=$(echo "$request" | sed -r 's/[ ]+/+/g')

This line replaces any space characters in the request variable with + symbols using the sed command, and stores the result back in the request variable.

curl -s cheat.sh/"$1"/"$request" 2>&1 | \
less -RX

The line containing the curl command sends an HTTP GET request to cheat.sh with the specified programming language and cheat sheet request. The -s flag makes the command silent, and the 2>&1 redirects error messages to standard output. The | symbol pipes the output of the command to the less command in the pipeline, which allows for scrolling through the output. The -R flag makes less display ANSI color escape sequences correctly, and the -X flag prevents the output from being cleared on exit.

Here's a line-by-line explanation of the second script:

string="$(rofi -dmenu -i -p "Cheat:" -theme ~/.config/rofi/launchers/cheat.rasi)"

This line launches a Rofi dmenu with the prompt "Cheat: " and a custom theme from the file located at ~/.config/rofi/launchers/cheat.rasi. The $(...) syntax is used to capture the output of the rofi command and store it in the variable string.

kitty --class kitty-helper -e $HOME/bin/cheat $(echo $string)

This line opens a new Kitty terminal window with the class kitty-helper, which can be used for identifying this window or configuring its appearance. The -e option is used to execute a command in the new window. The $(echo $string) part is used to pass the contents of the string variable as arguments to the first script.

So, overall, this script is used to launch a Rofi dmenu for user input, and then open a new Kitty terminal window to display the results of the cheat script with the user's input as arguments.

And this is how it looks the request python read csv.

In conclusion, the cheat script and the Rofi menu provide a simple and convenient way to search for programming-related information on cheat.sh, with the option to customize the appearance of the menu and the terminal window. The cheat script can be easily integrated into your workflow and adapted to your needs. Additionally, you can map a keybind to quickly access the cheat Rofi menu according to your window manager or desktop environment, making it even more efficient to use.

The scripts used in this tutorial can be found in the dotfiles repository on GitHub, along with other configuration files for various tools and applications.