summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgpotter2 <10530980+gpotter2@users.noreply.github.com>2022-12-31 00:38:55 +0100
committerzachir <zachir@librem.one>2023-10-19 08:22:37 -0500
commit27972f9a634b91934ee01050aecd38a775b3e04c (patch)
tree0c54e8172859c304dedf7237782a2b49968d236c
parent07dd7e3f9dd57530e16ac644e15521f37c60773b (diff)
Fix ps: 'improper AIX field descriptor'
It appears that `ps` doesn't support using custom separators (e.g. here: '|') on all OSes. This replaces it with hardcoded lengths when known (cpu, mem, etc...). E.G. on a debian bookworm machine this is what it returns: ``` ps -eo '%p|%c|%C|' -o "%mem" -o '|%a' --sort=-%cpu error: improper AIX field descriptor ```
-rw-r--r--cpu-widget/cpu-widget.lua30
1 files changed, 10 insertions, 20 deletions
diff --git a/cpu-widget/cpu-widget.lua b/cpu-widget/cpu-widget.lua
index 11debe8..23b61f6 100644
--- a/cpu-widget/cpu-widget.lua
+++ b/cpu-widget/cpu-widget.lua
@@ -14,8 +14,8 @@ 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"]]
+local CMD = [[sh -c "grep '^cpu.' /proc/stat; ps -eo 'pid:10,pcpu:5,pmem:5,comm:30,cmd' --sort=-pcpu ]]
+ .. [[| grep -v [p]s | grep -v [g]rep | 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]]
@@ -33,17 +33,9 @@ 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
+-- Remove spaces at end and beggining of a string
+function trim(s)
+ return (s:gsub("^%s*(.-)%s*$", "%1"))
end
-- Checks if a string starts with a another string
@@ -242,13 +234,11 @@ local function worker(user_args)
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 pid = trim(string.sub(line, 1, 10))
+ local cpu = trim(string.sub(line, 12, 16))
+ local mem = trim(string.sub(line, 18, 22))
+ local comm = trim(string.sub(line, 24, 53))
+ local cmd = trim(string.sub(line, 54))
local kill_proccess_button = enable_kill_button and create_kill_process_button() or nil