C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
exercises

Table of Contents

Introduction

The purpose of the EXERCISES section is to provide you with practice problems similar to those you might encounter as a developer or system administrator. We are just barely getting started, so please be patient. The exercises will become more complex as we progress through the course.

As an added benefit, the exercises will provide a great test bed for debugging the C-Menu code. As always, your comments and suggestions (even complaints) are welcome.

Exercise 1: System Configurations

SDDM Background Configuration

The objective of this exercise is to Create a C-Menu Menu that allows users to safely and easily configure their system settings. We will start with a simple menu that allows users to select a new SDDM background image. This exercise assumes you have sddm installed and that the sddm theme is located in /usr/share/sddm/themes.

Requisites

  • A working C-Menu installation.
  • SDDM installed and configured on your system.
  • A collection of background images available in the SDDM theme directory. For this exercise, we will assume the SDDM theme is located at /usr/share/sddm/themes/sddm-corporate-theme/Backgrounds, and that they are of type Portable Network Graphics (*.png). This exercise assumes that the Backgrounds directory contains a default.png image that is currently being used as the SDDM background. For the sddm-corporate-theme, the background image is specified in Themes/corporate.conf as Background=Backgrounds/default.png. You may want to install the sddm-corporate-theme as it is set up perfectly for this exercise.

sddm-corporate-theme

1️⃣ In your editor, open a new file:

vi ~/menuapp/msrc/workstation_config.m

2️⃣ Add the following content to the file and save it:

: WORKSTATION CONFIGURATION
:
: Select SDDM Background
!pick -n 1 -T "Select SDDM Background" -S sddm_bg.sh -c "sddm_chbg.sh %%"
!help
: Exit Workstation Configuration
!return

3️⃣ Open another file to create a bash script:

vi ~/menuapp/bin/sddm_chbg.sh

4️⃣ Add the following content to the file, save it, and make it executable:

#!/bin/bash
U=$(id -un)
G=$(id -gn)
bkgd_dir="/usr/share/sddm/themes/sddm-corporate-theme/Backgrounds"
rsh -c "chown -R $U:$G $bkgd_dir"
cd "$bkgd_dir"
for bg in $(lf -e '.*default\.png$' . '.*\.png$'); do
echo $(basename "$bg")
done

This script will change the ownership of the SDDM background directory to the current user, allowing them to select a new background image. It will then list the available background images which will provide input for C-Menu Pick. Don't forget to make the script executable.

chmod +x ~/menuapp/bin/sddm_chbg.sh

5️⃣ Now, create another bash script:

vi ~/menuapp/bin/sddm_chbg.sh

6️⃣ Add the following content to the file, save it, and make it executable:

#!/bin/bash
U=$(id -un)
G=$(id -gn)
bkgd_dir="/usr/share/sddm/themes/sddm-corporate-theme/Backgrounds"
rsh -c "chown -R $U:$G $bkgd_dir"
cd "$bkgd_dir"
ln -sf "$1" default.png

Pick will execute this script, passing the selected background image as an argument. The script will create a symbolic link named default.png that points to the selected background image, effectively changing the SDDM background.

7️⃣ Finally, test your menu by running it from the command line:

menu ~/menuapp/msrc/workstation_config.m

8️⃣ You may want to add the Workstation Configuration option to your main menu. To do this, open your main menu file:

vi ~/menuapp/msrc/main.m

9️⃣ Add the following lines to include the Workstation Configuration menu:

: Workstation Configuration
!menu workstation_config.m

From the C-Menu main menu, you should now see an option for Workstation Configuration. Selecting it will take you to the menu where you can choose a new SDDM background image.

Select SDDM Background

To verify that the background has been changed, you can log out of your session and return to the SDDM login screen. You should see the new background image you selected. Or, you can check the symbolic link in the SDDM background directory to confirm it points to the correct image.

SDDM Backgrounds

The above image shows the SDDM background directory before and after selecting the space_force.png background image.

Ghostty Configuration

