2010-06-19 18:40:07 +00:00
|
|
|
/**
|
2011-09-14 05:10:51 +00:00
|
|
|
* Copyright © Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* This file is licensed under the GPLv2.
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
*/
|
2010-06-19 18:40:07 +00:00
|
|
|
|
|
|
|
Transmission.fmt = (function()
|
|
|
|
{
|
2010-07-10 02:05:25 +00:00
|
|
|
var speed_K = 1000;
|
2012-02-03 21:15:07 +00:00
|
|
|
var speed_B_str = 'B/s';
|
|
|
|
var speed_K_str = 'kB/s';
|
2010-07-03 00:25:22 +00:00
|
|
|
var speed_M_str = 'MB/s';
|
|
|
|
var speed_G_str = 'GB/s';
|
2010-07-10 02:05:25 +00:00
|
|
|
var speed_T_str = 'TB/s';
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2012-02-03 21:21:52 +00:00
|
|
|
var size_K = 1000;
|
2012-02-03 21:15:07 +00:00
|
|
|
var size_B_str = 'B';
|
2012-02-03 21:21:52 +00:00
|
|
|
var size_K_str = 'kB';
|
2010-09-18 23:06:03 +00:00
|
|
|
var size_M_str = 'MB';
|
|
|
|
var size_G_str = 'GB';
|
|
|
|
var size_T_str = 'TB';
|
2010-07-10 02:05:25 +00:00
|
|
|
|
|
|
|
var mem_K = 1024;
|
2012-02-03 21:21:52 +00:00
|
|
|
var mem_B_str = 'B';
|
|
|
|
var mem_K_str = 'KiB';
|
|
|
|
var mem_M_str = 'MiB';
|
|
|
|
var mem_G_str = 'GiB';
|
|
|
|
var mem_T_str = 'TiB';
|
2010-06-19 18:40:07 +00:00
|
|
|
|
|
|
|
return {
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
updateUnits: function(u)
|
2010-09-18 23:06:03 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
/*
|
2010-09-18 23:06:03 +00:00
|
|
|
speed_K = u['speed-bytes'];
|
|
|
|
speed_K_str = u['speed-units'][0];
|
|
|
|
speed_M_str = u['speed-units'][1];
|
|
|
|
speed_G_str = u['speed-units'][2];
|
|
|
|
speed_T_str = u['speed-units'][3];
|
|
|
|
|
|
|
|
size_K = u['size-bytes'];
|
|
|
|
size_K_str = u['size-units'][0];
|
|
|
|
size_M_str = u['size-units'][1];
|
|
|
|
size_G_str = u['size-units'][2];
|
|
|
|
size_T_str = u['size-units'][3];
|
|
|
|
|
|
|
|
mem_K = u['memory-bytes'];
|
|
|
|
mem_K_str = u['memory-units'][0];
|
|
|
|
mem_M_str = u['memory-units'][1];
|
|
|
|
mem_G_str = u['memory-units'][2];
|
|
|
|
mem_T_str = u['memory-units'][3];
|
2011-08-24 02:04:35 +00:00
|
|
|
*/
|
2010-09-18 23:06:03 +00:00
|
|
|
},
|
2010-06-19 18:40:07 +00:00
|
|
|
|
2010-06-22 22:30:58 +00:00
|
|
|
/*
|
|
|
|
* Format a percentage to a string
|
|
|
|
*/
|
2011-08-24 02:04:35 +00:00
|
|
|
percentString: function(x) {
|
|
|
|
if (x < 10.0)
|
|
|
|
return x.toTruncFixed(2);
|
|
|
|
else if (x < 100.0)
|
|
|
|
return x.toTruncFixed(1);
|
2010-06-22 22:30:58 +00:00
|
|
|
else
|
2011-08-24 02:04:35 +00:00
|
|
|
return x.toTruncFixed(0);
|
2010-06-22 22:30:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Format a ratio to a string
|
|
|
|
*/
|
2011-08-24 02:04:35 +00:00
|
|
|
ratioString: function(x) {
|
2011-09-05 07:46:55 +00:00
|
|
|
if (x === -1)
|
2010-06-22 22:30:58 +00:00
|
|
|
return "None";
|
2011-09-05 07:46:55 +00:00
|
|
|
if (x === -2)
|
2010-06-22 22:30:58 +00:00
|
|
|
return '∞';
|
2011-09-05 07:46:55 +00:00
|
|
|
return this.percentString(x);
|
2010-06-22 22:30:58 +00:00
|
|
|
},
|
|
|
|
|
2010-06-19 18:40:07 +00:00
|
|
|
/**
|
2010-07-10 02:05:25 +00:00
|
|
|
* Formats the a memory size into a human-readable string
|
2010-06-19 18:40:07 +00:00
|
|
|
* @param {Number} bytes the filesize in bytes
|
2010-07-10 02:05:25 +00:00
|
|
|
* @return {String} human-readable string
|
2010-06-19 18:40:07 +00:00
|
|
|
*/
|
2011-08-24 02:04:35 +00:00
|
|
|
mem: function(bytes)
|
2010-06-19 18:40:07 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
if (bytes < mem_K)
|
2010-09-12 02:07:43 +00:00
|
|
|
return [ bytes, mem_B_str ].join(' ');
|
2010-07-10 02:05:25 +00:00
|
|
|
|
|
|
|
var convertedSize;
|
|
|
|
var unit;
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
if (bytes < Math.pow(mem_K, 2))
|
2010-07-10 02:05:25 +00:00
|
|
|
{
|
|
|
|
convertedSize = bytes / mem_K;
|
|
|
|
unit = mem_K_str;
|
|
|
|
}
|
2011-08-24 02:04:35 +00:00
|
|
|
else if (bytes < Math.pow(mem_K, 3))
|
2010-07-10 02:05:25 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
convertedSize = bytes / Math.pow(mem_K, 2);
|
2010-07-10 02:05:25 +00:00
|
|
|
unit = mem_M_str;
|
|
|
|
}
|
2011-08-24 02:04:35 +00:00
|
|
|
else if (bytes < Math.pow(mem_K, 4))
|
2010-07-10 02:05:25 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
convertedSize = bytes / Math.pow(mem_K, 3);
|
2010-07-10 02:05:25 +00:00
|
|
|
unit = mem_G_str;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
convertedSize = bytes / Math.pow(mem_K, 4);
|
2010-07-10 02:05:25 +00:00
|
|
|
unit = mem_T_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to have at least 3 digits and at least 1 decimal
|
2010-09-12 02:07:43 +00:00
|
|
|
return convertedSize <= 9.995 ? [ convertedSize.toTruncFixed(2), unit ].join(' ')
|
|
|
|
: [ convertedSize.toTruncFixed(1), unit ].join(' ');
|
2010-07-10 02:05:25 +00:00
|
|
|
},
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2010-07-10 02:05:25 +00:00
|
|
|
/**
|
|
|
|
* Formats the a disk capacity or file size into a human-readable string
|
|
|
|
* @param {Number} bytes the filesize in bytes
|
|
|
|
* @return {String} human-readable string
|
|
|
|
*/
|
2011-08-24 02:04:35 +00:00
|
|
|
size: function(bytes)
|
2010-07-10 02:05:25 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
if (bytes < size_K)
|
2010-09-12 02:07:43 +00:00
|
|
|
return [ bytes, size_B_str ].join(' ');
|
2010-07-10 02:05:25 +00:00
|
|
|
|
|
|
|
var convertedSize;
|
|
|
|
var unit;
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
if (bytes < Math.pow(size_K, 2))
|
2010-07-10 02:05:25 +00:00
|
|
|
{
|
|
|
|
convertedSize = bytes / size_K;
|
|
|
|
unit = size_K_str;
|
|
|
|
}
|
2011-08-24 02:04:35 +00:00
|
|
|
else if (bytes < Math.pow(size_K, 3))
|
2010-07-10 02:05:25 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
convertedSize = bytes / Math.pow(size_K, 2);
|
2010-07-10 02:05:25 +00:00
|
|
|
unit = size_M_str;
|
|
|
|
}
|
2011-08-24 02:04:35 +00:00
|
|
|
else if (bytes < Math.pow(size_K, 4))
|
2010-07-10 02:05:25 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
convertedSize = bytes / Math.pow(size_K, 3);
|
2010-07-10 02:05:25 +00:00
|
|
|
unit = size_G_str;
|
|
|
|
}
|
2010-06-19 18:40:07 +00:00
|
|
|
else
|
2010-07-10 02:05:25 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
convertedSize = bytes / Math.pow(size_K, 4);
|
2010-07-10 02:05:25 +00:00
|
|
|
unit = size_T_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to have at least 3 digits and at least 1 decimal
|
2010-09-12 02:07:43 +00:00
|
|
|
return convertedSize <= 9.995 ? [ convertedSize.toTruncFixed(2), unit ].join(' ')
|
|
|
|
: [ convertedSize.toTruncFixed(1), unit ].join(' ');
|
2010-06-19 18:40:07 +00:00
|
|
|
},
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
speedBps: function(Bps)
|
2010-09-12 01:13:38 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
return this.speed(this.toKBps(Bps));
|
2010-09-12 01:13:38 +00:00
|
|
|
},
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
toKBps: function(Bps)
|
2010-09-12 01:13:38 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
return Math.floor(Bps / speed_K);
|
2010-09-12 01:13:38 +00:00
|
|
|
},
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
speed: function(KBps)
|
2010-06-19 18:40:07 +00:00
|
|
|
{
|
2010-07-10 02:05:25 +00:00
|
|
|
var speed = KBps;
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
if (speed <= 999.95) // 0 KBps to 999 K
|
|
|
|
return [ speed.toTruncFixed(0), speed_K_str ].join(' ');
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2010-07-10 02:05:25 +00:00
|
|
|
speed /= speed_K;
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2010-07-10 02:05:25 +00:00
|
|
|
if (speed <= 99.995) // 1 M to 99.99 M
|
2010-09-12 02:07:43 +00:00
|
|
|
return [ speed.toTruncFixed(2), speed_M_str ].join(' ');
|
2010-07-10 02:05:25 +00:00
|
|
|
if (speed <= 999.95) // 100 M to 999.9 M
|
2010-09-12 02:07:43 +00:00
|
|
|
return [ speed.toTruncFixed(1), speed_M_str ].join(' ');
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2010-07-10 02:05:25 +00:00
|
|
|
// insane speeds
|
|
|
|
speed /= speed_K;
|
2010-09-12 02:07:43 +00:00
|
|
|
return [ speed.toTruncFixed(2), speed_G_str ].join(' ');
|
2010-06-19 18:40:07 +00:00
|
|
|
},
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
timeInterval: function(seconds)
|
2010-06-19 18:40:07 +00:00
|
|
|
{
|
2011-09-14 16:26:13 +00:00
|
|
|
var days = Math.floor (seconds / 86400),
|
|
|
|
hours = Math.floor ((seconds % 86400) / 3600),
|
|
|
|
minutes = Math.floor ((seconds % 3600) / 60),
|
|
|
|
seconds = Math.floor (seconds % 60),
|
|
|
|
d = days + ' ' + (days > 1 ? 'days' : 'day'),
|
|
|
|
h = hours + ' ' + (hours > 1 ? 'hours' : 'hour'),
|
|
|
|
m = minutes + ' ' + (minutes > 1 ? 'minutes' : 'minute'),
|
|
|
|
s = seconds + ' ' + (seconds > 1 ? 'seconds' : 'second');
|
|
|
|
|
|
|
|
if (days) {
|
|
|
|
if (days >= 4 || !hours)
|
|
|
|
return d;
|
|
|
|
return d + ', ' + h;
|
|
|
|
}
|
|
|
|
if (hours) {
|
|
|
|
if (hours >= 4 || !minutes)
|
|
|
|
return h;
|
|
|
|
return h + ', ' + m;
|
|
|
|
}
|
|
|
|
if (minutes) {
|
|
|
|
if (minutes >= 4 || !seconds)
|
|
|
|
return m;
|
|
|
|
return m + ', ' + s;
|
|
|
|
}
|
|
|
|
return s;
|
2010-06-19 18:40:07 +00:00
|
|
|
},
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
timestamp: function(seconds)
|
2010-06-19 18:40:07 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
if (!seconds)
|
2010-11-08 19:16:03 +00:00
|
|
|
return 'N/A';
|
|
|
|
|
2010-06-19 18:40:07 +00:00
|
|
|
var myDate = new Date(seconds*1000);
|
|
|
|
var now = new Date();
|
|
|
|
|
|
|
|
var date = "";
|
|
|
|
var time = "";
|
|
|
|
|
2011-09-05 07:46:55 +00:00
|
|
|
var sameYear = now.getFullYear() === myDate.getFullYear();
|
|
|
|
var sameMonth = now.getMonth() === myDate.getMonth();
|
2010-06-19 18:40:07 +00:00
|
|
|
|
|
|
|
var dateDiff = now.getDate() - myDate.getDate();
|
2011-08-24 02:04:35 +00:00
|
|
|
if (sameYear && sameMonth && Math.abs(dateDiff) <= 1){
|
2011-09-05 07:46:55 +00:00
|
|
|
if (dateDiff === 0){
|
2010-06-19 18:40:07 +00:00
|
|
|
date = "Today";
|
|
|
|
}
|
2011-09-05 07:46:55 +00:00
|
|
|
else if (dateDiff === 1){
|
2010-06-19 18:40:07 +00:00
|
|
|
date = "Yesterday";
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
date = "Tomorrow";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
date = myDate.toDateString();
|
|
|
|
}
|
|
|
|
|
|
|
|
var hours = myDate.getHours();
|
|
|
|
var period = "AM";
|
2011-08-24 02:04:35 +00:00
|
|
|
if (hours > 12){
|
2010-06-19 18:40:07 +00:00
|
|
|
hours = hours - 12;
|
|
|
|
period = "PM";
|
|
|
|
}
|
2011-09-05 07:46:55 +00:00
|
|
|
if (hours === 0){
|
2010-06-19 18:40:07 +00:00
|
|
|
hours = 12;
|
|
|
|
}
|
2011-08-24 02:04:35 +00:00
|
|
|
if (hours < 10){
|
2010-06-21 13:14:33 +00:00
|
|
|
hours = "0" + hours;
|
|
|
|
}
|
|
|
|
var minutes = myDate.getMinutes();
|
2011-08-24 02:04:35 +00:00
|
|
|
if (minutes < 10){
|
2010-06-21 13:14:33 +00:00
|
|
|
minutes = "0" + minutes;
|
|
|
|
}
|
|
|
|
var seconds = myDate.getSeconds();
|
2011-08-24 02:04:35 +00:00
|
|
|
if (seconds < 10){
|
2010-06-21 13:14:33 +00:00
|
|
|
seconds = "0" + seconds;
|
|
|
|
}
|
2010-06-19 18:40:07 +00:00
|
|
|
|
|
|
|
time = [hours, minutes, seconds].join(':');
|
|
|
|
|
|
|
|
return [date, time, period].join(' ');
|
2010-09-12 02:07:43 +00:00
|
|
|
},
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
plural: function(i, word)
|
2010-09-12 02:07:43 +00:00
|
|
|
{
|
2011-11-13 16:51:05 +00:00
|
|
|
return [ i.toStringWithCommas(), ' ', word, (i==1?'':'s') ].join('');
|
2011-08-24 18:42:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
peerStatus: function( flagStr )
|
|
|
|
{
|
|
|
|
var formattedFlags = [];
|
|
|
|
for (var i=0, flag; flag=flagStr[i]; ++i)
|
|
|
|
{
|
|
|
|
var explanation = null;
|
|
|
|
switch (flag)
|
|
|
|
{
|
|
|
|
case "O": explanation = "Optimistic unchoke"; break;
|
|
|
|
case "D": explanation = "Downloading from this peer"; break;
|
|
|
|
case "d": explanation = "We would download from this peer if they'd let us"; break;
|
|
|
|
case "U": explanation = "Uploading to peer"; break;
|
|
|
|
case "u": explanation = "We would upload to this peer if they'd ask"; break;
|
|
|
|
case "K": explanation = "Peer has unchoked us, but we're not interested"; break;
|
|
|
|
case "?": explanation = "We unchoked this peer, but they're not interested"; break;
|
|
|
|
case "E": explanation = "Encrypted Connection"; break;
|
|
|
|
case "H": explanation = "Peer was discovered through Distributed Hash Table (DHT)"; break;
|
|
|
|
case "X": explanation = "Peer was discovered through Peer Exchange (PEX)"; break;
|
|
|
|
case "I": explanation = "Peer is an incoming connection"; break;
|
|
|
|
case "T": explanation = "Peer is connected via uTP"; break;
|
|
|
|
}
|
|
|
|
|
2011-09-05 07:46:55 +00:00
|
|
|
if (!explanation) {
|
2011-08-24 18:42:10 +00:00
|
|
|
formattedFlags.push(flag);
|
|
|
|
} else {
|
|
|
|
formattedFlags.push("<span title=\"" + flag + ': ' + explanation + "\">" + flag + "</span>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return formattedFlags.join('');
|
2011-10-15 04:35:20 +00:00
|
|
|
}
|
2010-06-19 18:40:07 +00:00
|
|
|
}
|
|
|
|
})();
|