Notice: Trying to access array offset on value of type null in /customers/1/2/4/mrwaggel.be/httpd.www/admin/kernel/db/db_posts.class.php on line 390 Notice: Trying to access array offset on value of type null in /customers/1/2/4/mrwaggel.be/httpd.www/admin/kernel/db/db_pages.class.php on line 337 Notice: Trying to access array offset on value of type null in /customers/1/2/4/mrwaggel.be/httpd.www/admin/kernel/db/db_comments.class.php on line 283 Deprecated: Function get_magic_quotes_runtime() is deprecated in /customers/1/2/4/mrwaggel.be/httpd.www/admin/boot/rules/4-remove_magic.bit on line 18 Deprecated: Function get_magic_quotes_gpc() is deprecated in /customers/1/2/4/mrwaggel.be/httpd.www/admin/boot/rules/4-remove_magic.bit on line 23 - exampleMr.Waggel's blog

Javascript human readable byte size function

31 March, 2020

Here is a small function which converts a bytes in to a readable format (B, KB, MB, GB, TB).

Function

const TB = 1000000000000;
const GB = 1000000000;
const MB = 1000000;
const KB = 1000;

function bytesReadable(byteSize, decimals) {
    let unit;
    let i;
    let remainder;

    // Get the whole number, and the remainder for the
    // decimals later on.
    if (byteSize > TB) {
        unit = "TB";
        i = Math.floor(byteSize / TB);
        remainder = byteSize - (i * TB);
    }else if (byteSize > GB) {
        unit = "GB";
        i = Math.floor(byteSize / GB);
        remainder = byteSize - (i * GB);
    }else if (byteSize > MB) {
        unit = "MB";
        i = Math.floor(byteSize / MB);
        remainder = byteSize - (i * MB);

    }else if (byteSize > KB){
        unit = "KB";
        i = Math.floor(byteSize / KB),
            remainder = byteSize - i * KB;
    }else{
        // Can't go smaller than bytes!
        return i.toString() + " " + unit;
    }

    if (decimals === 0){
        return i.toString() + " " + unit;
    }

    // Now we calculate the eventual missing leading
    // zeroes to pad up if needed.
    let width;
    if (remainder > GB){
        width = 12;
    }else if (remainder > MB){
        width = 9;
    }else if (remainder > KB){
        width = 6;
    }else{
        width = 3;
    }

    // Fill up leading zeroes if they are missing
    let remainderString = remainder.toString();

    for (let i = remainderString.length; i < width; i++){
        remainderString = "0" + remainderString;
    }

    // Check to prevent unwanted behaviour
    if (decimals > remainderString.length){
        decimals = remainderString.length;
    }

    return i.toString() + "." + remainderString.substr(0, decimals) + " " + unit;
}

Sample

console.log(bytesReadable(425005, 2));
console.log(bytesReadable(8741208, 2));
console.log(bytesReadable(114448910, 2));
console.log(bytesReadable(557891, 2));
console.log(bytesReadable(114710, 2));
console.log(bytesReadable(8933578, 2));
console.log(bytesReadable(5849684981, 2));
console.log(bytesReadable(12033687, 2));
console.log(bytesReadable(742289, 2));
console.log(bytesReadable(678007439, 2));

// Output
425.00 KB
  8.74 MB
114.44 MB
557.89 KB
114.71 KB
  8.93 MB
  5.84 GB
 12.03 MB
742.28 KB
678.00 MB

 

 

xrandr virtual splitscreen cookbook

17 March, 2020

As of xrandr version 1.5 it has become easy to divide your physical monitors into smaller virtual monitors. This is done with the following commands (without the '' quotation marks), scroll down for examples, explanations and even further down for more information.

xrandr --setmonitor 'vn' 'vw'/'pw'x'vh'/'ph'+'wo'+'ho' 'od'
This commands creates a virtual monitor

vn virtual monitor name
vw virtual monitor width in pixels
pw physical width on monitor in milimeters
vh virtual monitor height in pixels
ph physcical heigt on monitor in milimeters
wo width offset of the virutal monitor
ho height offset of the virtual monitor
od output device, the physical monitor on which the virtual monitor will be projected, specify none if your previous virtual entry is projected on the same monitor

