Skip to content

New modes and improvements #14

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

Open
wants to merge 2 commits into
base: master
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Simple net speed
Gnome extension to show network speed
# Simple net speed
Gnome extension to show network speed, version forked from https://github.com/biji/simplenetspeed, added last 2 functions, adjusted font-size and width and slight performance improvement.

https://extensions.gnome.org/extension/1085/simple-net-speed/

Expand All @@ -10,5 +10,7 @@ Simply showing network speed. Left click to change modes:
1. Up & down speed in bits per second
1. Up & down speed in Bytes per second
1. Total of downloaded in Bytes (Right click to reset counter)
1. Total of uploaded bytes (Right click to reset all counters)
1. Total bytes transferred (Right click to reset all counters)

Middle click to change font size
119 changes: 50 additions & 69 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,28 @@ const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;

const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Slider = imports.ui.slider;

const PREFS_SCHEMA = 'org.gnome.shell.extensions.simplenetspeed';
const refreshTime = 3.0;

let settings;
let button, timeout;
// let icon, iconDark;
let ioSpeed;
let lastCount = 0, lastSpeed = 0, lastCountUp = 0;
let mode; // 0: kbps 1: K/s 2: U:kbps D:kbps 3: U:K/s D:K/s 4: Total KB
let fontmode;
let resetNextCount = false, resetCount = 0;
let resetNextCount = false, resetCount = 0, resetCountUp = 0;
let speed_map;

