cool-retro-term
122 строки · 3.5 Кб
1/*******************************************************************************
2* Copyright (c) 2013-2021 "Filippo Scognamiglio"
3* https://github.com/Swordfish90/cool-retro-term
4*
5* This file is part of cool-retro-term.
6*
7* cool-retro-term is free software: you can redistribute it and/or modify
8* it under the terms of the GNU General Public License as published by
9* the Free Software Foundation, either version 3 of the License, or
10* (at your option) any later version.
11*
12* This program is distributed in the hope that it will be useful,
13* but WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15* GNU General Public License for more details.
16*
17* You should have received a copy of the GNU General Public License
18* along with this program. If not, see <http://www.gnu.org/licenses/>.
19*******************************************************************************/
20
21.pragma library22function clamp(x, min, max) {23if (x <= min)24return min;25if (x >= max)26return max;27return x;28}
29
30function lint(a, b, t) {31return (1 - t) * a + (t) * b;32}
33
34function mix(c1, c2, alpha) {35return Qt.rgba(c1.r * alpha + c2.r * (1-alpha),36c1.g * alpha + c2.g * (1-alpha),37c1.b * alpha + c2.b * (1-alpha),38c1.a * alpha + c2.a * (1-alpha))39}
40
41function smoothstep(min, max, value) {42let x = Math.max(0, Math.min(1, (value - min) / (max - min)));43return x * x * (3 - 2 * x);44}
45
46function strToColor(s){47var r = parseInt(s.substring(1,3), 16) / 256;48var g = parseInt(s.substring(3,5), 16) / 256;49var b = parseInt(s.substring(5,7), 16) / 256;50return Qt.rgba(r, g, b, 1.0);51}
52
53/* Tokenizes a command into program and arguments, taking into account quoted
54* strings and backslashes.
55* Based on GLib's tokenizer, used by Gnome Terminal
56*/
57function tokenizeCommandLine(s){58var args = [];59var currentToken = "";60var quoteChar = "";61var escaped = false;62var nextToken = function() {63args.push(currentToken);64currentToken = "";65}66var appendToCurrentToken = function(c) {67currentToken += c;68}69
70for (var i = 0; i < s.length; i++) {71
72// char followed by backslash, append literally73if (escaped) {74escaped = false;75appendToCurrentToken(s[i]);76
77// char inside quotes, either close or append78} else if (quoteChar) {79escaped = s[i] === '\\';80if (quoteChar === s[i]) {81quoteChar = "";82nextToken();83} else if (!escaped) {84appendToCurrentToken(s[i]);85}86
87// regular char88} else {89escaped = s[i] === '\\';90switch (s[i]) {91case '\\':92// begin escape93break;94case '\n':95// newlines always delimits96nextToken();97break;98case ' ':99case '\t':100// delimit on new whitespace101if (currentToken) {102nextToken();103}104break;105case '\'':106case '"':107// begin quoted section108quoteChar = s[i];109break;110default:111appendToCurrentToken(s[i]);112}113}114}115
116// ignore last token if broken quotes/backslash117if (currentToken && !escaped && !quoteChar) {118nextToken();119}120
121return args;122}
123