Skip to content

Commit

Permalink
Lint fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Roy Golan <[email protected]>
  • Loading branch information
rgolangh committed Oct 14, 2024
1 parent bccb10f commit 12bfa91
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 62 deletions.
9 changes: 3 additions & 6 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export default [
globalThis: "readonly",
imports: "readonly",
Intl: "readonly",
log: "readonly",
logError: "readonly",
console: "readonly",
print: "readonly",
printerr: "readonly",
window: "readonly",
Expand All @@ -43,6 +42,7 @@ export default [
},

rules: {
"no-console": "off",
"array-bracket-newline": ["error", "consistent"],
"array-bracket-spacing": ["error", "never"],
"array-callback-return": "error",
Expand Down Expand Up @@ -82,10 +82,7 @@ export default [
}],

indent: ["error", 4, {
ignoredNodes: [
"CallExpression[callee.object.name=GObject][callee.property.name=registerClass] > ClassExpression:first-child",
],

SwitchCase: 1,
MemberExpression: "off",
}],

Expand Down
38 changes: 18 additions & 20 deletions src/extension.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
"use strict";

import Clutter from 'gi://Clutter';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import St from 'gi://St';
import Gio from 'gi://Gio';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import * as Dialog from 'resource:///org/gnome/shell/ui/dialog.js';
import * as ModalDialog from 'resource:///org/gnome/shell/ui/modalDialog.js';
import GObject from 'gi://GObject';
import Clutter from "gi://Clutter";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import St from "gi://St";
import Gio from "gi://Gio";
import * as PanelMenu from "resource:///org/gnome/shell/ui/panelMenu.js";
import * as PopupMenu from "resource:///org/gnome/shell/ui/popupMenu.js";
import * as Dialog from "resource:///org/gnome/shell/ui/dialog.js";
import * as ModalDialog from "resource:///org/gnome/shell/ui/modalDialog.js";
import GObject from "gi://GObject";

import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import {Extension} from "resource:///org/gnome/shell/extensions/extension.js";

import * as Podman from './modules/podman.js';
import * as Podman from "./modules/podman.js";

export default class ContainersExtension extends Extension {
/**
* enable is the entry point called by gnome-shell
*/
// eslint-disable-next-line no-unused-vars
enable() {
console.log(`enabling ${this.uuid} extension`);
this._indicator = new PanelMenu.Button(0.0, this.metadata.name, false);
Expand Down Expand Up @@ -49,7 +48,6 @@ export default class ContainersExtension extends Extension {
/**
* disable is called when the main extension menu is closed
*/
// eslint-disable-next-line no-unused-vars
disable() {
console.log("disabling containers extension");
this._indicator?.destroy();
Expand All @@ -58,8 +56,8 @@ export default class ContainersExtension extends Extension {
}

async _sync() {
this.podmanListenCmd = await Podman.newEventsProcess((containerEvent) => {
console.debug("container event for container " + containerEvent.name)
this.podmanListenCmd = await Podman.newEventsProcess(containerEvent => {
console.debug(`container event for container ${containerEvent.name}`);
this._renderMenu();
});
}
Expand All @@ -69,9 +67,9 @@ export default class ContainersExtension extends Extension {
const out = this.podmanListenCmd?.get_stdout_pipe();
await out.close_async(0, null, () => {});
await this.podmanListenCmd.force_exit();
console.debug("podman events process status " + this.podmanListenCmd.get_status());
console.debug(`podman events process status ${this.podmanListenCmd.get_status()}`);
} catch (e) {
console.error("cleaning up podman events subprocess failed" + e);
console.error(`cleaning up podman events subprocess failed ${e}`);
}
}

Expand Down Expand Up @@ -101,7 +99,7 @@ export default class ContainersExtension extends Extension {
if (containers.length > 0) {
containers.forEach(container => {
console.debug(container.toString());
this.menu.addMenuItem(new ContainerSubMenuItem(container, { extraInfo: this._settings.get_boolean("extra-info")}));
this.menu.addMenuItem(new ContainerSubMenuItem(container, {extraInfo: this._settings.get_boolean("extra-info")}));
});
} else {
this.menu.addMenuItem(new PopupMenu.PopupMenuItem("No containers detected"));
Expand All @@ -128,7 +126,7 @@ class ContainerSubMenuItem extends PopupMenu.PopupSubMenuMenuItem {
const actions = new PopupMenu.PopupBaseMenuItem({reactive: false, can_focus: false, style_class: "container-action-bar"});
actions.actor.set_x_expand(true);
actions.actor.set_x_align(Clutter.ActorAlign.END);
//this.insert_child_at_index(actions, 2);
// this.insert_child_at_index(actions, 2);
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());

const startBtn = createActionButton(() => container.start(), "media-playback-start-symbolic");
Expand Down Expand Up @@ -200,7 +198,7 @@ class ContainerSubMenuItem extends PopupMenu.PopupSubMenuMenuItem {
this.menu.addAction("Watch Statistics", () => container.stats());
this.menu.addAction("Copy Container Details", () => setClipboard(container.details()));
// the css nth- or last-of-type is probably not implemented in gjs
this.menu.box.get_children().at(-1).add_style_class_name("last-container-menu-item");
this.menu.box.get_children().at(-1).add_style_class_name("last-container-menu-item");
}
}

Expand Down
52 changes: 30 additions & 22 deletions src/modules/podman.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use strict";

import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import Gio from "gi://Gio";
import GLib from "gi://GLib";
import * as Main from "resource:///org/gnome/shell/ui/main.js";

const TERM_KEEP_ON_EXIT = true;
const TERM_CLOSE_ON_EXIT = false;
Expand All @@ -12,9 +12,11 @@ Gio._promisify(Gio.Subprocess.prototype,

let podmanVersion;

/** @returns {Container[]} list of containers as reported by podman */
// eslint-disable-next-line no-unused-vars
// @param {gio.settigs} settings
/**
* Get a list of containers
* @param {Gio.settings} settings - The extension settings
* @returns {Container[]} list of containers as reported by podman
*/
export async function getContainers(settings) {
if (podmanVersion === undefined) {
await discoverPodmanVersion();
Expand Down Expand Up @@ -134,8 +136,8 @@ class Container {
`Status: ${this.status}`,
`Image: ${this.image}`,
`Created: ${this.createdAt}`,
`Started: ${this.startedAt !== null ? this.startedAt : "never"}`
]
`Started: ${this.startedAt !== null ? this.startedAt : "never"}`,
];
if (this.Command !== null) {
containerDetails.push(`Command: ${this.command}`);
}
Expand All @@ -152,8 +154,9 @@ class Container {
}
}

/** discoverPodmanVersion fetches the podman version from cli */
// eslint-disable-next-line no-unused-vars
/**
* discoverPodmanVersion fetches the podman version from cli
*/
async function discoverPodmanVersion() {
let versionJson;

Expand Down Expand Up @@ -276,54 +279,59 @@ function runCommandInTerminal(terminal, command, containerName, args, keepOpenOn
GLib.spawn_command_line_async(cmdline);
console.debug(`command on ${containerName} terminated successfully`);
} catch (e) {
const errMsg = `Error occurred when running ${command} on container ${containerName}`;
const errMsg = `Error occurred when running ${command} on container ${containerName}.\nError: ${e}`;
Main.notify(errMsg);
console.error(errMsg);
}
}

/**
* start listening to podman events in a separate process, each event is a line read.
* @param {Function} onEvent - run onEvent function on every line read
* @returns {Gio.Subprocess} process - The process handle
*/
export async function newEventsProcess(onEvent) {
try {
const cmdline = "podman events --filter type=container --format '{\"name\": \"{{ .Name }}\"}'";
const [, argv] = GLib.shell_parse_argv(cmdline);
const process = Gio.Subprocess.new(argv,
Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE);

const process = Gio.Subprocess.new(argv, Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE);
const pipe = process.get_stdout_pipe();
await _read(pipe, onEvent);
return process;

} catch (e) {
console.error(e.message);
throw new Error("Error occurred when fetching containers");
}
}

/**
* Read the input straem as a json a apply the onEvent function on it
* @param {Gio.inputStream} inputStream - Input stream of an array of json messages, where each entry is a single event on a container. See "man podman-events".
* @param {Function} onEvent - Function to apply on each container event
*/
async function _read(inputStream, onEvent) {
const content = await inputStream.read_bytes_async(4096, GLib.PRIORITY_DEFAULT, null, (source, result) => {
await inputStream.read_bytes_async(4096, GLib.PRIORITY_DEFAULT, null, (source, result) => {
const rawjson = new TextDecoder().decode(source.read_bytes_finish(result).toArray());
console.debug("raw json answer " + rawjson);
console.debug(`raw json answer: ${rawjson}`);
if (rawjson === "") {
// no output is EOF, no need to continue processing
return;
}
const rawjsonArray = rawjson.split(/\n/);
rawjsonArray.forEach( j => {
rawjsonArray.forEach(j => {
if (j !== "") {
try {
const containerEvent = JSON.parse(j);
console.debug("firing callback on container event " + containerEvent);
console.debug(`firing callback on container event ${containerEvent}`);
onEvent(containerEvent);
} catch (e) {
console.error("json parse error " + e);
console.error(`json parse error ${e}`);
}
}
});
if (!source.is_closed()) {
// keep reading
_read(source, onEvent);
} else {
return;
}
});
}
Expand Down
28 changes: 14 additions & 14 deletions src/prefs.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import Gio from 'gi://Gio';
import Adw from 'gi://Adw';
import Gio from "gi://Gio";
import Adw from "gi://Adw";

import {ExtensionPreferences, gettext as _} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
import {ExtensionPreferences, gettext as _} from "resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js";

export default class ContainersPreferences extends ExtensionPreferences {
fillPreferencesWindow(window) {
window._settings = this.getSettings();

const page = new Adw.PreferencesPage({
title: _('General'),
icon_name: 'dialog-information-symbolic',
title: _("General"),
icon_name: "dialog-information-symbolic",
});
window.add(page);

const appearanceGroup = new Adw.PreferencesGroup({
title: _('Appearance'),
description: _('Configure the appearance of the extension'),
title: _("Appearance"),
description: _("Configure the appearance of the extension"),
});
page.add(appearanceGroup);

const extraInfoRow = new Adw.SwitchRow({
title: _('Extra Info'),
subtitle: _('Whether to show extra info of a container in name and the opened menu'),
title: _("Extra Info"),
subtitle: _("Whether to show extra info of a container in name and the opened menu"),
});
appearanceGroup.add(extraInfoRow);
window._settings.bind('extra-info', extraInfoRow, 'active', Gio.SettingsBindFlags.DEFAULT);
window._settings.bind("extra-info", extraInfoRow, "active", Gio.SettingsBindFlags.DEFAULT);

const behaviourGroup = new Adw.PreferencesGroup({
title: _('Behaviour'),
description: _('Configure the behaviour of the extension'),
title: _("Behaviour"),
description: _("Configure the behaviour of the extension"),
});
page.add(behaviourGroup);

const terminalRow = new Adw.EntryRow({
title: _('Terminal program with arguments'),
title: _("Terminal program with arguments"),
show_apply_button: true, // Allows user to apply the input
});
behaviourGroup.add(terminalRow);
window._settings.bind('terminal', terminalRow, 'text', Gio.SettingsBindFlags.DEFAULT);
window._settings.bind("terminal", terminalRow, "text", Gio.SettingsBindFlags.DEFAULT);
}
}

0 comments on commit 12bfa91

Please sign in to comment.