function init() {

settings = Convenience.getSettings(PREFS_SCHEMA);

mode = settings.get_int('mode'); // default mode using bit (bps, kbps)
fontmode = settings.get_int('fontmode');
fontmode = settings.get_int('fontmode');

button = new St.Bin({
style_class: 'panel-button',
Expand All @@ -37,32 +41,44 @@ function init() {
track_hover: true
});

/*
icon = new St.Icon({
gicon: Gio.icon_new_for_string(Me.path + "/icons/harddisk.svg")
});
iconDark = new St.Icon({
gicon: Gio.icon_new_for_string(Me.path + "/icons/harddisk-dark.svg")
});*/

ioSpeed = new St.Label({
text: '---',
style_class: 'simplenetspeed-label'
style_class: 'simplenetspeed'
});

// ioSpeedStaticIcon = new St.Label({
// text: '💾',
// style_class: 'simplenetspeed-static-icon'
// });

button.set_child(chooseLabel());
button.set_child(ioSpeed);
button.connect('button-press-event', changeMode);
updateSpeedMap();
updateStyle();
}

function updateStyle(){
style = "font-size: " + (0.15*fontmode + 0.65) + "em;"; // parenthesis needed: math inside, strcat outside
ioSpeed.set_style(style);
}

function updateSpeedMap(){
if (mode == 0 || mode == 2) {
speed_map = ["bps", "Kbps", "Mbps", "Gbps"];
}
else if (mode == 1 || mode == 3) {
speed_map = ["B/s", "K/s", "M/s", "G/s"];
}
else if (mode >= 4 && mode <= 6) {
speed_map = ["B", "KB", "MB", "GB"];
}
}

function changeMode(widget, event) {
// log(event.get_button());
if (event.get_button() == 3 && mode == 4) { // right click: reset downloaded sum
resetNextCount = true;
if (event.get_button() == 3) { // right click: reset downloaded sum
if (mode < 4){
mode = 6;
updateSpeedMap();
settings.set_int('mode', mode);
} else {
resetNextCount = true;
}
parseStat();
}
else if (event.get_button() == 2) { // change font
Expand All @@ -71,37 +87,21 @@ function changeMode(widget, event) {
fontmode=0;
}
settings.set_int('fontmode', fontmode);
button.set_child(chooseLabel());
updateStyle();
parseStat();
}
else if (event.get_button() == 1) {
mode++;
if (mode > 4) {
if (mode > 6) {
mode = 0;
}
updateSpeedMap();
settings.set_int('mode', mode);
button.set_child(chooseLabel());
parseStat();
}
log('mode:' + mode + ' font:' + fontmode);
}

function chooseLabel() {
if (mode == 0 || mode == 1 || mode == 4) {
styleName = 'simplenetspeed-label';
}
else {
styleName = 'simplenetspeed-label-w';
}

if (fontmode > 0) {
styleName = styleName + '-' + fontmode;
}

ioSpeed.set_style_class_name(styleName);
return ioSpeed;
}

function parseStat() {
try {
let input_file = Gio.file_new_for_path('/proc/net/dev');
Expand Down Expand Up @@ -146,18 +146,25 @@ function parseStat() {
dot = "⇅";
}

if (mode >= 0 && mode <= 1) {
if (mode == 0 || mode == 1) {
ioSpeed.set_text(dot + speedToString(speed));
}
else if (mode >= 2 && mode <= 3) {
else if (mode == 2 || mode == 3) {
ioSpeed.set_text("↓" + speedToString(speed - speedUp) + " ↑" + speedToString(speedUp));
}
else if (mode == 4) {
else if (mode >= 4 && mode <= 6) {
if (resetNextCount == true) {
resetNextCount = false;
resetCount = count;
resetCountUp = countUp;
}
if(mode == 4){
ioSpeed.set_text("∑ ↓ " + speedToString(count - resetCount));
} else if (mode == 5) {
ioSpeed.set_text("∑ ↑ " + speedToString(countUp - resetCountUp));
} else {
ioSpeed.set_text("∑ ↑↓ " + speedToString(count - resetCount + countUp - resetCountUp));
}
ioSpeed.set_text("∑ " + speedToString(count - resetCount));
}

lastCount = count;
Expand All @@ -167,37 +174,11 @@ function parseStat() {
ioSpeed.set_text(e.message);
}

/*
let curDiskstats = GLib.file_get_contents('/proc/diskstats');

if (diskstats == curDiskstats) {
if (cur !== 0) {
button.set_child(iconDark);
cur = 0;
}
} else {
if (cur != 1) {
button.set_child(icon);
cur = 1;
}
diskstats = curDiskstats;
}*/

return true;
}

function speedToString(amount) {
let digits;
let speed_map;
if (mode == 0 || mode == 2) {
speed_map = ["bps", "Kbps", "Mbps", "Gbps"];
}
else if (mode == 1 || mode == 3) {
speed_map = ["B/s", "K/s", "M/s", "G/s"];
}
else if (mode == 4) {
speed_map = ["B", "KB", "MB", "GB"];
}

if (amount === 0)
return "0" + speed_map[0];
Expand Down
40 changes: 16 additions & 24 deletions metadata.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
{
"_generated": "Generated by SweetTooth, do not edit",
"description": "Simply showing network speed. Left click to change modes:\n\n1. Total net speed in bits per second\n2. Total net speed in Bytes per second\n3. Up & down speed in bits per second\n4. Up & down speed in Bytes per second\n5. Total of downloaded in Bytes (Right click to reset counter)\n\nMiddle click to change font size",
"name": "Simple net speed",
"_generated": "Generated by SweetTooth, do not edit",
"description": "Simply showing network speed. Left click to change modes:\n\n1. Total net speed in bits per second\n2. Total net speed in Bytes per second\n3. Up & down speed in bits per second\n4. Up & down speed in Bytes per second\n5. Total of downloaded in Bytes (Right click to reset all counters)\n6. Total of uploaded bytes (Right click to reset all counters)\n7. Total bytes transferred (Right click to reset all counters)\n\nMiddle click to change font size",
"name": "Simple net speed",
"shell-version": [
"3.14",
"3.15",
"3.16",
"3.17",
"3.18",
"3.19",
"3.20",
"3.21",
"3.22",
"3.23",
"3.24",
"3.25",
"3.26",
"3.27",
"3.28",
"3.29",
"3.14",
"3.16",
"3.18",
"3.20",
"3.22",
"3.24",
"3.26",
"3.28",
"3.30"
],
"url": "https://github.com/biji/simplenetspeed",
"uuid": "[email protected]",
"version": 5
}
],
"url": "https://github.com/mirko-laruina/simplenetspeed",
"uuid": "[email protected]",
"version": 16
}