summaryrefslogtreecommitdiff
path: root/src/bin/tyls.c
diff options
context:
space:
mode:
authorCarsten Haitzler <raster@rasterman.com>2013-01-30 15:26:16 +0000
committerCarsten Haitzler <raster@rasterman.com>2013-01-30 15:26:16 +0000
commit53eeed9880771fba623d910933b03408a8c343c8 (patch)
treee1c2f5c54630a3333e3695f3c95a489635af907a /src/bin/tyls.c
parent4505778e117f2560ddcb2046e28232425fb17cbb (diff)
a very very very very rough/first cut and playing with tyls...
SVN revision: 83485
Diffstat (limited to 'src/bin/tyls.c')
-rw-r--r--src/bin/tyls.c383
1 files changed, 383 insertions, 0 deletions
diff --git a/src/bin/tyls.c b/src/bin/tyls.c
new file mode 100644
index 0000000..f1e7560
--- /dev/null
+++ b/src/bin/tyls.c
@@ -0,0 +1,383 @@
1#include <Eina.h>
2#include <Ecore.h>
3#include <Evas.h>
4#include <Ecore_Evas.h>
5#include <Ecore_File.h>
6#include <Edje.h>
7#include <Emotion.h>
8#include <termios.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include <string.h>
13
14enum {
15 SMALL,
16 MEDIUM,
17 LARGE
18};
19
20Ecore_Evas *ee = NULL;
21Evas *evas = NULL;
22Evas_Object *o = NULL;
23struct termios told, tnew;
24int tw = 0, th = 0, cw = 0, ch = 0;
25
26#include "extns.h"
27
28static int
29echo_off(void)
30{
31 if (tcgetattr(0, &told) != 0) return -1;
32 tnew = told;
33 tnew.c_lflag &= ~ECHO;
34 if (tcsetattr(0, TCSAFLUSH, &tnew) != 0) return -1;
35 return 0;
36}
37
38static int
39echo_on(void)
40{
41 return tcsetattr(0, TCSAFLUSH, &told);
42}
43
44static void
45scaleterm(int w, int h, int *iw, int *ih)
46{
47 if (w > (tw * cw))
48 {
49 *iw = tw;
50 *ih = ((h * (tw * cw) / w) + (ch - 1)) / ch;
51 }
52 else
53 {
54 *iw = (w + (cw - 1)) / cw;
55 *ih = (h + (ch - 1)) / ch;
56 }
57}
58
59static const char *
60is_fmt(const char *f, const char **extn)
61{
62 int i, len, l;
63
64 len = strlen(f);
65 for (i = 0; extn[i]; i++)
66 {
67 l = strlen(extn[i]);
68 if (len < l) continue;
69 if (!strcasecmp(extn[i], f + len - l)) return extn[i];
70 }
71 return NULL;
72}
73
74static void
75prnt(const char *path, int w, int h, int mode)
76{
77 int x, y, i;
78 char *line, buf[4096];
79
80 if ((w >= 512) || (h >= 512)) return;
81 line = malloc(w + 100);
82 if (!line) return;
83 snprintf(buf, sizeof(buf), "%c}ic#%i;%i;%s", 0x1b, w, h, path);
84 if (write(0, buf, strlen(buf) + 1) < 0) perror("write");
85 i = 0;
86 line[i++] = 0x1b;
87 line[i++] = '}';
88 line[i++] = 'i';
89 line[i++] = 'b';
90 line[i++] = 0;
91 for (x = 0; x < w; x++) line[i++] = '#';
92 line[i++] = 0x1b;
93 line[i++] = '}';
94 line[i++] = 'i';
95 line[i++] = 'e';
96 line[i++] = 0;
97 line[i++] = '\n';
98 for (y = 0; y < h; y++)
99 {
100 if (write(0, line, i) < 0) perror("write");
101 }
102 free(line);
103}
104
105static void
106list_dir(const char *dir, int mode)
107{
108 Eina_List *files, *l;
109 char *s, **names;
110 int maxlen = 0, cols, c, i, j, num, cw, stuff;
111
112 files = ecore_file_ls(dir);
113 if (!files) return;
114 names = calloc(eina_list_count(files), sizeof(char *));
115 if (!names) return;
116 i = 0;
117 EINA_LIST_FOREACH(files, l, s)
118 {
119 int len = eina_unicode_utf8_get_len(s);
120
121 if (s[0] == '.') continue;
122 if (len > maxlen) maxlen = len;
123 names[i] = s;
124 i++;
125 }
126 num = i;
127 stuff = 0;
128 if (mode == SMALL) stuff += 2;
129 stuff += 1; // spacer at start
130 stuff += 5; // xxxx[ /K/M/G/T/P...]
131 stuff += 1; // type [@/*/|/=...]
132 stuff += 1; // spacer
133 maxlen += stuff;
134 if (maxlen > 0)
135 {
136 cols = tw / maxlen;
137 if (cols < 1) cols = 1;
138 for (i = 0; i < ((num + (cols - 1)) / cols); i++)
139 {
140 for (c = 0; c < cols; c++)
141 {
142 char buf[4096], sz[6];
143 long long size;
144
145 s = names[((c * (num + (cols - 1))) / cols) + i];
146 if (!s) continue;
147 snprintf(buf, sizeof(buf), "%s/%s", dir, s);
148 int len = eina_unicode_utf8_get_len(s);
149 cw = tw / cols;
150 size = ecore_file_size(buf);
151 if (size < 1024)
152 snprintf(sz, sizeof(sz), " %4lld", size);
153 else if (size < (1024 * 1024))
154 snprintf(sz, sizeof(sz), "%4lldK", size / (1024));
155 else if (size < (1024 * 1024 * 1024))
156 snprintf(sz, sizeof(sz), "%4lldM", size / (1024 * 1024));
157 else if (size < (1024 * 1024 * 1024 * 1024))
158 snprintf(sz, sizeof(sz), "%4lldG", size / (1024 * 1024 * 1024));
159 else if (size < (1024 * 1024 * 1024 * 1024 * 1024))
160 snprintf(sz, sizeof(sz), "%4lldT", size / (1024 * 1024 * 1024 * 1024));
161 else if (size < (1024 * 1024 * 1024 * 1024 * 1024 * 1024))
162 snprintf(sz, sizeof(sz), "%4lldP", size / (1024 * 1024 * 1024 * 1024 * 1024));
163 len += stuff;
164 printf("%c}ic#%i;%i;%s%c", 0x1b, 2, 1, buf, 0);
165 printf("%c}ib%c", 0x1b, 0);
166 printf("##");
167 printf("%c}ie%c", 0x1b, 0);
168 printf(" %s %s", sz, s);
169 if (ecore_file_is_dir(buf)) printf("/");
170 else
171 {
172 char *ts;
173
174 ts = ecore_file_readlink(buf);
175 if (ts)
176 {
177 printf("@");
178 free(ts);
179 }
180 else
181 {
182 printf(" ");
183 }
184 }
185 for (j = 0; j < (cw - len); j++) printf(" ");
186 }
187 printf("\n");
188 }
189 }
190 free(names);
191 EINA_LIST_FREE(files, s) free(s);
192}
193/*
194 if ((is_fmt(rp, extn_img)) ||
195 (is_fmt(rp, extn_scale)) ||
196 (is_fmt(rp, extn_mov)))
197 {
198 o = evas_object_image_add(evas);
199 evas_object_image_file_set(o, rp, NULL);
200 evas_object_image_size_get(o, &w, &h);
201 if ((w >= 0) && (h > 0))
202 {
203 scaleterm(w, h, &iw, &ih);
204 prnt(rp, iw, ih, mode);
205 goto done;
206 }
207 evas_object_del(o);
208 o = NULL;
209 }
210
211 if (is_fmt(rp, extn_edj))
212 {
213 Eina_Bool ok = EINA_TRUE;
214
215 evas_object_del(o);
216 o = edje_object_add(evas);
217 if (!edje_object_file_set
218 (o, rp, "terminology/backgroud"))
219 {
220 if (!edje_object_file_set
221 (o, rp, "e/desktop/background"))
222 ok = EINA_FALSE;
223 }
224 if (ok)
225 {
226 Evas_Coord mw = 0, mh = 0;
227
228 edje_object_size_min_get(o, &mw, &mh);
229 if ((mw <= 0) || (mh <= 0))
230 edje_object_size_min_calc(o, &mw, &mh);
231 if ((mw <= 0) || (mh <= 0))
232 {
233 mw = (tw) * cw;
234 mh = (th - 1) * ch;
235 }
236 scaleterm(mw, mh, &iw, &ih);
237 prnt(rp, iw, ih, mode);
238 goto done;
239 }
240 evas_object_del(o);
241 o = NULL;
242 }
243
244 if ((is_fmt(rp, extn_aud)) ||
245 (is_fmt(rp, extn_mov)))
246 {
247 Eina_Bool ok = EINA_TRUE;
248
249 o = emotion_object_add(evas);
250 ok = emotion_object_init(o, NULL);
251 if (ok)
252 {
253 if (emotion_object_file_set(o, rp))
254 {
255 emotion_object_audio_mute_set(o, EINA_TRUE);
256 if (emotion_object_video_handled_get(o))
257 {
258 emotion_object_size_get(o, &w, &h);
259 if ((w >= 0) && (h > 0))
260 {
261 scaleterm(w, h, &iw, &ih);
262 prnt(rp, iw, ih, mode);
263 }
264 }
265 else
266 prnt(rp, tw, 3, NOIMG);
267 goto done;
268 }
269 }
270 evas_object_del(o);
271 o = NULL;
272 }
273done:
274 if (o) evas_object_del(o);
275 o = NULL;
276 free(rp);
277 }
278 evas_norender(evas);
279*/
280
281int
282main(int argc, char **argv)
283{
284 char buf[64];
285 int w = 0, h = 0;
286 int iw = 0, ih = 0;
287 Eina_Bool listed = EINA_FALSE;
288
289 if (!getenv("TERMINOLOGY")) return 0;
290 if ((argc == 2) && (!strcmp(argv[1], "-h")))
291 {
292 printf("Usage: %s [-s|-m|-l] FILE1 [FILE2 ...]\n"
293 "\n"
294 " -s Small list mode\n"
295 " -m Medium list mode\n"
296 " -l Large list mode\n",
297 argv[0]);
298 return 0;
299 }
300 eina_init();
301 ecore_init();
302 ecore_file_init();
303 evas_init();
304 ecore_evas_init();
305 edje_init();
306 emotion_init();
307 ee = ecore_evas_buffer_new(1, 1);
308 if (ee)
309 {
310 int i, mode = SMALL;
311 char *rp;
312
313 evas = ecore_evas_get(ee);
314 echo_off();
315 snprintf(buf, sizeof(buf), "%c}qs", 0x1b);
316 if (write(0, buf, strlen(buf) + 1) < 0) perror("write");
317 if (scanf("%i;%i;%i;%i", &tw, &th, &cw, &ch) != 4)
318 {
319 echo_on();
320 return 0;
321 }
322 if ((tw <= 0) || (th <= 0) || (cw <= 1) || (ch <= 1))
323 {
324 echo_on();
325 return 0;
326 }
327 echo_on();
328 for (i = 1; i < argc; i++)
329 {
330 char *path;
331
332 if (!strcmp(argv[i], "-c"))
333 {
334 mode = SMALL;
335 i++;
336 if (i >= argc) break;
337 }
338 else if (!strcmp(argv[i], "-m"))
339 {
340 mode = MEDIUM;
341 i++;
342 if (i >= argc) break;
343 }
344 else if (!strcmp(argv[i], "-l"))
345 {
346 mode = LARGE;
347 i++;
348 if (i >= argc) break;;
349 }
350 path = argv[i];
351 rp = ecore_file_realpath(path);
352 if (rp)
353 {
354 if (ecore_file_is_dir(rp))
355 {
356 list_dir(rp, mode);
357 listed = EINA_TRUE;
358 }
359 free(rp);
360 }
361 }
362 if (!listed)
363 {
364 rp = ecore_file_realpath("./");
365 if (rp)
366 {
367 list_dir(rp, mode);
368 free(rp);
369 }
370 }
371 exit(0);
372// ecore_main_loop_begin();
373 ecore_evas_free(ee);
374 }
375 emotion_shutdown();
376 edje_shutdown();
377 ecore_evas_shutdown();
378 evas_shutdown();
379 ecore_file_shutdown();
380 ecore_shutdown();
381 eina_shutdown();
382 return 0;
383}