diff options
author | Thibaut Broggi <broggi_t@epitech.eu> | 2015-02-26 23:10:10 +0100 |
---|---|---|
committer | Boris Faure <billiob@gmail.com> | 2015-02-26 23:47:22 +0100 |
commit | 03b88bafc755e93c75c17a10902b8b3e070eeb56 (patch) | |
tree | 2b12bb2e07a382f63cea40d28c9cc83468950a54 /src | |
parent | 27304c456f5d837dae4cddcc2a1dfeba38b2fc0b (diff) |
tyls: change options management and refactor
adding an option "-a" that display hidden files
changing the way options are managed: arguments can no be passed in any order
refactoring size_print function to support ZB and YB
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/tyls.c | 116 |
1 files changed, 49 insertions, 67 deletions
diff --git a/src/bin/tyls.c b/src/bin/tyls.c index 843cbfd..d145fd9 100644 --- a/src/bin/tyls.c +++ b/src/bin/tyls.c | |||
@@ -46,40 +46,18 @@ echo_on(void) | |||
46 | static void | 46 | static void |
47 | size_print(char *buf, int bufsz, char *sz, unsigned long long size) | 47 | size_print(char *buf, int bufsz, char *sz, unsigned long long size) |
48 | { | 48 | { |
49 | if (size < 1024LL) | 49 | char prefixes[] = " KMGTPEZY"; |
50 | { | 50 | int i = 0; |
51 | snprintf(buf, bufsz, "%4lld", size); | 51 | |
52 | *sz = ' '; | 52 | while (prefixes[i]) |
53 | } | ||
54 | else if (size < (1024LL * 1024LL)) | ||
55 | { | ||
56 | snprintf(buf, bufsz, "%4lld", size / (1024LL)); | ||
57 | *sz = 'K'; | ||
58 | } | ||
59 | else if (size < (1024LL * 1024LL * 1024LL)) | ||
60 | { | ||
61 | snprintf(buf, bufsz, "%4lld", size / (1024LL * 1024LL)); | ||
62 | *sz = 'M'; | ||
63 | } | ||
64 | else if (size < (1024LL * 1024LL * 1024LL * 1024LL)) | ||
65 | { | ||
66 | snprintf(buf, bufsz, "%4lld", size / (1024LL * 1024 * 1024LL)); | ||
67 | *sz = 'G'; | ||
68 | } | ||
69 | else if (size < (1024LL * 1024LL * 1024LL * 1024LL * 1024LL)) | ||
70 | { | ||
71 | snprintf(buf, bufsz, "%4lld", size / (1024LL * 1024LL * 1024LL * 1024LL)); | ||
72 | *sz = 'T'; | ||
73 | } | ||
74 | else if (size < (1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL)) | ||
75 | { | ||
76 | snprintf(buf, bufsz, "%4lld", size / (1024LL * 1024LL * 1024LL * 1024LL * 1024LL)); | ||
77 | *sz = 'P'; | ||
78 | } | ||
79 | else | ||
80 | { | 53 | { |
81 | snprintf(buf, bufsz, "%4lld", size / (1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL)); | 54 | if (size < (unsigned long long)pow(1024LL, i + 1) || !prefixes[i + 1]) |
82 | *sz = 'E'; | 55 | { |
56 | snprintf(buf, bufsz, "%4lld", size / (unsigned long long)pow(1024LL, i)); | ||
57 | *sz = prefixes[i]; | ||
58 | return; | ||
59 | } | ||
60 | ++i; | ||
83 | } | 61 | } |
84 | } | 62 | } |
85 | 63 | ||
@@ -167,6 +145,12 @@ typedef struct _Cmatch | |||
167 | const char *match, *icon; | 145 | const char *match, *icon; |
168 | } Cmatch; | 146 | } Cmatch; |
169 | 147 | ||
148 | typedef struct _Tyls_Options | ||
149 | { | ||
150 | int mode; | ||
151 | Eina_Bool hidden; | ||
152 | } Tyls_Options; | ||
153 | |||
170 | // for regular files | 154 | // for regular files |
171 | const Cmatch fmatch[] = | 155 | const Cmatch fmatch[] = |
172 | { | 156 | { |
@@ -604,7 +588,7 @@ fileprint(char *path, char *name, Eina_Bool type) | |||
604 | } | 588 | } |
605 | 589 | ||
606 | static void | 590 | static void |
607 | list_dir(const char *dir, int mode) | 591 | list_dir(const char *dir, Tyls_Options *options) |
608 | { | 592 | { |
609 | Eina_List *files, *l; | 593 | Eina_List *files, *l; |
610 | char *s, **names; | 594 | char *s, **names; |
@@ -619,15 +603,15 @@ list_dir(const char *dir, int mode) | |||
619 | { | 603 | { |
620 | int len = eina_unicode_utf8_get_len(s); | 604 | int len = eina_unicode_utf8_get_len(s); |
621 | 605 | ||
622 | if (s[0] == '.') continue; | 606 | if (s[0] == '.' && options->hidden == EINA_FALSE) continue; |
623 | if (len > maxlen) maxlen = len; | 607 | if (len > maxlen) maxlen = len; |
624 | names[i] = s; | 608 | names[i] = s; |
625 | i++; | 609 | i++; |
626 | } | 610 | } |
627 | num = i; | 611 | num = i; |
628 | stuff = 0; | 612 | stuff = 0; |
629 | if (mode == SMALL) stuff += 2; | 613 | if (options->mode == SMALL) stuff += 2; |
630 | else if (mode == MEDIUM) stuff += 4; | 614 | else if (options->mode == MEDIUM) stuff += 4; |
631 | stuff += 5; // xxxx[ /K/M/G/T/P...] | 615 | stuff += 5; // xxxx[ /K/M/G/T/P...] |
632 | stuff += 1; // spacer at start | 616 | stuff += 1; // spacer at start |
633 | // name | 617 | // name |
@@ -651,7 +635,7 @@ list_dir(const char *dir, int mode) | |||
651 | char buf[4096]; | 635 | char buf[4096]; |
652 | const char *icon; | 636 | const char *icon; |
653 | 637 | ||
654 | if (mode == SMALL) | 638 | if (options->mode == SMALL) |
655 | { | 639 | { |
656 | for (c = 0; c < cols; c++) | 640 | for (c = 0; c < cols; c++) |
657 | { | 641 | { |
@@ -681,7 +665,7 @@ list_dir(const char *dir, int mode) | |||
681 | } | 665 | } |
682 | printf("\n"); | 666 | printf("\n"); |
683 | } | 667 | } |
684 | else if (mode == MEDIUM) | 668 | else if (options->mode == MEDIUM) |
685 | { | 669 | { |
686 | for (c = 0; c < cols; c++) | 670 | for (c = 0; c < cols; c++) |
687 | { | 671 | { |
@@ -744,13 +728,15 @@ int | |||
744 | main(int argc, char **argv) | 728 | main(int argc, char **argv) |
745 | { | 729 | { |
746 | char buf[64]; | 730 | char buf[64]; |
747 | Eina_Bool listed = EINA_FALSE; | 731 | Eina_List *dirs = NULL, *l; |
732 | Tyls_Options options = {SMALL, EINA_FALSE}; | ||
748 | 733 | ||
749 | if (!getenv("TERMINOLOGY")) return 0; | 734 | if (!getenv("TERMINOLOGY")) return 0; |
750 | if ((argc == 2) && (!strcmp(argv[1], "-h"))) | 735 | if ((argc == 2) && (!strcmp(argv[1], "-h"))) |
751 | { | 736 | { |
752 | printf("Usage: %s [-s|-m] FILE1 [FILE2 ...]\n" | 737 | printf("Usage: %s [-a] [-s|-m] FILE1 [FILE2 ...]\n" |
753 | "\n" | 738 | "\n" |
739 | " -a Show hidden files\n" | ||
754 | " -s Small list mode\n" | 740 | " -s Small list mode\n" |
755 | " -m Medium list mode\n", | 741 | " -m Medium list mode\n", |
756 | /*" -l Large list mode\n", Enable again once we support it */ | 742 | /*" -l Large list mode\n", Enable again once we support it */ |
@@ -770,19 +756,15 @@ main(int argc, char **argv) | |||
770 | ee = ecore_evas_buffer_new(1, 1); | 756 | ee = ecore_evas_buffer_new(1, 1); |
771 | if (ee) | 757 | if (ee) |
772 | { | 758 | { |
773 | int i, cw, ch, mode = SMALL; | 759 | int i, cw, ch; |
774 | char *rp; | 760 | char *rp; |
775 | 761 | ||
776 | evas = ecore_evas_get(ee); | 762 | evas = ecore_evas_get(ee); |
777 | echo_off(); | 763 | echo_off(); |
778 | snprintf(buf, sizeof(buf), "%c}qs", 0x1b); | 764 | snprintf(buf, sizeof(buf), "%c}qs", 0x1b); |
779 | if (write(0, buf, strlen(buf) + 1) < 0) perror("write"); | 765 | if (write(0, buf, strlen(buf) + 1) < 0) perror("write"); |
780 | if (scanf("%i;%i;%i;%i", &tw, &th, &cw, &ch) != 4) | 766 | if (scanf("%i;%i;%i;%i", &tw, &th, &cw, &ch) != 4 |
781 | { | 767 | || (tw <= 0) || (th <= 0) || (cw <= 1) || (ch <= 1)) |
782 | echo_on(); | ||
783 | return 0; | ||
784 | } | ||
785 | if ((tw <= 0) || (th <= 0) || (cw <= 1) || (ch <= 1)) | ||
786 | { | 768 | { |
787 | echo_on(); | 769 | echo_on(); |
788 | return 0; | 770 | return 0; |
@@ -790,8 +772,7 @@ main(int argc, char **argv) | |||
790 | echo_on(); | 772 | echo_on(); |
791 | for (i = 1; i < argc; i++) | 773 | for (i = 1; i < argc; i++) |
792 | { | 774 | { |
793 | char *path; | 775 | char *cmp[] = {"-s", "-m", "-l"}; |
794 | char *cmp[] = {"-c", "-m", "-l"}; | ||
795 | int modes[] = {SMALL, MEDIUM, LARGE}; | 776 | int modes[] = {SMALL, MEDIUM, LARGE}; |
796 | unsigned int j; | 777 | unsigned int j; |
797 | 778 | ||
@@ -799,34 +780,35 @@ main(int argc, char **argv) | |||
799 | { | 780 | { |
800 | if (!strcmp(argv[i], cmp[j])) | 781 | if (!strcmp(argv[i], cmp[j])) |
801 | { | 782 | { |
802 | mode = modes[j]; | 783 | options.mode = modes[j]; |
803 | if (++i >= argc) break; | ||
804 | } | 784 | } |
805 | } | 785 | } |
806 | if (i >= argc) break; | 786 | if (!strcmp(argv[i], "-a")) |
807 | path = argv[i]; | ||
808 | rp = ecore_file_realpath(path); | ||
809 | if (rp) | ||
810 | { | 787 | { |
811 | if (ecore_file_is_dir(rp)) | 788 | options.hidden = EINA_TRUE; |
812 | { | 789 | } |
813 | list_dir(rp, mode); | 790 | if (argv[i][0] != '-') |
814 | listed = EINA_TRUE; | 791 | { |
815 | } | 792 | dirs = eina_list_append(dirs, argv[i]); |
816 | free(rp); | ||
817 | } | 793 | } |
818 | } | 794 | } |
819 | if (!listed) | 795 | if (!eina_list_count(dirs)) |
820 | { | 796 | { |
821 | rp = ecore_file_realpath("./"); | 797 | dirs = eina_list_append(dirs, "./"); |
822 | if (rp) | 798 | } |
799 | char *path; | ||
800 | EINA_LIST_FOREACH(dirs, l, path) | ||
801 | { | ||
802 | if ((rp = ecore_file_realpath(path)) | ||
803 | && ecore_file_is_dir(rp)) | ||
823 | { | 804 | { |
824 | list_dir(rp, mode); | 805 | list_dir(rp, &options); |
825 | free(rp); | 806 | free(rp); |
826 | } | 807 | } |
827 | } | 808 | } |
809 | eina_list_free(dirs); | ||
828 | fflush(stdout); | 810 | fflush(stdout); |
829 | exit(0); | 811 | return 0; |
830 | // ecore_main_loop_begin(); | 812 | // ecore_main_loop_begin(); |
831 | ecore_evas_free(ee); | 813 | ecore_evas_free(ee); |
832 | } | 814 | } |