5

I have this block in /etc/i3blocks.conf

[rss]
command=rsstail -n1 -1 -N -u https://news.ycombinator.com/rss | iconv -f UTF-8 -t ASCII//TRANSLIT | head -c 50
color=#00FF00
interval=300

I'd like to click on this block and open firefox with the correct URL.

Here is a block that opens firefox with URL.

[clickme]
align=center
full_text=Click me!
command=/usr/bin/firefox --new-tab https://www.stackexchange.com/

I don't know how to combine it together. Here is a documentation.

Here is how to get a link:

rsstail -l -n1 -1 -N -u https://news.ycombinator.com/rss | sed -n 2p

I probably will need to save a link of rsstail to some "block variable" and than use it in the command.

So far, I have this solution:

[rss]
command=rsstail -n1 -1 -N -u https://news.ycombinator.com/rss | iconv -f UTF-8 -t ASCII//TRANSLIT # | head -c 40
color=#00FF00
interval=300

[clickme]
align=center
full_text=*
command=firefox --new-tab `rsstail -l -n1 -1 -N -u https://news.ycombinator.com/rss | sed -n 2p`

It opens the link, when I click *. So, I'm not clicking on the title, but on the * next to the title. But the link may not be associated with the title, and this is what I don't want. I need to pass the link to some kind of variable (e.g. block variable or environment variable, whatever possible).

2 Answers 2

1

This was quite harder than I thought it would be. You have to create a separate bash script to make it happen. I presume you have correctly setup paths in your i3blocks config file.

Create a bash script for the feed

You need to create a bash read the information you need to display the topic and when clicked if would open correct web page.

Warning: There is a hidden limitation which could cause issues if you are not aware of it. The URL length can be maximum of 32 chars long as the instance variable is defined that way. If you want to use longer URL you need to use a URL shortener. (also mentioned in the script!)

I have created a bash script called rss and it is placed within a rss directory. My path is: ~/Build/i3blocks/blocklets/rss/rss.

In the i3blocks config I have a global command:

command=$SCRIPT_DIR/$BLOCK_NAME/$BLOCK_NAME

and in the i3 config I have:

bar {
       # status_command i3status
       status_command SCRIPT_DIR=~/Build/i3blocks/blocklets i3blocks
}

The rss script:

#!/usr/bin/env bash

# WARNING: This has a LIMIT of 32 characters for now!
# If your URL is longer you should use an URL shortener
# Caused by https://github.com/vivien/i3blocks/blob/01a4f4a9ca1e3d0b3c95d1d62000ad8506bdff1a/include/block.h#L50

# if BLOCK_INSTANCE is empty try a default value page instead
rss_url="${BLOCK_INSTANCE:-https://news.ycombinator.com/rss}"

if [[ "${url}" = "" ]]; then
  exit 1
fi