xrandr --fb 'tw'x'th'
This command sets the total resolution of all the screens

tw The total width off all virtual monitors + their width offsets combined
th The total height off all virtual monitors + their height offsets combined

xrandr --delmonitor 'vn'
This command deletes a monitor

vn virtual monitor name

Identify your monitors(s)

Run the command xrandr in your terminal, you should see a list with all your output devices and for connected devices their supported modi. For my system it looks like this (shortened);

$ xrandr
Screen 0: minimum 320 x 200, current 1920 x 1080, maximum 16384 x 16384
DVI-D-1 disconnected (normal left inverted right x axis y axis)
VGA-2 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 476mm x 268mm
...
VGA-1-1 connected (normal left inverted right x axis y axis)
...
HDMI-1-1 disconnected (normal left inverted right x axis y axis)
DP-1-1 disconnected (normal left inverted right x axis y axis)
HDMI-1-2 connected (normal left inverted right x axis y axis)
...

We can see I have three monitors connected on my system, but only one is turned on and displaying, that is VGA-2.
As you may notice it looks like a virtual screen which xrandr created by default, lets look closer.

Overload CSS `cursor: wait` page wide with jquery

04 December, 2018

Problem:

undefined

You probably want to tell users on your page to wait for something important that is happening in the background. Most likely performing asynchronous page calls, calculations, DOM manipulation etc...

But just like any CSS property, the nested elements override the parent's property. This is troublesome if you have a lot of buttons and custom cursors in your style.

Metamod Source Game Event listening

05 October, 2016

A small explanation on how to listen to game events. This example is made for CS:GO, but will work on other Source based games too (some event names may be different). The example is meant for class based plugins. So if you are creating static functions you'll have to do things a little bit different.

PHP milliseconds timestamp as an integer

05 June, 2016

There is no native function for a unix timestamp in milliseconds, only 'time()' which returns seconds since epoch and 'microtime()', which returns microseconds since the current second. Although you can use 'microtime(true)', but it returns a float that can not guarantee precision. Or just 'microtime()' and explode the string.

Golang mgo append to a nested array in a document

24 May, 2016

Small tutorial on how to add/push/append a new element to an array/list inside a document, based on my previous post.

Golang mgo retrieve nested structured arrays

24 May, 2016

Small tutorial on how to retrieve nested arrays/lists from a 'mgo.Find()' result.

Golang transfer a file over a TCP socket

21 May, 2016

In this tutorial I'll demonstrate how to send a file in Go over a TCP connection using a server that sends the file and a client that receives it, I'll try to go into detail as much as possible.

You can download the source code here.

Comments and remarks are more than welcome.

Golang mgo create objectid from a hexadecimal string example

19 May, 2016

Storing a string that holds the hexadecimal string of a objectID is done by using the bson package with function 'objectIdHex()'.

Golang mgo findAndModify auto increment id tutorial

17 May, 2016

Here is a brief explanation for the 'findAndModify()' equivalent for mgo. Instead of 'findAndModify()' we will use 'mgo.Find().Apply()'. I will demonstrate how to use auto incremented ids instead of random unique generated IDs for documents, since mongdb does not have a native way of doing this (because MongoDB is not a relational database).

It work's like this, we create a collection, insert a document in that collection that contains the last generated ID. Every time we call 'findAndModify()', mongoDB will increment the value and we get that new value back, which you should use to insert a new document with.

Golang byte to int example

10 May, 2016

In Golang there isn't a native function to convert a character number (!= int number) to int.

An int in bytes is the normal sequence of 0-9 normally, but when you are dealing with number characters, they start of at number 48 (in the ASCII table)

So a character 0 is 48 in bytes, 1 is 49, 2 is 50, and so on...

Golang hash sum and checksum to string tutorial and examples

17 April, 2016

The output of the function "hash.Sum()" is generated in byte code, and the output of "Checksum()" is a unsigned integer.

Generate CRC32 hash of a file in Golang turorial

17 April, 2016

In previous posts I showed how to make a MD5 hash and a SHA1 hash of a file, generating a CRC checksum on a file is however a bit more complicated since it uses a polynomial table. I'll explain everything in depth.

Generate SHA1 hash of a file in Golang example

08 April, 2016

This function is based on my previous post but adapted for the SHA1 algorithm.

Home