forked from ggreer/lscolors
/
lscolors.js
288 lines (238 loc) · 7.2 KB
/
lscolors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* LSCOLORS.js
* Created by Geoff Greer 7/31/2006
*
*/
// I hate global variables, but these are necessary
var currentColorIndex = 0;
var indexLength = 22;
var colors = new Array('e','x','f','x','c','x','d','x','b','x','e','g','e','d','a','b','a','g','a','c','a','d');
var divIDs = new Array("directory","system_link","socket","pipe","executable","block_special","char_special","exe_setuid","exe_setgid","dir_writeothers_sticky","dir_writeothers_nosticky");
//Update the "Bold" check boxes and keep track of which type of file we're changing
function updateColorIndex()
{
var isGroundBold;
currentColorIndex = document.getElementById("currentColorIndex").value * 2;
if(isBold(currentColorIndex))
{
document.getElementById("isForegroundBold").checked = true;
}
else
{
document.getElementById("isForegroundBold").checked = false;
}
if(isBold(currentColorIndex + 1))
{
document.getElementById("isBackgroundBold").checked = true;
}
else
{
document.getElementById("isBackgroundBold").checked = false;
}
}
//Change the color of a fore or background. This is called by the little color boxes in the html file.
function changeColor(color,isBackground)
{
if(isBold(currentColorIndex + isBackground))
{
colors[currentColorIndex + isBackground] = color.toUpperCase();
}
else
{
colors[currentColorIndex + isBackground] = color;
}
updateColorString();
}
//Make text bold (basically capitalize it)
function BoldText(isBackground)
{
colors[currentColorIndex + isBackground] = colors[currentColorIndex + isBackground].toUpperCase();
updateColorString();
}
//(tolower the letter)
function unBoldText(isBackground)
{
colors[currentColorIndex + isBackground] = colors[currentColorIndex + isBackground].toLowerCase();
updateColorString();
}
//Returns true if a letter is upper case
function isBold(index)
{
if(colors[index] == colors[index].toUpperCase())
{
return true;
}
else
{
return false;
}
}
//Updates the string in the input box
function updateColorString()
{
var colorString = "";
for(var i = 0; i < indexLength; i++)
{
colorString = colorString + colors[i];
}
document.getElementById("colorStringBSD").value=colorString;
var colorStringLinux = translateColorToLinux(colorString);
document.getElementById("colorStringLinux").value=colorStringLinux;
makePreview();
}
//Updates the preview ls. (basically sets a bunch of attributes on various spans)
function makePreview()
{
var color;
var backgroundColor;
var divColor;
var divBackground;
var divFontWeight;
var i = 0;
var colorString = document.getElementById("colorStringBSD").value;
var colorStringLinux = translateColorToLinux(colorString);
document.getElementById("colorStringLinux").value=colorStringLinux;
for (i = 0; i < indexLength; i++)
{
colors[i]=colorString.charAt(i);
}
for (i = 0; i < indexLength; i+=2)
{
divFontWeight = document.getElementById(divIDs[i/2]);
if(isBold(i))
{
divFontWeight.style.fontWeight = "bold";
}
else
{
divFontWeight.style.fontWeight = "normal";
}
color = translateColor(colors[i]);
divColor = document.getElementById(divIDs[i/2]);
divColor.style.color = color;
backgroundColor = translateColor(colors[i + 1]);
divBackground = document.getElementById(divIDs[i/2]);
divBackground.style.backgroundColor = backgroundColor;
}
}
function translateColor(color)
{
if(color == 'a')
{
return "black";
}
else if(color == 'b')
{
return "red";
}
else if(color == 'c')
{
return "green";
}
else if(color == 'd')
{
return "brown";
}
else if(color == 'e')
{
return "blue";
}
else if(color == 'f')
{
return "magenta";
}
else if(color == 'g')
{
return "cyan";
}
else if(color == 'h')
{
return "#cccccc";
}
}
function translateColorToLinux(colorString)
{
var color = "";
var linuxColorString = "";
for (var i = 0; i < colorString.length; i++)
{
color=colorString.charAt(i);
switch(i) {
case 0:
linuxColorString += "di="; //directory
break;
case 2:
linuxColorString += "ln="; //symlink
break;
case 4:
linuxColorString += "so="; //socket
break;
case 6:
linuxColorString += "pi="; //pipe
break;
case 8:
linuxColorString += "ex="; //executable
break;
case 10:
linuxColorString += "bd="; //block device
break;
case 12:
linuxColorString += "cd="; //character device
break;
case 14:
linuxColorString += "su="; //setuid
break;
case 16:
linuxColorString += "sg="; //setgid
break;
case 18:
linuxColorString += "tw="; //other writable sticky
break;
case 20:
linuxColorString += "ow="; //other writable non-sticky
break;
}
//Linux LSCOLORS has different codes for foreground/background colors :/
if (i % 2 === 0) {
//If bold, add bold thingy
if(color == color.toUpperCase())
linuxColorString += "1;";
if(color == 'a' || color == 'x')
linuxColorString += "0"; //default (black)
else if(color == 'b')
linuxColorString += "31"; //red
else if(color == 'c')
linuxColorString += "32"; //green
else if(color == 'd')
linuxColorString += "33"; //orange/brown
else if(color == 'e')
linuxColorString += "34"; //blue
else if(color == 'f')
linuxColorString += "35"; //magenta
else if(color == 'g')
linuxColorString += "36"; //cyan
else if(color == 'h')
linuxColorString += "37"; //grey
}
else {
linuxColorString += ";";
if(color == 'a' || color == 'x')
linuxColorString += "40"; //default (black) background
else if(color == 'b')
linuxColorString += "41"; //red background
else if(color == 'c')
linuxColorString += "42"; //green background
else if(color == 'd')
linuxColorString += "43"; //orange/brown background
else if(color == 'e')
linuxColorString += "44"; //blue background
else if(color == 'f')
linuxColorString += "45"; //magenta background
else if(color == 'g')
linuxColorString += "46"; //cyan background
else if(color == 'h')
linuxColorString += "47"; //grey background
linuxColorString += ":";
}
}
return linuxColorString;
}