# get the title to show on bar
rss_title=$(rsstail -n1 -1 -N -u "${rss_url}" | iconv -f UTF-8 -t ASCII//TRANSLIT | head -c 40)
# the link to web page that will be displayed
rss_link=$(rsstail -l -n1 -1 -N -u "${rss_url}" | sed -n 2p)

if [[ "${rss_title}" = *404* ]]; then
  echo "${rss_url}"
  echo "${rss_url}"
  echo ""
  exit 1
fi

if [[ "${BLOCK_BUTTON}" -eq 1 ]]; then
  /usr/bin/firefox --new-tab "${rss_link}"
fi

echo "${rss_title}"
echo "${rss_title}"
echo ""

Add a block to your i3blocks config file

[rss]
instance=https://news.ycombinator.com/rss
label=
interval=300

Note: The label is an Unicode symbol for the RSS, I don't know why unix SE does not see it.

I have decided to share how the result looks at my end. First I saw the RSS feed and then I clicked on it:

RSS feed on the bar and after clicking o it

6
  • I need to make the first block in my question clickable. You created another block, so that the rsstail have to run twice and during time the link can change (not to be associated with the correct title)
    – xralf
    Commented May 10, 2023 at 13:15
  • @xralf I see well the logic is the same I'll adjust the answer so the first block will be clickable. But it will open multiple tabs as it gives Google I/O 2023.
    – tukan
    Commented May 10, 2023 at 13:22
  • @xralf Perhaps I don't fully understand the question...
    – tukan
    Commented May 10, 2023 at 13:48
  • Yes, I need to use the first block, which displays RSS title and than create for the same block onclick event (or use some variable, e.g. local block variables, or environment variables, whatever that is possible) with the link associated with the title.
    – xralf
    Commented May 10, 2023 at 14:08
  • @xralf I finally understand it now. That is longer than this, I'll delete the question and edit it tomorrow.
    – tukan
    Commented May 10, 2023 at 14:10
1
+250

Include onclick to start firefox with the url.

[rss]
command=rsstail -n1 -1 -N -u https://news.ycombinator.com/rss | iconv -f UTF-8 -t ASCII//TRANSLIT | head -c 50
color=#00FF00
interval=300
markup=pango
onclick=/usr/bin/firefox --new-tab "$(rsstail -l -n1 -1 -N -u https://news.ycombinator.com/rss | sed -n 2p)"

markup=pango enable the use of pango markup in the block's text, it is not required for basic text formatting but it is useful.

onclick event launches firefox with the url and running the rsstail command and extracting the second line of the output

Reload i3blocks to apply the changes pkill -RTMIN+10 i3blocks

Click on the rss block, it will start firefox with the correct url.

i3blocks-scripts

i3blocks-gaps pango markup color for label works with color name

20 Using the code from others to change your i3 look – i3 blocks

i3blocks-contrib

In i3blocks, how can a dynamic text be clickable?

Clickable i3block?

Try with separate block:

[start_firefox]
command=/usr/bin/firefox --new-tab https://news.ycombinator.com/
interval=once

[rss]
command=echo "<a href='signal:USR1:start_firefox'>$(rsstail -n1 -1 -N -u https://news.ycombinator.com/rss | iconv -f UTF-8 -t ASCII//TRANSLIT | head -c 50)</a>"
color=#00FF00
interval=300
markup=pango

Without <a href></a>:

[start_firefox]
command=/usr/bin/firefox --new-tab https://news.ycombinator.com/
interval=once

[rss]
command=$(rsstail -n1 -1 -N -u https://news.ycombinator.com/rss | iconv -f UTF-8 -t ASCII//TRANSLIT | head -c 50)
color=#00FF00
interval=300
markup=pango

Without head -c 50 and pango:

[start_firefox]
command=/usr/bin/firefox --new-tab https://news.ycombinator.com/
interval=once

[rss]
command=$(rsstail -n1 -1 -N -u https://news.ycombinator.com/rss | iconv -f UTF-8 -t ASCII//TRANSLIT)
color=#00FF00
interval=300

head -c 50 might cause a problem

Check if the commands run alone without error:

rsstail -n1 -1 -N -u news.ycombinator.com/rss | iconv -f UTF-8 -t ASCII//TRANSLIT | head -c 50 
rsstail -n1 -1 -N -u news.ycombinator.com/rss | iconv -f UTF-8 -t ASCII//TRANSLIT

Other errors:

  • Check the path for /usr/bin/firefox
  • Is --new-tab recognized, try with and without
  • Change the interval value of 300
  • Is the rss block in the correct format
  • Does rsstail command working correctly
  • Is iconv necessary
  • Can you run the commands separately
  • What version do you have
  • Do you see the blocks if you run i3blocks in the terminal
  • Did you edit the ~/.config/i3/config
4
  • I added your block to my /etc/i3blocks.conf and then i3-msg restart and Now I have error Error: status_command process exited unexpectadly (exit 1)
    – xralf
    Commented May 10, 2023 at 14:28
  • try again with 2 blocks
    – Z0OM
    Commented May 10, 2023 at 14:31
  • can you run the rsstail command alone to make sure there is no error? rsstail -n1 -1 -N -u news.ycombinator.com/rss | iconv -f UTF-8 -t ASCII//TRANSLIT | head -c 50
    – Z0OM
    Commented May 10, 2023 at 14:34
  • Your new answer only opens news.ycombinator.com after i3-msg restart it won't display anything in i3 bar.
    – xralf
    Commented May 10, 2023 at 14:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .