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

Add custom system monitor option #54

Open
wants to merge 4 commits into
base: feature/v27
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"globals": {
"log": "readonly",
"logError": "readonly",
"TextDecoder": "readonly"
"TextDecoder": "readonly",
"Intl": "readonly",
"console": "readonly"
}
}
75 changes: 50 additions & 25 deletions po/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,110 @@
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: gnome-runcat-extension 26\n"
"Project-Id-Version: gnome-runcat-extension 27\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-10-04 10:53+0300\n"
"POT-Creation-Date: 2024-03-21 00:13+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: src/indicator.js:161
#: src/indicator.js:179
msgid "Open System Monitor"
msgstr ""

#: src/indicator.js:165
#: src/indicator.js:189
#, javascript-format
msgid "Execution of “%s” failed"
msgstr ""

#: src/indicator.js:197
msgid "Settings"
msgstr ""

#: src/indicator.js:201
msgid "Failed to open extension settings"
msgstr ""

#: src/prefs.js:54
msgid "RunCat Settings"
msgstr ""

#: src/prefs.js:105
#: src/prefs.js:138
msgid "Version"
msgstr ""

#: src/resources/ui/preferences.ui:6
#: src/resources/ui/preferences.ui:6 src/resources/ui/preferences.ui:10
msgid "General"
msgstr ""

#: src/resources/ui/preferences.ui:10
msgid "General Preferences"
msgstr ""

#: src/resources/ui/preferences.ui:14
msgid "Idle threshold"
msgstr ""

#: src/resources/ui/preferences.ui:34
msgid "Displaying items"
msgstr ""

#: src/resources/ui/preferences.ui:38
#: src/resources/ui/preferences.ui:18
msgid "Character and percentage"
msgstr ""

#: src/resources/ui/preferences.ui:39
#: src/resources/ui/preferences.ui:19
msgid "Percentage only"
msgstr ""

#: src/resources/ui/preferences.ui:40
#: src/resources/ui/preferences.ui:20
msgid "Character only"
msgstr ""

#: src/resources/ui/preferences.ui:51
#: src/resources/ui/preferences.ui:30
msgid "Use a custom system monitor"
msgstr ""

#: src/resources/ui/preferences.ui:33
msgid "Command"
msgstr ""

#: src/resources/ui/preferences.ui:45
msgid "Character"
msgstr ""

#: src/resources/ui/preferences.ui:49
msgid "Invert speed"
msgstr ""

#: src/resources/ui/preferences.ui:55
msgid "Idle threshold"
msgstr ""

#: src/resources/ui/preferences.ui:56
msgid "Available when speed is not inverted"
msgstr ""

#: src/resources/ui/preferences.ui:74
msgid "Reset preferences"
msgstr ""

#: src/resources/ui/preferences.ui:52
#: src/resources/ui/preferences.ui:75
msgid "Reset RunCat preferences to defaults"
msgstr ""

#: src/resources/ui/preferences.ui:65
#: src/resources/ui/preferences.ui:88
msgid "Reset"
msgstr ""

#: src/resources/ui/preferences.ui:84
#: src/resources/ui/preferences.ui:107
msgid "The cat tells you the CPU usage by running speed"
msgstr ""

#: src/resources/ui/preferences.ui:88
#: src/resources/ui/preferences.ui:111
msgid "Visit RunCat's GitHub page"
msgstr ""

#: src/resources/ui/preferences.ui:98
#: src/resources/ui/preferences.ui:121
msgid "Visit Homepage"
msgstr ""

#: src/resources/ui/preferences.ui:102
#: src/resources/ui/preferences.ui:125
msgid "About RunCat"
msgstr ""
71 changes: 48 additions & 23 deletions po/ru.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: gnome-runcat-extension 20\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-10-04 10:43+0300\n"
"POT-Creation-Date: 2024-03-19 11:49+0300\n"
"PO-Revision-Date: 2022-09-30 19:16+0300\n"
"Last-Translator: Sergei Kolesnikov <[email protected]>\n"
"Language-Team: Russian\n"
Expand All @@ -18,76 +18,101 @@ msgstr ""
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && "
"n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)\n"

#: src/indicator.js:161
#: src/indicator.js:179
msgid "Open System Monitor"
msgstr "Открыть Системный монитор"

#: src/indicator.js:165
#: src/indicator.js:189
#, javascript-format
msgid "Execution of “%s” failed"
msgstr "Не удалось выполнить «%s»"

#: src/indicator.js:197
msgid "Settings"
msgstr "Настройки"

#: src/indicator.js:201
msgid "Failed to open extension settings"
msgstr "Не удалось открыть настройки расширения"

#: src/prefs.js:54
msgid "RunCat Settings"
msgstr "Настройки RunCat"

#: src/prefs.js:105
#: src/prefs.js:138
msgid "Version"
msgstr "Версия"

#: src/resources/ui/preferences.ui:6
#: src/resources/ui/preferences.ui:6 src/resources/ui/preferences.ui:10
msgid "General"
msgstr "Общие"

#: src/resources/ui/preferences.ui:10
msgid "General Preferences"
msgstr "Общие настройки"

#: src/resources/ui/preferences.ui:14
msgid "Idle threshold"
msgstr "Порог бездействия"

#: src/resources/ui/preferences.ui:34
msgid "Displaying items"
msgstr "Отображаемые элементы"

#: src/resources/ui/preferences.ui:38
#: src/resources/ui/preferences.ui:18
msgid "Character and percentage"
msgstr "Персонаж и проценты"

#: src/resources/ui/preferences.ui:39
#: src/resources/ui/preferences.ui:19
msgid "Percentage only"
msgstr "Только проценты"

#: src/resources/ui/preferences.ui:40
#: src/resources/ui/preferences.ui:20
msgid "Character only"
msgstr "Только персонаж"

#: src/resources/ui/preferences.ui:51
#: src/resources/ui/preferences.ui:30
msgid "Use a custom system monitor"
msgstr "Использовать пользовательский системный монитор"

#: src/resources/ui/preferences.ui:33
msgid "Command"
msgstr "Команда"

#: src/resources/ui/preferences.ui:45
msgid "Character"
msgstr "Персонаж"

#: src/resources/ui/preferences.ui:49
msgid "Invert speed"
msgstr "Инвертировать скорость"

#: src/resources/ui/preferences.ui:55
msgid "Idle threshold"
msgstr "Порог бездействия"

#: src/resources/ui/preferences.ui:56
msgid "Available when speed is not inverted"
msgstr "Доступен, когда скорость не инвертирована"

#: src/resources/ui/preferences.ui:74
msgid "Reset preferences"
msgstr "Сбросить настройки"

#: src/resources/ui/preferences.ui:52
#: src/resources/ui/preferences.ui:75
msgid "Reset RunCat preferences to defaults"
msgstr "Сбросить настройки RunCat к значениям по умолчанию"

#: src/resources/ui/preferences.ui:65
#: src/resources/ui/preferences.ui:88
msgid "Reset"
msgstr "Сбросить"

#: src/resources/ui/preferences.ui:84
#: src/resources/ui/preferences.ui:107
msgid "The cat tells you the CPU usage by running speed"
msgstr ""
"Котик, который показывает загрузку\n"
" процессора скоростью бега"

#: src/resources/ui/preferences.ui:88
#: src/resources/ui/preferences.ui:111
msgid "Visit RunCat's GitHub page"
msgstr "Посетить страницу RunCat на GitHub"

#: src/resources/ui/preferences.ui:98
#: src/resources/ui/preferences.ui:121
msgid "Visit Homepage"
msgstr "Открыть домашнюю страницу"

#: src/resources/ui/preferences.ui:102
#: src/resources/ui/preferences.ui:125
msgid "About RunCat"
msgstr "О RunCat"
5 changes: 5 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export const displayingItems = {
export const gioSettingsKeys = {
IDLE_THRESHOLD: 'idle-threshold',
DISPLAYING_ITEMS: 'displaying-items',
INVERT_SPEED: 'invert-speed',
customSystemMonitor: {
ENABLED: 'custom-system-monitor-enabled',
COMMAND: 'custom-system-monitor-command',
},
}

export const SYSTEM_MONITOR_COMMAND = 'gnome-system-monitor -r'
79 changes: 10 additions & 69 deletions src/dataProviders/cpu.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,28 @@
import Gio from 'gi://Gio'
import GTop from 'gi://GTop'

import { LOG_PREFIX } from '../constants.js'


try {
// eslint-disable-next-line no-underscore-dangle
Gio._promisify(Gio.File.prototype, 'load_contents_async', 'load_contents_finish')
} catch (e) {
logError(e)
}

/** @typedef {{ active: number, total: number }} CpuData */

/**
* @param {string} line
* @returns {CpuData}
*/
function parseCpuLine(line) {
const values = line.trim().split(/[\s]+/).slice(1)

// see `man proc`
const [
user,
nice,
system,
idle,
iowait,
irq, // eslint-disable-line
softirq,
steal,
guest, // eslint-disable-line
guestNice, // eslint-disable-line
] = values.map(n => parseInt(n, 10))

return {
active: user + system + nice + softirq + steal,
total: user + system + nice + softirq + steal + idle + iowait,
}
}
export const MAX_CPU_UTILIZATION = 1.0

/**
* @param {string} contents
*
* @returns {{ system: CpuData, specific: CpuData[] }}
* @returns {{ active: number, total: number }}
*/
function parseProcStatContents(contents) {
const [cpu, ...cpuN] = contents
.split('\n')
.filter(line => line.startsWith('cpu'))
function getCpuStats() {
const cpu = new GTop.glibtop_cpu()
GTop.glibtop_get_cpu(cpu)

return {
system: parseCpuLine(cpu),
specific: cpuN.map(parseCpuLine),
active: cpu.user + cpu.sys + cpu.nice,
total: cpu.total,
}

}

export default async function* () {
const procStatFile = Gio.File.new_for_path('/proc/stat')

let prevActive = 0
let prevTotal = 0

while (true) {
const [bytes] = await procStatFile.load_contents_async(null)
const contents = new TextDecoder('utf-8').decode(bytes)

const { active, total } = parseProcStatContents(contents).system

let utilization = 100 * ((active - prevActive) / (total - prevTotal))
if (Number.isNaN(utilization) || !Number.isFinite(utilization)) {
const data = JSON.stringify({ total, active, prevTotal, prevActive })
log(`${LOG_PREFIX}: cpu utilization is ${utilization}, data: ${data}`)

utilization = 0
}

if (utilization > 100) {
const data = JSON.stringify({ total, active, prevTotal, prevActive })
log(`${LOG_PREFIX}: cpu utilization is ${utilization}, data: ${data}`)

utilization = 100
}
const { active, total } = getCpuStats()
const utilization = (active - prevActive) / Math.max((total - prevTotal), MAX_CPU_UTILIZATION)

prevActive = active
prevTotal = total
Expand Down
Loading
Loading