Skip to content

Commit f0d8021

Browse files
committed
Rewrite to support gnome-shell 45
1 parent 3555859 commit f0d8021

File tree

2 files changed

+178
-198
lines changed

2 files changed

+178
-198
lines changed

extension.js

Lines changed: 177 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -1,222 +1,210 @@
1-
const Clutter = imports.gi.Clutter;
2-
const St = imports.gi.St;
3-
const Main = imports.ui.main;
4-
// const Tweener = imports.ui.tweener;
5-
const Gio = imports.gi.Gio;
6-
const GLib = imports.gi.GLib;
7-
const ByteArray = imports.byteArray;
8-
9-
const ExtensionUtils = imports.misc.extensionUtils;
10-
const Me = ExtensionUtils.getCurrentExtension();
11-
12-
const PREFS_SCHEMA = 'org.gnome.shell.extensions.simplenetspeed';
13-
const refreshTime = 3;
14-
15-
let settings;
16-
let button, timeout;
17-
// let icon, iconDark;
18-
let ioSpeed;
1+
// @ts-ignore
2+
import Clutter from 'gi://Clutter'
3+
// @ts-ignore
4+
import St from 'gi://St';
5+
// @ts-ignore
6+
import Gio from 'gi://Gio'
7+
// @ts-ignore
8+
import GLib from 'gi://GLib'
9+
10+
// @ts-ignore
11+
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
12+
// @ts-ignore
13+
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
14+
15+
const REFRESH_TIME = 3;
16+
1917
let lastCount = 0, lastSpeed = 0, lastCountUp = 0;
20-
let mode; // 0: kbps 1: K/s 2: U:kbps D:kbps 3: U:K/s D:K/s 4: Total KB
21-
let fontmode;
2218
let resetNextCount = false, resetCount = 0;
2319

