• [i3 Medicine] Spoon #1 - A trash applet to your statusbar


    Intro:
    There are quite a few reasons why a Trash applet makes sense in a wm like i3. Despite using a tiling wm, many of us still make use of file managers. Either graphical or console, many of them include facilities to manage the Trash directory. On the other hand, there’s an uptick of the number of users installing the i3 WM on top of their current DE (Gnome, MATE, Xfce, etc).

    The following is a way to integrate Trash into i3, through your i3 bar that should work regardless of you using i3status, i3blocks, or any other scriptable bar like polybar that can accept signals.

    The following example used i3blocks. Adapt it if you use something different.

    Step 1: Create a systemd unit that monitors the Trash directory for changes
    We do no want our status bar to keep checking the Trash directory for changes every second or so. That’s wasteful. Instead we want to signal the status bar that it needs to update only when changes happen to the Trash directory. This is a much better use of computer resources and it also means any changes are instantly reported by the status bar, instead of we having to wait for some interval.

    So first create the .path unit file that will monitor the Trash directory for changes. This unit will look for any changes to the supplied directory:

    # $XDG_DATA_HOME/systemd/user/trash-dir.path
    [Unit]
    Description=Monitorization of changes to Trash
    
    [Path]
    PathModified=/home/your_user_name/.local/share/Trash/files
    
    [Install]
    WantedBy=paths.target
    

    PathModified requires a full path. So replace your_user_name appropriately to reflect your home dir.

    Do not replace the string user in the script path. The path is indeed .../systemd/user/trash-dir.path

    Next, create the .service file that the above unit will trigger when the Trash directory is changed (files are added or deleted).

    # $XDG_DATA_HOME/systemd/user/trash-dir.service
    [Unit]
    Description=Update i3blocks Trash block
    
    [Service]
    Type=oneshot
    ExecStart=/usr/bin/pkill -SIGRTMIN+1 i3blocks
    

    This is where our status bar will get informed that a change has occurred and it needs to be updated. In here we are simply sending a SIGUSR1 signal to i3blocks with the help of the pkill command.

    Step 1a: Fire up the new systemd units

    We need to tell systemd about our new units and get them started.

    First, let’s inform systemd we have new units:
    $ systemctl --user daemon-reload

    Now, let’s enable the .path unit, so it starts automatically on boot. This is the only one that needs to be enabled, since the .service one will instead be activated by it and only on demand:
    $ systemctl --user enable trash-dir.path

    Finally, let’s start it now:
    $ systemctl --user start trash-dir.path

    Step 2: Create the shell script that the status bar will use to display Trash information

    This is a regular shell script. Don’t forget to make it executable:

    $ cat .config/i3/blocks/trash 
    #!/usr/bin/env bash
    #: ------------- --------------------------------------------------------------
    #: i3 Blocklet  : trash, shows trash usage status, display trash contents on
    #:              :        left click, and empties the trash on right click
    #: Date         : 2017-10-08
    #: Author       : "Mario Figueiredo" <[email protected]>
    #: Version      : 1.0
    #: Requires     : xdg-utils, trash-cli
    #: Fonts        : Font Awesome
    #: Systemd      : user .path unit to monitor changes to trash directory
    #: ------------- --------------------------------------------------------------
    #: Revisions    : 1.0 - Initial version
    #: ------------- --------------------------------------------------------------
    
    loc="$XDG_DATA_HOME/Trash/files"
    trash=#b57474
    empty=#73a0d9
    ico=
    
    if [[ $(ls -A "$loc") ]]; then
        printf "<span foreground=\"%s\">%s</span>" $trash $ico
    else
        printf "<span foreground=\"%s\">%s</span>" $empty $ico
    fi
    
    case $BLOCK_BUTTON in
        1)
            # left mouse button; open trash location
            xdg-open "$loc" &> /dev/null ;;
        3)
            # right mouse button; call trash binding mode
            xdotool key Super_L+shift+t ;;
        *)
            ;;
    esac
    

    This script is of course tailored for i3blocks. But you should be able to adapt it to your status bar if you are using something else, provided it has support for mouse clicking.

    Make note of the dependencies (listed under Requires) and install them if you don’t have them already. trash-cli is btw quite a cool little utility that allows you to use the trash from your console.

    The $ico variable is simply the icon to be displayed. It shouldn’t display here on the web browser. But, so you know, I’m taking it from Font Awesome.

    xdg-open should open the Trash directory in your default file manager, if you have the proper entry in your ~/.config/mimeapps.list or under /usr/share/applications/mime.cache. Let me know if it is not working for you.

    Step 3: Configure your status bar

    And all we need to do now is to configure our status bar. Again this is different depending on what you are using. Here is what you need to add to the i3blocks config file:

    [trash]
    markup=pango
    command=~/.config/i3/blocks/trash
    interval=once
    signal=1
    

    markup=pango makes sure that the icon color is changed according to the html-like markup we defined in the script above (red for full, blue for empty).

    command= is self-explanatory. Again, remember to make the script executable: chmod +x ~/.config/i3/blocks/trash.

    interval=once means the script will be executed only once when i3blocks starts and no more. It doesn’t have a timer. This is what we want, because of the next directive…

    signal=1 is what will update this entry in the status bar. So, when systemd sends signal SIGRTMIN+1 to i3blocks (as we defined in step 1), the script is executed and the status bar updates the state of the Trash icon.

  • Wow! What an amazing guide:grinning:. This seems like great material for the Wiki here…

    Keep trying, never give up. In the end, you will find that it was all worth it

    My ISO:
    https://rebornos.wordpress.com
    Linux Basics: https://linuxbasicssite.wordpress.com

  • @Krugar
    @Keegan

    Did @karasu finally find an i3-wm maintainer? :P

    Antergos (default OS) - WIN10 (abandoned)
    I3wm - Mate desktop
    AMD - A4 7300 Radeon graphics
    16 GB ram
    HD 1 TB
    Linux newbie since 06/2016

  • Not that I know of… perhaps you could fill that slot if you so desire? (Just guessing at the intent behind your question here ;)).

    Keep trying, never give up. In the end, you will find that it was all worth it

    My ISO:
    https://rebornos.wordpress.com
    Linux Basics: https://linuxbasicssite.wordpress.com

Posts 4Views 143
Log in to reply