1️⃣ In this exercise, we will add Ghostty configurations for color themes and fonts to the Workstation Configuration. First, open the workstation_config.m file:

vi ~/menuapp/msrc/workstation_config.m

2️⃣ Add the following lines to the workstation_config.m to include options for Ghostty themes and fonts:

: Select Ghostty Theme
!pick -n 1 -T "Select Ghostty Theme" -S ghostty_themes.sh -c "ghostty_chtheme.sh %%"
: Select Ghostty Font
!pick -n 1 -T "Select Ghostty Font" -S ghostty_fonts.sh -c "ghostty_chfont.sh %%"

3️⃣ Next, create the ghostty_themes.sh and ghostty_chtheme.sh scripts:

vi ~/menuapp/bin/ghostty_themes.sh

4️⃣ Add the following content to the file, save it, and make it executable:

#!/bin/bash
U=$(id -un)
G=$(id -gn)
themes_dir="$HOME"/.config/ghostty/themes
rsh -c "chown -R $U:$G $themes_dir"
cd "$themes_dir"
for theme in $(lf -d 1 -t f -e '.*default_theme$' . '.*'); do
echo $(basename "$theme")
done

5️⃣ Now, create the ~/menuapp/bin/ghostty_chtheme.sh script, add the following content, save it, and make it executable.

#!/bin/bash
U=$(id -un)
G=$(id -gn)
themes_dir="$HOME"/.config/ghostty/themes
rsh -c "chown -R $U:$G $themes_dir"
cd "$themes_dir"
ln -sf "$1" default_theme

6️⃣ Finally, create the ghostty_fonts.sh and ghostty_chfont.sh scripts:

vi ~/menuapp/bin/ghostty_fonts.sh
#!/bin/bash
U=$(id -un)
G=$(id -gn)
fonts_dir="$HOME"/.config/ghostty/fonts
rsh -c "chown -R $U:$G $fonts_dir"
cd "$fonts_dir"
for font in $(lf -d 1 -t f -e '.*default_font$' . '.*'); do
echo $(basename "$font")
done
vi ~/menuapp/bin/ghostty_chfont.sh
#!/bin/bash
U=$(id -un)
G=$(id -gn)
fonts_dir="$HOME"/.config/ghostty/fonts
rsh -c "chown -R $U:$G $fonts_dir"
cd "$fonts_dir"
ln -sf "$1" default_font

7️⃣ Test your menu again by running it from the command line:

menu ~/menuapp/msrc/workstation_config.m

Or, if you have installed the ~/.bashrc configurations described in Augmenting C-Menu, you can just type mm to start C-Menu and navigate to the Workstation Configuration menu.

Alacritty and Kitty

The process for adding Alacritty and Kitty configurations is similar to the Ghostty configuration. You will need to create scripts to list available themes and fonts for Alacritty and Kitty, as well as scripts to change the default theme and font by creating symbolic links.

You may want to automate the creation of these scripts by using sed to replace "ghostty" with "alacritty" and "kitty" in the existing Ghostty scripts. Here's an example of how you can do this:

#!/bin/bash
sed 's/ghostty/kitty/g' ghostty_fonts.sh >kitty_fonts.sh
sed 's/ghostty/kitty/g' ghostty_chfont.sh >kitty_chfont.sh
sed 's/ghostty/kitty/g' ghostty_themes.sh >kitty_themes.sh
sed 's/ghostty/kitty/g' ghostty_chtheme.sh >kitty_chtheme.sh
chmod a+x kitty_*.sh
cp kitty_*.sh ~/menuapp/bin/
sed 's/ghostty/alacritty/g' ghostty_fonts.sh >alacritty_fonts.sh
sed 's/ghostty/alacritty/g' ghostty_chfont.sh >alacritty_chfont.sh
sed 's/ghostty/alacritty/g' ghostty_themes.sh >alacritty_themes.sh
sed 's/ghostty/alacritty/g' ghostty_chtheme.sh >alacritty_chtheme.sh
chmod a+x alacritty_*.sh
cp alacritty_*.sh ~/menuapp/bin/

