boxen

Create boxes in the terminal
git clone https://noulin.net/git/boxen.git
Log | Files | Refs | README | LICENSE

boxen.h (4383B)


      1 #pragma once
      2 
      3 /* Libsheepy documentation: http://spartatek.se/libsheepy/ */
      4 
      5 /*
      6 OPTIONS: (the values are case insensitive)
      7 
      8 "{borderColor:     'Yellow',"
      9  "borderStyle:     'double',"
     10  "dimBorder:       true,"
     11  "padding:         {top: 1, left: 2, right: 2, bottom: 2},"
     12  "margin:          {top: 1, left: 1, right: 1, bottom: 1},"
     13  "float:           'right',"
     14  "backgroundColor: 'green',"
     15  "align:           'center'}"
     16 
     17 
     18 borderColor
     19 
     20 Type: string
     21 Values: black red green yellow blue magenta cyan white gray, a hex value like #ff0000 or reset for default terminal color
     22 
     23 Color of the box border.
     24 
     25 
     26 borderStyle
     27 
     28 Type: string object
     29 Default: single
     30 single, double, /round,/ single-double, double-single, classic
     31 
     32 
     33 dimBorder
     34 
     35 Type: boolean
     36 Default: false
     37 
     38 Reduce opacity of the border.
     39 
     40 
     41 
     42 padding
     43 
     44 Type: number Object
     45 Default: 0
     46 
     47 Space between the text and box border.
     48 
     49 Accepts a number or an object with any of the top, right, bottom, left properties. When a number is specified, the left/right padding is 3 times the top/bottom to make it look nice.
     50 
     51 
     52 margin
     53 
     54 
     55 Type: number Object
     56 Default: 0
     57 
     58 Space around the box.
     59 
     60 Accepts a number or an object with any of the top, right, bottom, left properties. When a number is specified, the left/right margin is 3 times the top/bottom to make it look nice.
     61 
     62 
     63 float
     64 
     65 Type: string
     66 Values: right center left
     67 Default: left
     68 
     69 Float the box on the available terminal screen space.
     70 
     71 
     72 backgroundColor
     73 
     74 Type: string
     75 Values: black red green yellow blue magenta cyan white gray, a hex value like #ff0000 or reset for default terminal color
     76 
     77 Color of the background.
     78 
     79 
     80 align
     81 
     82 Type: string
     83 Default: left
     84 Values: left center right
     85 
     86 Align the text in the box based on the widest line.
     87 */
     88 
     89 
     90 
     91 #define boxO(obj, input, opts) (obj)->f->box(obj, input, opts)
     92 
     93 typ struct {
     94   char *topLeft;
     95   char *topRight;
     96   char *bottomLeft;
     97   char *bottomRight;
     98   char *horizontal;
     99   char *vertical;
    100 } borderStyleBoxent;
    101 
    102 /* Class boxen */
    103 typ struct boxen boxent;
    104 
    105 /* for object inheriting boxen, cast to boxen to be able to use this class functions and generics*/
    106 #define cBoxen(self) ( (boxent*) self )
    107 
    108 typ void            (*freeBoxenFt)      (boxent *self);
    109 typ void            (*terminateBoxenFt) (boxent **self);
    110 typ char*           (*toStringBoxenFt)  (boxent *self);
    111 typ boxent* (*duplicateBoxenFt) (boxent *self);
    112 typ void            (*smashBoxenFt)     (boxent **self);
    113 
    114 /**
    115  * free boxen
    116  */
    117 typ void            (*finishBoxenFt)    (boxent **self);
    118 
    119 typ const char*     (*helpBoxenFt)      (boxent *self);
    120 
    121 typ char *          (*boxBoxenFt)       (boxent *self, char *input, char *opts);
    122 
    123 
    124 /**
    125  * class functions
    126  * allocated once for all objects
    127  *
    128  * freed with finalizeBoxen
    129  */
    130 
    131 /**
    132  * use this define in child classes and add the new function after this class functions
    133  *
    134  * in this define, add the methods after <finishBoxenFt    finish;>
    135  *
    136  * Example:
    137  * #define RINGFUNCTIONST \n *   BOXENFUNCTIONST; \n *   setSizeRingFt           setSize
    138  */
    139 #define BOXENFUNCTIONST \
    140   helpBoxenFt      help;\
    141   boxBoxenFt       box
    142 
    143 typ struct {
    144   freeBoxenFt      free;
    145   terminateBoxenFt terminate;
    146   toStringBoxenFt  toString;
    147   duplicateBoxenFt duplicate;
    148   smashBoxenFt     smash;
    149   finishBoxenFt    finish;
    150   BOXENFUNCTIONST;
    151 } boxenFunctionst;
    152 
    153 /**
    154  * class
    155  */
    156 struct boxen {
    157   const char      *type;
    158   boxenFunctionst *f;
    159 
    160   char              *borderColor;
    161   u32               borderColorHex;
    162   borderStyleBoxent borderStyle;
    163   char              *dimBorder;
    164   struct {u32 top; u32 left; u32 right; u32 bottom;} padding;
    165   struct {u32 top; u32 left; u32 right; u32 bottom;} margin;
    166   char              *boxFloat;
    167   char              *backgroundColor;
    168   u32               backgroundColorHex;
    169   char              *align;
    170 };
    171 
    172 /* boxen */
    173 
    174 #define createBoxen(obj) ;boxent obj; initiateBoxen(&obj)
    175 #define createAllocateBoxen(obj) ;boxent *obj; initiateAllocateBoxen(&obj)
    176 
    177 void initiateBoxen(boxent *self);
    178 void initiateAllocateBoxen(boxent **self);
    179 void finalizeBoxen(void);
    180 
    181 /* initialize class methods, call registerMethodsBoxen from classes inheriting this class */
    182 void registerMethodsBoxen(boxenFunctionst *f);
    183 
    184 boxent* allocBoxen(void);
    185 
    186 /* end class boxen*/
    187 // vim: set expandtab ts=2 sw=2:
    188 
    189 #define isBoxenCompiledWithCurrentLisheepyVersion checkLibsheepyVersionBoxen(LIBSHEEPY_VERSION)
    190 bool checkLibsheepyVersionBoxen(const char *currentLibsheepyVersion);