Initial commit

This commit is contained in:
Kristofers Solo
2022-04-27 11:41:37 +03:00
commit f1d310a07d
700 changed files with 101758 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
# CPU widget
[![GitHub issues by-label](https://img.shields.io/github/issues-raw/streetturtle/awesome-wm-widgets/cpu)](https://github.com/streetturtle/awesome-wm-widgets/labels/cpu)
This widget shows the average CPU load among all cores of the machine:
![screenshot](./cpu.gif)
## How it works
To measure the load I took Paul Colby's bash [script](http://colby.id.au/calculating-cpu-usage-from-proc-stat/) and rewrote it in Lua, which was quite simple.
So awesome simply reads the first line of /proc/stat:
```bash
$ cat /proc/stat | grep '^cpu '
cpu 197294 718 50102 2002182 3844 0 2724 0 0 0
```
and calculates the percentage.
## Customization
It is possible to customize widget by providing a table with all or some of the following config parameters:
| Name | Default | Description |
|---|---|---|
| `width` | 50 | Width of the widget |
| `step_width` | 2 | Width of the step |
| `step_spacing` | 1 | Space size between steps |
| `color` | `beautiful.fg_normal` | Color of the graph |
| `enable_kill_button` | `false` | Show button which kills the process |
| `process_info_max_length` | `-1` | Truncate the process information. Some processes may have a very long list of parameters which won't fit in the screen, this options allows to truncate it to the given length. |
| `timeout` | 1 | How often in seconds the widget refreshes |
### Example
```lua
cpu_widget({
width = 70,
step_width = 2,
step_spacing = 0,
color = '#434c5e'
})
```
The config above results in the following widget:
![custom](./custom.png)
## Installation
Clone/download repo and use widget in **rc.lua**:
```lua
local cpu_widget = require("awesome-wm-widgets.cpu-widget.cpu-widget")
...
s.mytasklist, -- Middle widget
{ -- Right widgets
layout = wibox.layout.fixed.horizontal,
...
-- default
cpu_widget(),
-- or custom
cpu_widget({
width = 70,
step_width = 2,
step_spacing = 0,
color = '#434c5e'
})
...
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

View File

@@ -0,0 +1,339 @@
-------------------------------------------------
-- CPU Widget for Awesome Window Manager
-- Shows the current CPU utilization
-- More details could be found here:
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/cpu-widget
-- @author Pavel Makhov
-- @copyright 2020 Pavel Makhov
-------------------------------------------------
local awful = require("awful")
local watch = require("awful.widget.watch")
local wibox = require("wibox")
local beautiful = require("beautiful")
local gears = require("gears")
local CMD = [[sh -c "grep '^cpu.' /proc/stat; ps -eo '%p|%c|%C|' -o "%mem" -o '|%a' --sort=-%cpu ]]
.. [[| head -11 | tail -n +2"]]
-- A smaller command, less resource intensive, used when popup is not shown.
local CMD_slim = [[grep --max-count=1 '^cpu.' /proc/stat]]
local HOME_DIR = os.getenv("HOME")
local WIDGET_DIR = HOME_DIR .. '/.config/awesome/awesome-wm-widgets/cpu-widget'
local cpu_widget = {}
local cpu_rows = {
spacing = 4,
layout = wibox.layout.fixed.vertical,
}
local is_update = true
local process_rows = {
layout = wibox.layout.fixed.vertical,
}
-- Splits the string by separator
-- @return table with separated substrings
local function split(string_to_split, separator)
if separator == nil then separator = "%s" end
local t = {}
for str in string.gmatch(string_to_split, "([^".. separator .."]+)") do
table.insert(t, str)
end
return t
end
-- Checks if a string starts with a another string
local function starts_with(str, start)
return str:sub(1, #start) == start
end
local function create_textbox(args)
return wibox.widget{
text = args.text,
align = args.align or 'left',
markup = args.markup,
forced_width = args.forced_width or 40,
widget = wibox.widget.textbox
}
end
local function create_process_header(params)
local res = wibox.widget{
create_textbox{markup = '<b>PID</b>'},
create_textbox{markup = '<b>Name</b>'},
{
create_textbox{markup = '<b>%CPU</b>'},
create_textbox{markup = '<b>%MEM</b>'},
params.with_action_column and create_textbox{forced_width = 20} or nil,
layout = wibox.layout.align.horizontal
},
layout = wibox.layout.ratio.horizontal
}
res:ajust_ratio(2, 0.2, 0.47, 0.33)
return res
end
local function create_kill_process_button()
return wibox.widget{
{
id = "icon",
image = WIDGET_DIR .. '/window-close-symbolic.svg',
resize = false,
opacity = 0.1,
widget = wibox.widget.imagebox
},
widget = wibox.container.background
}
end
local function worker(user_args)
local args = user_args or {}
local width = args.width or 50
local step_width = args.step_width or 2
local step_spacing = args.step_spacing or 1
local color = args.color or beautiful.fg_normal
local background_color = args.background_color or "#00000000"
local enable_kill_button = args.enable_kill_button or false
local process_info_max_length = args.process_info_max_length or -1
local timeout = args.timeout or 1
local cpugraph_widget = wibox.widget {
max_value = 100,
background_color = background_color,
forced_width = width,
step_width = step_width,
step_spacing = step_spacing,
widget = wibox.widget.graph,
color = "linear:0,0:0,20:0,#FF0000:0.3,#FFFF00:0.6," .. color
}
-- This timer periodically executes the heavy command while the popup is open.
-- It is stopped when the popup is closed and only the slim command is run then.
-- This greatly improves performance while the popup is closed at the small cost
-- of a slightly longer popup opening time.
local popup_timer = gears.timer {
timeout = timeout
}
local popup = awful.popup{
ontop = true,
visible = false,
shape = gears.shape.rounded_rect,
border_width = 1,
border_color = beautiful.bg_normal,
maximum_width = 300,
offset = { y = 5 },
widget = {}
}
-- Do not update process rows when mouse cursor is over the widget
popup:connect_signal("mouse::enter", function() is_update = false end)
popup:connect_signal("mouse::leave", function() is_update = true end)
cpugraph_widget:buttons(
awful.util.table.join(
awful.button({}, 1, function()
if popup.visible then
popup.visible = not popup.visible
-- When the popup is not visible, stop the timer
popup_timer:stop()
else
popup:move_next_to(mouse.current_widget_geometry)
-- Restart the timer, when the popup becomes visible
-- Emit the signal to start the timer directly and not wait the timeout first
popup_timer:start()
popup_timer:emit_signal("timeout")
end
end)
)
)
--- By default graph widget goes from left to right, so we mirror it and push up a bit
cpu_widget = wibox.widget {
{
cpugraph_widget,
reflection = {horizontal = true},
layout = wibox.container.mirror
},
bottom = 2,
color = background_color,
widget = wibox.container.margin
}
-- This part runs constantly, also when the popup is closed.
-- It updates the graph widget in the bar.
local maincpu = {}
watch(CMD_slim, timeout, function(widget, stdout)
local _, user, nice, system, idle, iowait, irq, softirq, steal, _, _ =
stdout:match('(%w+)%s+(%d+)%s(%d+)%s(%d+)%s(%d+)%s(%d+)%s(%d+)%s(%d+)%s(%d+)%s(%d+)%s(%d+)')
local total = user + nice + system + idle + iowait + irq + softirq + steal
local diff_idle = idle - tonumber(maincpu['idle_prev'] == nil and 0 or maincpu['idle_prev'])
local diff_total = total - tonumber(maincpu['total_prev'] == nil and 0 or maincpu['total_prev'])
local diff_usage = (1000 * (diff_total - diff_idle) / diff_total + 5) / 10
maincpu['total_prev'] = total
maincpu['idle_prev'] = idle
widget:add_value(diff_usage)
end,
cpugraph_widget
)
-- This part runs whenever the timer is fired.
-- It therefore only runs when the popup is open.
local cpus = {}
popup_timer:connect_signal('timeout', function()
awful.spawn.easy_async(CMD, function(stdout, _, _, _)
local i = 1
local j = 1
for line in stdout:gmatch("[^\r\n]+") do
if starts_with(line, 'cpu') then
if cpus[i] == nil then cpus[i] = {} end
local name, user, nice, system, idle, iowait, irq, softirq, steal, _, _ =
line:match('(%w+)%s+(%d+)%s(%d+)%s(%d+)%s(%d+)%s(%d+)%s(%d+)%s(%d+)%s(%d+)%s(%d+)%s(%d+)')
local total = user + nice + system + idle + iowait + irq + softirq + steal
local diff_idle = idle - tonumber(cpus[i]['idle_prev'] == nil and 0 or cpus[i]['idle_prev'])
local diff_total = total - tonumber(cpus[i]['total_prev'] == nil and 0 or cpus[i]['total_prev'])
local diff_usage = (1000 * (diff_total - diff_idle) / diff_total + 5) / 10
cpus[i]['total_prev'] = total
cpus[i]['idle_prev'] = idle
local row = wibox.widget
{
create_textbox{text = name},
create_textbox{text = math.floor(diff_usage) .. '%'},
{
max_value = 100,
value = diff_usage,
forced_height = 20,
forced_width = 150,
paddings = 1,
margins = 4,
border_width = 1,
border_color = beautiful.bg_focus,
background_color = beautiful.bg_normal,
bar_border_width = 1,
bar_border_color = beautiful.bg_focus,
color = "linear:150,0:0,0:0,#D08770:0.3,#BF616A:0.6," .. beautiful.fg_normal,
widget = wibox.widget.progressbar,
},
layout = wibox.layout.ratio.horizontal
}
row:ajust_ratio(2, 0.15, 0.15, 0.7)
cpu_rows[i] = row
i = i + 1
else
if is_update == true then
local columns = split(line, '|')
local pid = columns[1]
local comm = columns[2]
local cpu = columns[3]
local mem = columns[4]
local cmd = columns[5]
local kill_proccess_button = enable_kill_button and create_kill_process_button() or nil
local pid_name_rest = wibox.widget{
create_textbox{text = pid},
create_textbox{text = comm},
{
create_textbox{text = cpu, align = 'center'},
create_textbox{text = mem, align = 'center'},
kill_proccess_button,
layout = wibox.layout.fixed.horizontal
},
layout = wibox.layout.ratio.horizontal
}
pid_name_rest:ajust_ratio(2, 0.2, 0.47, 0.33)
local row = wibox.widget {
{
pid_name_rest,
top = 4,
bottom = 4,
widget = wibox.container.margin
},
widget = wibox.container.background
}
row:connect_signal("mouse::enter", function(c) c:set_bg(beautiful.bg_focus) end)
row:connect_signal("mouse::leave", function(c) c:set_bg(beautiful.bg_normal) end)
if enable_kill_button then
row:connect_signal("mouse::enter", function() kill_proccess_button.icon.opacity = 1 end)
row:connect_signal("mouse::leave", function() kill_proccess_button.icon.opacity = 0.1 end)
kill_proccess_button:buttons(
awful.util.table.join( awful.button({}, 1, function()
row:set_bg('#ff0000')
awful.spawn.with_shell('kill -9 ' .. pid)
end) ) )
end
awful.tooltip {
objects = { row },
mode = 'outside',
preferred_positions = {'bottom'},
timer_function = function()
local text = cmd
if process_info_max_length > 0 and text:len() > process_info_max_length then
text = text:sub(0, process_info_max_length - 3) .. '...'
end
return text
:gsub('%s%-', '\n\t-') -- put arguments on a new line
:gsub(':/', '\n\t\t:/') -- java classpath uses : to separate jars
end,
}
process_rows[j] = row
j = j + 1
end
end
end
popup:setup {
{
cpu_rows,
{
orientation = 'horizontal',
forced_height = 15,
color = beautiful.bg_focus,
widget = wibox.widget.separator
},
create_process_header{with_action_column = enable_kill_button},
process_rows,
layout = wibox.layout.fixed.vertical,
},
margins = 8,
widget = wibox.container.margin
}
end)
end)
return cpu_widget
end
return setmetatable(cpu_widget, { __call = function(_, ...)
return worker(...)
end })

Binary file not shown.

After

Width:  |  Height:  |  Size: 255 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
id="svg7384"
version="1.1"
height="16"
width="16"
inkscape:version="0.48.5 r10040"
sodipodi:docname="window-close-symbolic.svg">
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="1366"
inkscape:window-height="723"
id="namedview15"
showgrid="true"
inkscape:zoom="14.75"
inkscape:cx="14.600061"
inkscape:cy="10.005214"
inkscape:window-x="0"
inkscape:window-y="23"
inkscape:window-maximized="1"
inkscape:current-layer="svg7384">
<inkscape:grid
type="xygrid"
id="grid2992" />
</sodipodi:namedview>
<title
id="title9167">Gnome Symbolic Icon Theme</title>
<metadata
id="metadata90">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title>Gnome Symbolic Icon Theme</dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs7386" />
<g
transform="translate(-60,-518)"
id="layer9"
style="display:inline" />
<g
transform="translate(-60,-518)"
id="layer10" />
<g
transform="translate(-60,-518)"
id="layer11" />
<g
id="g2996"
transform="matrix(0.75,0,0,0.75,2,2.0546875)">
<g
id="layer12"
transform="translate(-60,-518)">
<g
style="display:inline"
id="layer4-4-1"
transform="translate(19,-242)">
<path
style="font-size:medium;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-indent:0;text-align:start;text-decoration:none;line-height:normal;letter-spacing:normal;word-spacing:normal;text-transform:none;direction:ltr;block-progression:tb;writing-mode:lr-tb;text-anchor:start;color:#bebebe;fill:#bebebe;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:1.78124988;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:new;font-family:Andale Mono;-inkscape-font-specification:Andale Mono"
id="path10839-9"
d="m 45,764 1,0 c 0.01037,-1.2e-4 0.02079,-4.6e-4 0.03125,0 0.254951,0.0112 0.50987,0.12858 0.6875,0.3125 L 49,766.59375 51.3125,764.3125 C 51.578125,764.082 51.759172,764.007 52,764 l 1,0 0,1 c 0,0.28647 -0.03434,0.55065 -0.25,0.75 l -2.28125,2.28125 2.25,2.25 C 52.906938,770.46942 52.999992,770.7347 53,771 l 0,1 -1,0 c -0.265301,-10e-6 -0.530586,-0.0931 -0.71875,-0.28125 L 49,769.4375 46.71875,771.71875 C 46.530586,771.90694 46.26529,772 46,772 l -1,0 0,-1 c -3e-6,-0.26529 0.09306,-0.53058 0.28125,-0.71875 l 2.28125,-2.25 L 45.28125,765.75 C 45.070508,765.55537 44.97809,765.28075 45,765 l 0,-1 z"
inkscape:connector-curvature="0" />
</g>
</g>
</g>
<g
transform="translate(-60,-518)"
id="layer13" />
<g
transform="translate(-60,-518)"
id="layer14" />
<g
transform="translate(-60,-518)"
id="layer15" />
</svg>

After

Width:  |  Height:  |  Size: 3.6 KiB