Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I add simple script output to my bar? #172

Open
winteriscariot opened this issue May 6, 2024 · 1 comment
Open

How do I add simple script output to my bar? #172

winteriscariot opened this issue May 6, 2024 · 1 comment

Comments

@winteriscariot
Copy link

winteriscariot commented May 6, 2024

I believe this is what the scanner option is for? However, the instructions on how to use the scanner option are unclear. I've attempted to include various things in my "taskbar" declaration but either nothing happens, or I've got incorrect options and sfwbar segfaults. I've checked all the example configs and none of those really provide a lot of hints either.

The docs show the following in the Scanner section:

scanner {
  file("/proc/swaps",NoGlob) {
    SwapTotal = RegEx("[\t ]([0-9]+)")
    SwapUsed = RegEx("[\t ][0-9]+[\t ]([0-9]+)")
  }
  exec("getweather.sh") {
    $WeatherTemp = Json(".forecast.today.degrees")
  }
}

but that has no relation to the bar itself? I don't understand what the docs are showing me. How would you indicate the position in the bar of that "getweather.sh" script as shown in the example?

I just want to show CPU load and RAM usage on the far right hand side of the taskbar. I have a simple bash script to spit this out as a simple string. How can I add this to sfwbar?

@LBCrion
Copy link
Owner

LBCrion commented May 6, 2024

First thing, if you manage to get sfwbar to segfault with a dodgy config, please let me know (and post the config that did it if possible)! It certainly shouldn't happen and I would like to get rid of any bugs that cause it. Hopefully the config parser update I'm working on should get rid of most of these segfaults.

As for the question itself, there is the usual way of doing this (i.e. running the script over an over) and the better way (which is why scanner has been written to begin with).

If all you want to do is to display an output of a script, it's easy:

scanner {
  exec("/path/to/script.sh") {
    MyVar = Grab()
  }
}

layout {
  ...
  label {
    value = $MyVar
    interval = 1000
  }
}

This will update value of a label every second and since it references MyVar which is a scanner variable it will execute the script /path/to/script.sh and populate MyVar (and the label) with it's output.

Now, the above is the UNIX way of doing these things and it's also a pretty horrible way of building a taskbar. You're running a script every second and the script is probably running cat, grep, cut etc every time. This takes up a lot of resources just spawning processes for all these commands, not to mention initializing them, parsing arguments etc.

The scanner system of sfwbar can perform most of the tasks involved in displaying and updating this information internally. Let's say, you want to display CPU utilization, you can do something like this:

scanner {
   file("/proc/stat") {
    CpuUser = RegEx("^cpu [\t ]*([0-9]+)",Sum)
    CpuNice = RegEx("^cpu [\t ]*[0-9]+ ([0-9]+)",Sum)
    CpuSystem = RegEx("^cpu [\t ]*(?:[0-9]+ ){2}([0-9]+)",Sum)
    CpuIdle = RegEx("^cpu [\t ]*(?:[0-9]+ ){3}([0-9]+)",Sum)
  }
}

layout {
  ...
  label {
    value = "CPU: " + Str((CpuUser - CpuUser.pval)/(CpuUser + CpuNice + CpuSystem + CpuIdle - CpuUser.pval + CpuNice.pval + CpuSytem.pval - CpuIdle.pval)*100) + "%"
    interval = 1000
  }
}

This config works similar to the first one, but instead of running a script every second, it opens /proc/stat and parses it's contents. While parsing, it will look for lines matching regular expressions (in the above case lines beginning with "cpu") and populate variables with the contents of the first capture buffer in each regular expression. So, CpuUser will be the first number after "cpu", CpuNice will be the second number etc.

Each scan variable stores it's latest value, but also it's value from the previous update in .pval suffix. So expression CpuUser - CpuUser.pval will tell you how much CPU time was spent in user state between the last two updates. The label expression computes what percentage of the total time was spent in the User state, i.e. the CPU utilization. So this config gives you CPU utilization without having to spawn a single process. Everything is done entirely within the sfwbar executable.

If you want to look at the examples of setting up scanners for various things, you can look at cpu.source, memory.source and swap.source in default sfwbar configs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants