csvFmt

Display csv file with formating
git clone https://noulin.net/git/csvFmt.git
Log | Files | Refs | README

csvFmt.c (2079B)


      1 #! /usr/bin/env sheepy
      2 /* or direct path to sheepy: #! /usr/local/bin/sheepy */
      3 
      4 /* Libsheepy documentation: https://spartatek.se/libsheepy/ */
      5 #include "libsheepyObject.h"
      6 #include "fort.h"
      7 
      8 /* enable/disable logging */
      9 /* #undef pLog */
     10 /* #define pLog(...) */
     11 
     12 int main(int ARGC, char** ARGV) {
     13 
     14   initLibsheepy(ARGV[0]);
     15   setLogMode(LOG_FUNC);
     16 
     17   if (ARGC < 2) {
     18     logE("Missing csv file argument");
     19     ret 1;
     20   }
     21 
     22 
     23   cleanListP(list) = NULL;
     24   void **csv = NULL;
     25 
     26   // load file.csv lines in list
     27   list = readText(ARGV[1]);
     28 
     29   if (!list) {
     30     logE("Error loading %s", ARGV[1]);
     31     ret 1;
     32   }
     33 
     34   int rows = 0;
     35   int cols = 0;
     36 
     37   // split lines and store in csv list
     38   forEachCharP(list, l) {
     39     char **spl = split(*l, ";");
     40     pErrorNULL(listPush(&csv, spl));
     41     cols = maxV(cols, lenG(spl));
     42     inc rows;
     43   }
     44 
     45 	char **t = malloc(rows * cols * sizeof(char*));
     46   int j = 0;
     47   forEachType(void, csv, L) {
     48     char **l = (char**) (*L);
     49     int i = 0;
     50     forEachS(l, c) {
     51       *(t + j * cols + i) = c;
     52       inc i;
     53     }
     54     if (i < cols) {
     55       while (i < cols) {
     56         *(t + j * cols + i) = "";
     57         inc i;
     58       }
     59     }
     60     inc j;
     61   }
     62 
     63   ft_table_t *table2 = ft_create_table();
     64 
     65 
     66   ft_set_border_style(table2, FT_SIMPLE_STYLE);
     67   //ft_set_border_style(table2, FT_DOUBLE_STYLE);
     68 
     69   ft_table_write(table2, rows, cols, (const char **) t);
     70 
     71   ft_set_cell_prop(table2, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
     72   range(i, cols) {
     73     ft_set_cell_prop(table2, 0, i, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_BOLD);
     74   }
     75   range(i, rows) {
     76     ft_set_cell_prop(table2, i, FT_ANY_COLUMN, FT_CPROP_CELL_BG_COLOR,  FT_COLOR_DEFAULT);
     77     ft_set_cell_prop(table2, i, FT_ANY_COLUMN, FT_CPROP_CELL_BG_RGBCOLOR,  i & 1 ? 0x2f2f2f : 0x1f1f1f);
     78   }
     79   ft_set_cell_prop(table2, 0, FT_ANY_COLUMN, FT_CPROP_CELL_BG_RGBCOLOR,  0x4867ff);
     80 
     81   // 0x4042f
     82   // 0x2f2f2f
     83   printf("%s\n", ft_to_string(table2));
     84   ft_destroy_table(table2);
     85 
     86 	free(t);
     87   // free csv list
     88   forEachType(void, csv, l) {
     89     listFreeS(*l);
     90   }
     91   free(csv);
     92   ret 0;
     93 }
     94 // vim: set expandtab ts=2 sw=2: