2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2009-10-04 21:30:18 +00:00
|
|
|
|
2012-03-29 19:29:48 +00:00
|
|
|
const Shell = imports.gi.Shell;
|
|
|
|
|
2009-10-04 21:30:18 +00:00
|
|
|
/*
|
|
|
|
* This function is intended to extend the String object and provide
|
|
|
|
* an String.format API for string formatting.
|
|
|
|
* It has to be set up using String.prototype.format = Format.format;
|
|
|
|
* Usage:
|
|
|
|
* "somestring %s %d".format('hello', 5);
|
2010-06-23 21:46:59 +00:00
|
|
|
* It supports %s, %d, %x and %f, for %f it also support precisions like
|
|
|
|
* "%.2f".format(1.526). All specifiers can be prefixed with a minimum
|
|
|
|
* field width, e.g. "%5s".format("foo"). Unless the width is prefixed
|
|
|
|
* with '0', the formatted string will be padded with spaces.
|
2009-10-04 21:30:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
function format() {
|
|
|
|
let str = this;
|
|
|
|
let i = 0;
|
|
|
|
let args = arguments;
|
|
|
|
|
2012-03-29 19:29:48 +00:00
|
|
|
return str.replace(/%(I+)?([0-9]+)?(?:\.([0-9]+))?(.)/g, function (str, flagsGroup, widthGroup, precisionGroup, genericGroup) {
|
2009-10-04 21:30:18 +00:00
|
|
|
|
|
|
|
if (precisionGroup != '' && genericGroup != 'f')
|
|
|
|
throw new Error("Precision can only be specified for 'f'");
|
|
|
|
|
2012-03-29 19:29:48 +00:00
|
|
|
let hasAlternativeIntFlag = (flagsGroup.indexOf('I') != -1);
|
|
|
|
|
|
|
|
if (hasAlternativeIntFlag && genericGroup != 'd')
|
|
|
|
throw new Error("Alternative output digits can only be specfied for 'd'");
|
|
|
|
|
2010-06-23 21:46:59 +00:00
|
|
|
let fillChar = (widthGroup[0] == '0') ? '0' : ' ';
|
|
|
|
let width = parseInt(widthGroup, 10) || 0;
|
|
|
|
|
|
|
|
function fillWidth(s, c, w) {
|
|
|
|
let fill = '';
|
|
|
|
for (let i = 0; i < w; i++)
|
|
|
|
fill += c;
|
|
|
|
return fill.substr(s.length) + s;
|
|
|
|
}
|
|
|
|
|
|
|
|
let s = '';
|
2009-10-04 21:30:18 +00:00
|
|
|
switch (genericGroup) {
|
|
|
|
case '%':
|
|
|
|
return '%';
|
|
|
|
break;
|
|
|
|
case 's':
|
2010-06-23 21:46:59 +00:00
|
|
|
s = args[i++].toString();
|
2009-10-04 21:30:18 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
2012-03-29 19:29:48 +00:00
|
|
|
let intV = parseInt(args[i++]);
|
|
|
|
if (hasAlternativeIntFlag)
|
|
|
|
s = Shell.format_int_alternative_output(intV);
|
|
|
|
else
|
|
|
|
s = intV.toString();
|
2010-06-23 21:46:59 +00:00
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
s = parseInt(args[i++]).toString(16);
|
2009-10-04 21:30:18 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (precisionGroup == '')
|
2010-06-23 21:46:59 +00:00
|
|
|
s = parseFloat(args[i++]).toString();
|
2009-10-04 21:30:18 +00:00
|
|
|
else
|
2010-06-23 21:46:59 +00:00
|
|
|
s = parseFloat(args[i++]).toFixed(parseInt(precisionGroup));
|
2009-10-04 21:30:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('Unsupported conversion character %' + genericGroup);
|
|
|
|
}
|
2010-06-23 21:46:59 +00:00
|
|
|
return fillWidth(s, fillChar, width);
|
2009-10-04 21:30:18 +00:00
|
|
|
});
|
|
|
|
}
|