2014-12-12 21:31:19 +01:00
|
|
|
.pragma library
|
2015-01-20 00:57:23 +01:00
|
|
|
function clamp(x, min, max) {
|
|
|
|
if (x <= min)
|
|
|
|
return min;
|
|
|
|
if (x >= max)
|
|
|
|
return max;
|
|
|
|
return x;
|
|
|
|
}
|
2015-01-05 21:47:42 +01:00
|
|
|
function lint(a, b, t) {
|
|
|
|
return (1 - t) * a + (t) * b;
|
|
|
|
}
|
2014-12-12 21:31:19 +01:00
|
|
|
function mix(c1, c2, alpha){
|
|
|
|
return Qt.rgba(c1.r * alpha + c2.r * (1-alpha),
|
|
|
|
c1.g * alpha + c2.g * (1-alpha),
|
|
|
|
c1.b * alpha + c2.b * (1-alpha),
|
|
|
|
c1.a * alpha + c2.a * (1-alpha))
|
|
|
|
}
|
|
|
|
function strToColor(s){
|
|
|
|
var r = parseInt(s.substring(1,3), 16) / 256;
|
|
|
|
var g = parseInt(s.substring(3,5), 16) / 256;
|
|
|
|
var b = parseInt(s.substring(5,7), 16) / 256;
|
|
|
|
return Qt.rgba(r, g, b, 1.0);
|
|
|
|
}
|
2016-03-21 12:55:09 -07:00
|
|
|
|
|
|
|
/* Tokenizes a command into program and arguments, taking into account quoted
|
|
|
|
* strings and backslashes.
|
|
|
|
* Based on GLib's tokenizer, used by Gnome Terminal
|
|
|
|
*/
|
|
|
|
function tokenizeCommandLine(s){
|
|
|
|
var args = [];
|
|
|
|
var currentToken = "";
|
|
|
|
var quoteChar = "";
|
|
|
|
var escaped = false;
|
|
|
|
var nextToken = function() {
|
|
|
|
args.push(currentToken);
|
|
|
|
currentToken = "";
|
|
|
|
}
|
|
|
|
var appendToCurrentToken = function(c) {
|
|
|
|
currentToken += c;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < s.length; i++) {
|
|
|
|
|
|
|
|
// char followed by backslash, append literally
|
|
|
|
if (escaped) {
|
|
|
|
escaped = false;
|
|
|
|
appendToCurrentToken(s[i]);
|
|
|
|
|
|
|
|
// char inside quotes, either close or append
|
|
|
|
} else if (quoteChar) {
|
|
|
|
escaped = s[i] === '\\';
|
|
|
|
if (quoteChar === s[i]) {
|
|
|
|
quoteChar = "";
|
|
|
|
nextToken();
|
|
|
|
} else if (!escaped) {
|
|
|
|
appendToCurrentToken(s[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// regular char
|
|
|
|
} else {
|
|
|
|
escaped = s[i] === '\\';
|
|
|
|
switch (s[i]) {
|
|
|
|
case '\\':
|
|
|
|
// begin escape
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
// newlines always delimits
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
// delimit on new whitespace
|
|
|
|
if (currentToken) {
|
|
|
|
nextToken();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
case '"':
|
|
|
|
// begin quoted section
|
|
|
|
quoteChar = s[i];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
appendToCurrentToken(s[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore last token if broken quotes/backslash
|
|
|
|
if (currentToken && !escaped && !quoteChar) {
|
|
|
|
nextToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|