[go: nahoru, domu]

Skip to content

Commit

Permalink
Fixed error by replace Gio's read by load_contents.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlynxZhou committed Oct 4, 2022
1 parent c0d8cf2 commit bb89d44
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,16 @@ const getCurrentNetSpeed = (refreshInterval) => {

try {
const inputFile = Gio.File.new_for_path("/proc/net/dev");
const fileInputStream = inputFile.read(null);
// See <https://gjs.guide/guides/gobject/basics.html#gobject-construction>.
// If we want new operator, we need to pass params in object.
// Short param is only used for static constructor.
const dataInputStream = new Gio.DataInputStream({
"base_stream": fileInputStream
});
const [, content] = inputFile.load_contents(null);
// See <https://github.com/GNOME/gjs/blob/master/doc/ByteArray.md#tostringauint8array-encodingstringstring>.
const lines = ByteArray.toString(content).split('\n');

// Caculate the sum of all interfaces' traffic line by line.
let totalDownBytes = 0;
let totalUpBytes = 0;
let line = null;
// See <https://gjs-docs.gnome.org/gio20~2.66p/gio.datainputstream#method-read_line>.
while ((line = dataInputStream.read_line(null)[0]) != null) {
// See <https://github.com/GNOME/gjs/blob/master/doc/ByteArray.md#tostringauint8array-encodingstringstring>.
const fields = ByteArray.toString(line).trim().split(/\W+/);

for (let i = 0; i < lines.length; ++i) {
const fields = lines[i].trim().split(/\W+/);
if (fields.length <= 2) {
continue;
}
Expand Down Expand Up @@ -86,8 +80,6 @@ const getCurrentNetSpeed = (refreshInterval) => {
totalUpBytes += currentInterfaceUpBytes;
}

fileInputStream.close(null);

if (lastTotalDownBytes === 0) {
lastTotalDownBytes = totalDownBytes;
}
Expand Down

0 comments on commit bb89d44

Please sign in to comment.