24-
function byteArrayToString(bytes) {
25-
if (global.TextDecoder) {
26-
return new TextDecoder().decode(bytes);
20+
export default class SimpleNetSpeedExtension extends Extension{
21+
enable() {
22+
const PREFS_SCHEMA = 'org.gnome.shell.extensions.simplenetspeed';
23+
// @ts-ignore
24+
this._settings = this.getSettings(PREFS_SCHEMA);
25+
26+
// 0: kbps 1: K/s 2: U:kbps D:kbps 3: U:K/s D:K/s 4: Total KB
27+
// default mode using bit (bps, kbps)
28+
this.mode = this._settings.get_int('mode');
29+
this.fontmode = this._settings.get_int('fontmode');
30+
console.log(`SimpleNetSpeed.enable: mode ${this.mode}, fontmode ${this.fontmode}`);
31+
32+
this.button = new St.Bin({
33+
style_class: 'panel-button',
34+
reactive: true,
35+
can_focus: true,
36+
x_expand: true,
37+
y_expand: false,
38+
track_hover: true
39+
});
40+
41+
// let icon, iconDark;
42+
this.ioSpeed = new St.Label({
43+
text: '---',
44+
y_align: Clutter.ActorAlign.CENTER,
45+
style_class: 'simplenetspeed-label'
46+
});
47+
48+
this.button.set_child(this.chooseLabel());
49+
this.button.connect('button-press-event', this.changeMode.bind(this));
50+
51+
Main.panel._rightBox.insert_child_at_index(this.button, 0);
52+
this.timeout = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, REFRESH_TIME, () => {
53+
return this.parseStat();
54+
});
2755
}
2856

29-
return imports.byteArray.toString(bytes);
30-
}
31-
32-
function init() {
33-
34-
}
35-
36-
function changeMode(widget, event) {
37-
// log(event.get_button());
38-
if (event.get_button() == 3 && mode == 4) { // right click: reset downloaded sum
39-
resetNextCount = true;
40-
parseStat();
41-
}
42-
else if (event.get_button() == 2) { // change font
43-
fontmode++;
44-
if (fontmode > 4) {
45-
fontmode=0;
57+
disable() {
58+
if (this.timeout) {
59+
GLib.source_remove(this.timeout);
60+
this.timeout = null;
4661
}
47-
settings.set_int('fontmode', fontmode);
48-
chooseLabel();
49-
parseStat();
62+
Main.panel._rightBox.remove_child(this.button);
63+
this.button.destroy();
64+
this.ioSpeed = null;
65+
this.button = null;
66+
this._settings = null;
5067
}
51-
else if (event.get_button() == 1) {
52-
mode++;
53-
if (mode > 4) {
54-
mode = 0;
68+
69+
changeMode(_widget, event) {
70+
console.log('SimpleNetSpeed.changeMode, event button:' + event.get_button());
71+
if (event.get_button() === 3 && this.mode === 4) { // right click: reset downloaded sum
72+
resetNextCount = true;
73+
this.parseStat();
74+
}
75+
else if (event.get_button() === 2) { // change font
76+
this.fontmode++;
77+
if (this.fontmode > 4) {
78+
this.fontmode = 0;
79+
}
80+
this._settings.set_int('fontmode', this.fontmode);
81+
this.chooseLabel();
82+
this.parseStat();
83+
}
84+
else if (event.get_button() === 1) {
85+
this.mode++;
86+
if (this.mode > 4) {
87+
this.mode = 0;
88+
}
89+
this._settings.set_int('mode', this.mode);
90+
this.chooseLabel();
91+
this.parseStat();
5592
}
56-
settings.set_int('mode', mode);
57-
chooseLabel();
58-
parseStat();
93+
console.log('SimpleNetSpeed.changeMode mode: ' + this.mode + ' font: ' + this.fontmode);
5994
}
60-
log('mode:' + mode + ' font:' + fontmode);
61-
}
6295

63-
function chooseLabel() {
64-
if (mode == 0 || mode == 1 || mode == 4) {
65-
styleName = 'simplenetspeed-label';
66-
}
67-
else { // 2 , 3
68-
styleName = 'simplenetspeed-label-w';
69-
}
96+
chooseLabel() {
97+
let styleName;
98+
if (this.mode === 0 || this.mode === 1 || this.mode === 4) {
99+
styleName = 'simplenetspeed-label';
100+
}
101+
else { // 2 , 3
102+
styleName = 'simplenetspeed-label-w';
103+
}
70104

71-
if (fontmode > 0) {
72-
styleName = styleName + '-' + fontmode;
73-
}
105+
if (this.fontmode > 0) {
106+
styleName = styleName + '-' + this.fontmode;
107+
}
74108

75-
ioSpeed.set_style_class_name(styleName);
76-
return ioSpeed;
77-
}
109+
this.ioSpeed.set_style_class_name(styleName);
110+
return this.ioSpeed;
111+
}
78112

79-
function parseStat() {
80-
try {
81-
let input_file = Gio.file_new_for_path('/proc/net/dev');
82-
83-
let [, contents, etag] = input_file.load_contents(null);
84-
contents = byteArrayToString(contents);
85-
let lines = contents.split('\n');
86-
87-
let count = 0;
88-
let countUp = 0;
89-
let line;
90-
91-
for (let i=0;i<lines.length;i++) {
92-
line = lines[i];
93-
line = line.trim();
94-
let fields = line.split(/\W+/);
95-
if (fields.length<=2) break;
96-
97-
if (fields[0] != "lo" &&
98-
!fields[0].match(/^ifb[0-9]+/) && // created by python-based bandwidth manager "traffictoll"
99-
!fields[0].match(/^lxdbr[0-9]+/) && // created by lxd container manager
100-
!fields[0].match(/^virbr[0-9]+/) &&
101-
!fields[0].match(/^br[0-9]+/) &&
102-
!fields[0].match(/^vnet[0-9]+/) &&
103-
!fields[0].match(/^tun[0-9]+/) &&
104-
!fields[0].match(/^tap[0-9]+/) &&
105-
!isNaN(parseInt(fields[1]))) {
106-
count = count + parseInt(fields[1]) + parseInt(fields[9]);
107-
countUp = countUp + parseInt(fields[9]);
113+
// eslint-disable-next-line complexity
114+
parseStat() {
115+
try {
116+
const input_file = Gio.file_new_for_path('/proc/net/dev');
117+
const [, contents] = input_file.load_contents(null);
118+
const lines = new TextDecoder().decode(contents).split('\n');
119+
120+
let count = 0;
121+
let countUp = 0;
122+
for (const line of lines) {
123+
const fields = line.trim().split(/\W+/);
124+
if (fields.length <= 2) break;
125+
126+
if (fields[0] !== "lo" &&
127+
!fields[0].match(/^ifb[0-9]+/) && // created by python-based bandwidth manager "traffictoll"
128+
!fields[0].match(/^lxdbr[0-9]+/) && // created by lxd container manager
129+
!fields[0].match(/^virbr[0-9]+/) &&
130+
!fields[0].match(/^br[0-9]+/) &&
131+
!fields[0].match(/^vnet[0-9]+/) &&
132+
!fields[0].match(/^tun[0-9]+/) &&
133+
!fields[0].match(/^tap[0-9]+/) &&
134+
!isNaN(parseInt(fields[1], 10))) {
135+
count = count + parseInt(fields[1], 10) + parseInt(fields[9], 10);
136+
countUp = countUp + parseInt(fields[9], 10);
137+
}
108138
}
109-
}
110139

111-
if (lastCount === 0) lastCount = count;
112-
if (lastCountUp === 0) lastCountUp = countUp;
140+
if (lastCount === 0) lastCount = count;
141+
if (lastCountUp === 0) lastCountUp = countUp;
113142

114-
let speed = (count - lastCount) / refreshTime;
115-
let speedUp = (countUp - lastCountUp) / refreshTime;
143+
let speed = (count - lastCount) / REFRESH_TIME;
144+
let speedUp = (countUp - lastCountUp) / REFRESH_TIME;
116145

117-
// TEST
118-
// lastSpeed = 199899999;
119-
// speed= 1998999999;
120-
// speedUp= 99899999;
146+
// TEST
147+
// lastSpeed = 199899999;
148+
// speed= 1998999999;
149+
// speedUp= 99899999;
121150

122-
let dot = "";
123-
if (speed > lastSpeed) {
124-
dot = "⇅";
125-
}
151+
const dot = speed > lastSpeed ? "⇅" : "";
126152

127-
if (mode >= 0 && mode <= 1) {
128-
ioSpeed.set_text(dot + speedToString(speed));
129-
}
130-
else if (mode >= 2 && mode <= 3) {
131-
ioSpeed.set_text("↓" + speedToString(speed - speedUp) + " ↑" + speedToString(speedUp));
132-
}
133-
else if (mode == 4) {
134-
if (resetNextCount == true) {
135-
resetNextCount = false;
136-
resetCount = count;
153+
if (this.mode >= 0 && this.mode <= 1) {
154+
this.ioSpeed.set_text(dot + this.speedToString(speed));
137155
}
138-
ioSpeed.set_text("∑ " + speedToString(count - resetCount));
156+
else if (this.mode >= 2 && this.mode <= 3) {
157+
this.ioSpeed.set_text("↓" + this.speedToString(speed - speedUp) + " ↑" + this.speedToString(speedUp));
158+
}
159+
else if (this.mode === 4) {
160+
if (resetNextCount === true) {
161+
resetNextCount = false;
162+
resetCount = count;
163+
}
164+
this.ioSpeed.set_text("∑ " + this.speedToString(count - resetCount));
165+
}
166+
167+
lastCount = count;
168+
lastCountUp = countUp;
169+
lastSpeed = speed;
170+
} catch (e) {
171+
this.ioSpeed.set_text(e.message);
139172
}
140173

141-
lastCount = count;
142-
lastCountUp = countUp;
143-
lastSpeed = speed;
144-
} catch (e) {
145-
ioSpeed.set_text(e.message);
174+
return GLib.SOURCE_CONTINUE;
146175
}
147176

148-
return GLib.SOURCE_CONTINUE;
149-
}
150-
151-
function speedToString(amount) {
152-
let digits;
153-
let speed_map;
154-
if (mode == 0 || mode == 2) {
155-
speed_map = ["bps", "Kbps", "Mbps", "Gbps"];
156-
}
157-
else if (mode == 1 || mode == 3) {
158-
speed_map = ["B/s", "K/s", "M/s", "G/s"];
159-
}
160-
else if (mode == 4) {
161-
speed_map = ["B", "KB", "MB", "GB"];
162-
}
177+
speedToString(amount) {
178+
let digits;
179+
let speed_map = [];
180+
if (this.mode === 0 || this.mode === 2) {
181+
speed_map = ["bps", "Kbps", "Mbps", "Gbps"];
182+
}
183+
else if (this.mode === 1 || this.mode === 3) {
184+
speed_map = ["B/s", "K/s", "M/s", "G/s"];
185+
}
186+
else if (this.mode === 4) {
187+
speed_map = ["B", "KB", "MB", "GB"];
188+
}
163189

164-
if (amount === 0)
165-
return "0" + speed_map[0];
190+
if (amount === 0) {
191+
return "0" + speed_map[0];
192+
}
166193

167-
if (mode==0 || mode==2) amount = amount * 8;
194+
if (this.mode === 0 || this.mode === 2) amount = amount * 8;
168195

169-
let unit = 0;
170-
while (amount >= 1000) { // 1M=1024K, 1MB/s=1000MB/s
171-
amount /= 1000;
172-
++unit;
173-
}
196+
let unit = 0;
197+
while (amount >= 1000) { // 1M=1024K, 1MB/s=1000MB/s
198+
amount /= 1000;
199+
++unit;
200+
}
174201

175-
if (amount >= 100) // 100MB 100KB 200KB
202+
if (amount >= 100) // 100MB 100KB 200KB
176203
digits = 0;
177-
else if (amount >= 10) // 10MB 10.2
204+
else if (amount >= 10) // 10MB 10.2
178205
digits = 1;
179-
else
206+
else
180207
digits = 2;
181-
return String(amount.toFixed(digits)) + speed_map[unit];
182-
}
183-
184-
function enable() {
185-
settings = ExtensionUtils.getSettings(PREFS_SCHEMA);
186-
187-
mode = settings.get_int('mode'); // default mode using bit (bps, kbps)
188-
fontmode = settings.get_int('fontmode');
189-
190-
button = new St.Bin({
191-
style_class: 'panel-button',
192-
reactive: true,
193-
can_focus: true,
194-
x_expand: true,
195-
y_expand: false,
196-
track_hover: true
197-
});
198-
199-
ioSpeed = new St.Label({
200-
text: '---',
201-
y_align: Clutter.ActorAlign.CENTER,
202-
style_class: 'simplenetspeed-label'
203-
});
204-
205-
button.set_child(chooseLabel());
206-
button.connect('button-press-event', changeMode);
207-
208-
Main.panel._rightBox.insert_child_at_index(button, 0);
209-
timeout = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, refreshTime, () => {
210-
return parseStat();
211-
});
212-
}
213-
214-
function disable() {
215-
if (timeout) {
216-
GLib.source_remove(timeout);
217-
timeout = null;
208+
return String(amount.toFixed(digits)) + speed_map[unit];
218209
}
219-
Main.panel._rightBox.remove_child(button);
220-
button.destroy();
221-
settings = button = ioSpeed = null;
222210
}

0 commit comments

Comments
 (0)