Here is the final content of the workstation_config.m file after adding the Alacritty and Kitty configuration options:

: WORKSTATION CONFIGURATION
:
: Select SDDM Background
!pick -n 1 -T "Select SDDM Background" -S sddm_bg.sh -c "sddm_chbg.sh %%"
: Select Ghostty Font
!pick -n 1 -T "Select Ghostty Font" -S ghostty_fonts.sh -c "ghostty_chfont.sh %%"
: Select Ghostty Theme
!pick -n 1 -T "Select Ghostty Theme" -S ghostty_themes.sh -c "ghostty_chtheme.sh %%"
: Select Kitty Font
!pick -n 1 -T "Select Kitty Font" -S kitty_font.sh -c "kitty_chfont.sh %%"
: Select Kitty Theme
!pick -n 1 -T "Select Kitty Theme" -S kitty_themes.sh -c "kitty_chtheme.sh %%"
: Select Alacritty Font
!pick -n 1 -T "Select Alacritty Font" -S alacritty_fonts.sh -c "alacritty_chfont.sh %%"
: Select Alacritty Theme
!pick -n 1 -T "Select Alacritty Theme" -S alacritty_themes.sh -c "alacritty_chtheme.sh %%"
: Help
!help
: Exit Workstation Configuration
!return
Workstation Configuration

Rustlings Exercises

The objective of this exercise is to streamline the process of iteratively editing the Rustlings exercises with C-Menu Pick. Of course, there is a simpler and fully automatic way to do this, but this exercise is meant to give you practice with C-Menu Pick for similar projects without the built-in tracking that Rustlings provides. For example, see the following bash script:

#!/bin/bash
while true; do
{
vi $(find exercises -name $(sed -n '3p' .rustlings-state.txt).rs)
echo Edit $(sed -n '3p' .rustlings-state.txt).rs
if [ $(enterchr "Y or N") != "Y" ]; then
echo
exit 0
fi
}
done

But, don't use the above script. It hasn't been fully tested and there are probably instances in which it won't work. C-Menu Pick is more fun, and it provides a more controlled way to edit the Rustlings exercises.

Before starting this exercise, you will need to install the Rust toolchain and Rustlings. The easiest way to do this is as follows:

  • Install Rustup:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • Install Rustlings in your home directory:

See Rustlings


  • Create a bash script with the following content and save it in ~/menuapp/bin/rust_src.
lf rustlings -d 5 'exercises.*\.rs$' | sort
  • Add the following two lines to ~/menuapp/msrc/main.m if it isn't already there.
: Rustlings Source
!pick -S rust_src -n 1 -T "Rustlings Source - Edit" -c nvim.sh %%

Start Rustlings:

cd rustlings
rustlings
Rustlings A1

At the bottom of the rustlings display, you will notice the line, "Current exercise: " and the relative path of the Rust source file for the current exercise. This is the file you will be editing with C-Menu Pick.

Start C-Menu and select the Rustlings Source option. When it appears, press the tab key to activate the line editor. Type the last few characters of the file name in the line editor window. For example, "maps2".

Press the tab key again to activate the object selector. Select the line containing the file name that matches the current exercise. In this case, there is only one file that matches, so press the space bar to select it. The selected file will be opened in Neovim (or your preferred editor if you change nvim.sh) for editing.

Rustlings A2

Edit the Rustlings source file to correct the compiler errors and save it.

Rustlings 5

When you save the file, Rustlings will automatically check your work.

Rustlings 6

If you were successful, you can press "n" in the Rustlings terminal to advance to the next exercise.

Then, repeat the process of selecting the Rustlings Source option in C-Menu and editing the next exercise.

If the line editor refuses to accept a typed character, it is probably because of the preemptive error checking. If the character you are trying to enter would result in no matches, the line editor will reject it.

🐸 Enjoy using C-Menu! If you encounter any issues or have questions, feel free to open an issue on the C-Menu GitHub repository