dclib-color.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. /***************************************************************************
  2. * *
  3. * _____ ____ *
  4. * | __ \ / __ \ _ _ _____ *
  5. * | | \ \ / / \_\ | | | | _ \ *
  6. * | | \ \| | | | | | |_| | *
  7. * | | | || | | | | | ___/ *
  8. * | | / /| | __ | | | | _ \ *
  9. * | |__/ / \ \__/ / | |___| | |_| | *
  10. * |_____/ \____/ |_____|_|_____/ *
  11. * *
  12. * Wiimms source code library *
  13. * *
  14. ***************************************************************************
  15. * *
  16. * Copyright (c) 2012-2022 by Dirk Clemens <wiimm@wiimm.de> *
  17. * *
  18. ***************************************************************************
  19. * *
  20. * This library is free software; you can redistribute it and/or modify *
  21. * it under the terms of the GNU General Public License as published by *
  22. * the Free Software Foundation; either version 2 of the License, or *
  23. * (at your option) any later version. *
  24. * *
  25. * This library is distributed in the hope that it will be useful, *
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  28. * GNU General Public License for more details. *
  29. * *
  30. * See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
  31. * *
  32. ***************************************************************************/
  33. #ifndef DCLIB_COLOR_H
  34. #define DCLIB_COLOR_H 1
  35. #define _GNU_SOURCE 1
  36. #include <string.h>
  37. #include "dclib-types.h"
  38. #include "dclib-debug.h"
  39. //
  40. ///////////////////////////////////////////////////////////////////////////////
  41. /////////////// color modes ///////////////
  42. ///////////////////////////////////////////////////////////////////////////////
  43. #ifndef SUPPORT_36_COLORS
  44. #define SUPPORT_36_COLORS 1
  45. #endif
  46. //-----------------------------------------------------------------------------
  47. // [[ColorMode_t]]
  48. typedef enum ColorMode_t
  49. {
  50. COLMD_OFF = -1, // don't support colors
  51. COLMD_AUTO = 0, // auto detect
  52. COLMD_ON, // on, auto select mode
  53. COLMD_8_COLORS, // force 8 color mode
  54. COLMD_256_COLORS, // force 256 color mode
  55. }
  56. ColorMode_t;
  57. //-----------------------------------------------------------------------------
  58. struct KeywordTab_t;
  59. extern const struct KeywordTab_t color_mode_keywords[];
  60. extern ColorMode_t opt_colorize; // <0:no color, 0:auto, >0:force color
  61. int ScanOptColorize ( ColorMode_t *opt, ccp arg, ccp err_prefix );
  62. ccp GetColorModeName ( ColorMode_t col_mode, ccp res_not_found );
  63. //-----------------------------------------------------------------------------
  64. // returns COLMD_OFF | COLMD_8_COLORS | COLMD_256_COLORS
  65. ColorMode_t NormalizeColorMode ( ColorMode_t col_mode, int n_colors );
  66. ColorMode_t NormalizeColorModeByTermName
  67. (
  68. ColorMode_t col_mode, // predefind mode
  69. ccp term_name // find "256color" in terminal name.
  70. // if NULL: use getenv("TERM")
  71. // assume 8 or 256 colors!
  72. );
  73. struct TerminalInfo_t;
  74. ColorMode_t NormalizeColorModeByTerm
  75. (
  76. ColorMode_t col_mode, // predefind mode
  77. const struct
  78. TerminalInfo_t *term // if NULL: use getenv("TERM") to find terminal
  79. );
  80. // iteration
  81. ColorMode_t GetNextColorMode ( ColorMode_t col_mode );
  82. ColorMode_t GetPrevColorMode ( ColorMode_t col_mode );
  83. //
  84. ///////////////////////////////////////////////////////////////////////////////
  85. /////////////// terminal & colors ///////////////
  86. ///////////////////////////////////////////////////////////////////////////////
  87. #define N_COLORSET_GREY 10
  88. #define N_COLORSET_HL_LINE 4
  89. #define N_COLORSET_ORDER 9
  90. extern int termios_valid; // valid if >0
  91. void ResetTermios(void);
  92. bool EnableSingleCharInput(void);
  93. ///////////////////////////////////////////////////////////////////////////////
  94. // Sequence counter for output to stdout (and stderr).
  95. // Functions shall increment it on output. so that other functions
  96. // know about an destroid screen.
  97. extern uint stdout_seq_count;
  98. //-----------------------------------------------------------------------------
  99. // [[term_size_t]]
  100. typedef struct term_size_t
  101. {
  102. uint width;
  103. uint height;
  104. }
  105. term_size_t;
  106. extern uint opt_width, opt_max_width, opt_height, opt_max_height;
  107. int ScanOptWidth ( ccp arg );
  108. int ScanOptMaxWidth ( ccp arg );
  109. int ScanOptHeight ( ccp arg );
  110. int ScanOptMaxHeight ( ccp arg );
  111. term_size_t GetTermSize ( int default_width, int default_height );
  112. term_size_t GetTermSizeFD ( int fd, int default_width, int default_height );
  113. int GetTermWidth ( int default_value, int min_value );
  114. int GetTermWidthFD ( int fd, int default_value, int min_value );
  115. int GetTermHeight ( int default_value, int min_value );
  116. int GetTermHeightFD ( int fd, int default_value, int min_value );
  117. //-----------------------------------
  118. extern term_size_t auto_term_size; // default = {80,25};
  119. extern uint auto_term_resized; // incremented for each resize, init by 0
  120. extern uint auto_term_size_dirty; // >0: SIGWINCH received, auto_term_size is dirty
  121. bool GetAutoTermSize(void); // return true if changed
  122. void EnableAutoTermSize(void);
  123. ///////////////////////////////////////////////////////////////////////////////
  124. // [[good_term_width_t]]
  125. typedef struct good_term_width_t
  126. {
  127. int def_width;
  128. int min_width;
  129. int max_width;
  130. int good_width;
  131. bool setup_done;
  132. }
  133. good_term_width_t;
  134. extern good_term_width_t good_term_width;
  135. // If !gtw: Use good_term_width
  136. int GetGoodTermWidth ( good_term_width_t *gtw, bool recalc );
  137. ///////////////////////////////////////////////////////////////////////////////
  138. //----- some basic terminal codes
  139. #define TERM_CURSOR_UP "\e[A" // move cursor up
  140. #define TERM_CURSOR_DOWN "\e[B" // move cursor down
  141. #define TERM_CURSOR_RIGHT "\e[C" // move cursor right
  142. #define TERM_CURSOR_LEFT "\e[D" // move cursor left
  143. #define TERM_CURSOR_UP_N "\e[%dA" // move cursor N* up
  144. #define TERM_CURSOR_DOWN_N "\e[%dB" // move cursor N* down
  145. #define TERM_CURSOR_RIGHT_N "\e[%dC" // move cursor N* right
  146. #define TERM_CURSOR_LEFT_N "\e[%dD" // move cursor N* left
  147. #define TERM_CLEAR_EOL "\e[K" // clear from cursor to end of line
  148. #define TERM_CLEAR_BOL "\e[1K" // clear from beginning of line to cursor
  149. #define TERM_CLEAR_LINE "\e[2K" // clear entire line
  150. #define TERM_CLEAR_EOD "\e[J" // clear from cursor to end of display
  151. #define TERM_CLEAR_BOD "\e[1J" // clear from beginning of display to cursor
  152. #define TERM_CLEAR_DISPLAY "\e[2J" // clear Entire display
  153. #define TERM_SET_WRAP "\e[?7h" // enabled line wrapping
  154. #define TERM_RESET_WRAP "\e[?7l" // disable line wrapping
  155. #define TERM_TEXT_MODE_BEG "\e[" // begin of text mode sequence
  156. #define TERM_TEXT_MODE_BEG0 "\e[0;" // begin of text mode sequence with reset
  157. #define TERM_TEXT_MODE_END "m" // end of text mode sequence
  158. #define TERM_TEXT_MODE_RESET "\e[m" // reset text mode
  159. // pointers are initialized with codes above
  160. extern ccp TermCursorUp; // move cursor up
  161. extern ccp TermCursorDown; // move cursor down
  162. extern ccp TermCursorRight; // move cursor right
  163. extern ccp TermCursorLeft; // move cursor left
  164. extern ccp TermCursorUpN; // move cursor N* up (format string with 1 '%d')
  165. extern ccp TermCursorDownN; // move cursor N* down (format string with 1 '%d')
  166. extern ccp TermCursorRightN; // move cursor N* right (format string with 1 '%d')
  167. extern ccp TermCursorLeftN; // move cursor N* left (format string with 1 '%d')
  168. extern ccp TermClearEOL; // clear from cursor to end of line
  169. extern ccp TermClearBOL; // clear from beginning of line to cursor
  170. extern ccp TermClearLine; // clear entire line
  171. extern ccp TermClearEOD; // clear from cursor to end of display
  172. extern ccp TermClearBOD; // clear from beginning of display to cursor
  173. extern ccp TermClearDisplay; // clear entire display
  174. extern ccp TermSetWrap; // enabled line wrapping
  175. extern ccp TermResetWrap; // disable line wrapping
  176. extern ccp TermTextModeBeg; // begin of text mode sequence
  177. extern ccp TermTextModeBeg0; // begin of text mode sequence with reset
  178. extern ccp TermTextModeEnd; // end of text mode sequence
  179. extern ccp TermTextModeReset; // reset text mode
  180. //-----------------------------------------------------------------------------
  181. // [[TermTextMode_t]]
  182. typedef enum TermTextMode_t
  183. {
  184. //--- standard fg colors: \e[30..37m
  185. TTM_BLACK = 0x08,
  186. TTM_RED = 0x09,
  187. TTM_GREEN = 0x0a,
  188. TTM_YELLOW = 0x0b,
  189. TTM_BLUE = 0x0c,
  190. TTM_MAGENTA = 0x0d,
  191. TTM_CYAN = 0x0e,
  192. TTM_WHITE = 0x0f,
  193. //--- standard bg colors: \e[40..47m
  194. TTM_BG_BLACK = 0x80,
  195. TTM_BG_RED = 0x90,
  196. TTM_BG_GREEN = 0xa0,
  197. TTM_BG_YELLOW = 0xb0,
  198. TTM_BG_BLUE = 0xc0,
  199. TTM_BG_MAGENTA = 0xd0,
  200. TTM_BG_CYAN = 0xe0,
  201. TTM_BG_WHITE = 0xf0,
  202. //--- grayscale: \e[48;5;<VAL>m
  203. TTM_GRAY0 = 0x04, // black
  204. TTM_GRAY1 = 0x05, // dark gray
  205. TTM_GRAY2 = 0x06, // light gray
  206. TTM_GRAY3 = 0x07, // real white
  207. TTM_BG_GRAY0 = 0x40, // black
  208. TTM_BG_GRAY1 = 0x50, // dark gray
  209. TTM_BG_GRAY2 = 0x60, // light gray
  210. TTM_BG_GRAY3 = 0x70, // real white
  211. //--- misc
  212. TTM_RESET = 0x0100,
  213. TTM_BOLD = 0x0200,
  214. TTM_NO_BOLD = 0x0400,
  215. TTM_UL = 0x0800,
  216. TTM_NO_UL = 0x1000,
  217. //--- flags and masks ans shifts
  218. TTM_F_COLOR = 0x08,
  219. TTM_M_COLOR = 0x07,
  220. TTM_F_GRAYSCALE = 0x04, // only valid if !TTM_F_COLOR
  221. TTM_M_GRAYSCALE = 0x03,
  222. TTM_F_BG_COLOR = 0x80,
  223. TTM_M_BG_COLOR = 0x70,
  224. TTM_F_BG_GRAYSCALE = 0x40, // only valid if !TTM_F_BG_COLOR
  225. TTM_M_BG_GRAYSCALE = 0x30,
  226. TTM_SHIFT_BG = 4,
  227. //--- and her are some recommendations for unique colors
  228. //--- these Colors are also used to setup 'ColorSet_t'
  229. // [[new-color]]
  230. TTM_COL_SETUP = TTM_RESET | TTM_BOLD | TTM_BLUE,
  231. TTM_COL_RUN = TTM_RESET | TTM_BOLD | TTM_WHITE,
  232. TTM_COL_ABORT = TTM_RESET | TTM_BOLD | TTM_MAGENTA,
  233. TTM_COL_FINISH = TTM_RESET | TTM_BOLD | TTM_CYAN,
  234. TTM_COL_SCRIPT = TTM_RESET | TTM_NO_BOLD | TTM_BLACK | TTM_BG_WHITE,
  235. TTM_COL_OPEN = TTM_RESET | TTM_NO_BOLD | TTM_RED,
  236. TTM_COL_CLOSE = TTM_RESET | TTM_NO_BOLD | TTM_RED,
  237. TTM_COL_FILE = TTM_RESET | TTM_NO_BOLD | TTM_YELLOW,
  238. TTM_COL_JOB = TTM_RESET | TTM_BOLD | TTM_BLUE,
  239. TTM_COL_DEBUG = TTM_RESET | TTM_BOLD | TTM_RED,
  240. TTM_COL_LOG = TTM_RESET | TTM_BOLD | TTM_WHITE | TTM_BG_BLUE,
  241. TTM_COL_MARK = TTM_RESET | TTM_BOLD | TTM_WHITE,
  242. TTM_COL_INFO = TTM_RESET | TTM_BOLD | TTM_CYAN,
  243. TTM_COL_HINT = TTM_RESET | TTM_BOLD | TTM_YELLOW,
  244. TTM_COL_WARN = TTM_RESET | TTM_BOLD | TTM_RED,
  245. TTM_COL_ERR = TTM_RESET | TTM_BOLD | TTM_MAGENTA,
  246. TTM_COL_BAD = TTM_RESET | TTM_BOLD | TTM_MAGENTA,
  247. TTM_COL_NAME = TTM_RESET | TTM_NO_BOLD | TTM_CYAN,
  248. TTM_COL_OP = TTM_RESET | TTM_BOLD | TTM_WHITE,
  249. TTM_COL_VALUE = TTM_RESET | TTM_NO_BOLD | TTM_GREEN,
  250. TTM_COL_SUCCESS = TTM_RESET | TTM_BOLD | TTM_GREEN,
  251. TTM_COL_ERROR = TTM_RESET | TTM_NO_BOLD | TTM_BLACK | TTM_BG_YELLOW,
  252. TTM_COL_FAIL = TTM_RESET | TTM_NO_BOLD | TTM_WHITE | TTM_BG_RED,
  253. TTM_COL_FAIL2 = TTM_RESET | TTM_BOLD | TTM_WHITE | TTM_BG_RED,
  254. TTM_COL_FATAL = TTM_RESET | TTM_BOLD | TTM_WHITE | TTM_BG_MAGENTA,
  255. TTM_COL_SELECT = TTM_RESET | TTM_NO_BOLD | TTM_YELLOW,
  256. TTM_COL_DIFFER = TTM_RESET | TTM_BOLD | TTM_YELLOW | TTM_BG_BLUE,
  257. TTM_COL_HL_LINE0 = TTM_RESET | TTM_NO_BOLD | TTM_WHITE,
  258. TTM_COL_HL_LINE1 = TTM_RESET | TTM_BOLD | TTM_CYAN,
  259. TTM_COL_HL_LINE2 = TTM_RESET | TTM_BOLD | TTM_GREEN,
  260. TTM_COL_HL_LINE3 = TTM_RESET | TTM_BOLD | TTM_MAGENTA,
  261. TTM_COL_ORDER0 = TTM_RESET | TTM_NO_BOLD | TTM_WHITE,
  262. TTM_COL_ORDER1 = TTM_RESET | TTM_BOLD | TTM_BLUE,
  263. TTM_COL_ORDER2 = TTM_RESET | TTM_BOLD | TTM_CYAN,
  264. TTM_COL_ORDER3 = TTM_RESET | TTM_BOLD | TTM_GREEN,
  265. TTM_COL_ORDER4 = TTM_RESET | TTM_BOLD | TTM_YELLOW,
  266. TTM_COL_ORDER5 = TTM_RESET | TTM_BOLD | TTM_RED,
  267. TTM_COL_ORDER6 = TTM_RESET | TTM_BOLD | TTM_MAGENTA,
  268. TTM_COL_ORDER7 = TTM_RESET | TTM_BOLD | TTM_WHITE,
  269. TTM_COL_ORDER8 = TTM_RESET | TTM_BOLD | TTM_WHITE,
  270. TTM_COL_STAT_LINE = TTM_RESET | TTM_BOLD | TTM_WHITE | TTM_BG_BLUE,
  271. TTM_COL_WARN_LINE = TTM_RESET | TTM_BOLD | TTM_RED | TTM_BG_BLUE,
  272. TTM_COL_PROC_LINE = TTM_RESET | TTM_BOLD | TTM_WHITE | TTM_BG_MAGENTA,
  273. TTM_COL_CITE = TTM_RESET | TTM_NO_BOLD | TTM_GREEN,
  274. TTM_COL_STATUS = TTM_RESET | TTM_BOLD | TTM_GREEN,
  275. TTM_COL_HIGHLIGHT = TTM_RESET | TTM_BOLD | TTM_YELLOW,
  276. TTM_COL_HIDE = TTM_RESET | TTM_BOLD | TTM_BLACK,
  277. TTM_COL_HEADING = TTM_RESET | TTM_BOLD | TTM_BLUE,
  278. TTM_COL_CAPTION = TTM_RESET | TTM_BOLD | TTM_CYAN,
  279. TTM_COL_SECTION = TTM_RESET | TTM_BOLD | TTM_GREEN,
  280. TTM_COL_SYNTAX = TTM_RESET | TTM_BOLD | TTM_WHITE,
  281. TTM_COL_CMD = TTM_RESET | TTM_NO_BOLD | TTM_CYAN,
  282. TTM_COL_OPTION = TTM_RESET | TTM_NO_BOLD | TTM_GREEN,
  283. TTM_COL_PARAM = TTM_RESET | TTM_NO_BOLD | TTM_YELLOW,
  284. TTM_COL_ON = TTM_RESET | TTM_BOLD | TTM_GREEN,
  285. TTM_COL_OFF = TTM_RESET | TTM_BOLD | TTM_RED,
  286. }
  287. TermTextMode_t;
  288. //-----------------------------------------------------------------------------
  289. // [[TermColorIndex_t]]
  290. typedef enum TermColorIndex_t
  291. {
  292. #if SUPPORT_36_COLORS
  293. TCI_GRAY0, // gray scale
  294. TCI_GRAY1,
  295. TCI_GRAY2,
  296. TCI_GRAY3,
  297. TCI_GRAY4,
  298. TCI_GRAY5,
  299. TCI_GRAY6,
  300. TCI_GRAY7,
  301. TCI_RED, // normal colors in rainbow order
  302. TCI_RED_ORANGE,
  303. TCI_ORANGE,
  304. TCI_ORANGE_YELLOW,
  305. TCI_YELLOW,
  306. TCI_YELLOW_GREEN,
  307. TCI_GREEN,
  308. TCI_GREEN_CYAN,
  309. TCI_CYAN,
  310. TCI_CYAN_BLUE,
  311. TCI_BLUE,
  312. TCI_BLUE_MAGENTA,
  313. TCI_MAGENTA,
  314. TCI_MAGENTA_RED,
  315. TCI_B_RED, // bold colors in rainbow order
  316. TCI_B_RED_ORANGE,
  317. TCI_B_ORANGE,
  318. TCI_B_ORANGE_YELLOW,
  319. TCI_B_YELLOW,
  320. TCI_B_YELLOW_GREEN,
  321. TCI_B_GREEN,
  322. TCI_B_GREEN_CYAN,
  323. TCI_B_CYAN,
  324. TCI_B_CYAN_BLUE,
  325. TCI_B_BLUE,
  326. TCI_B_BLUE_MAGENTA,
  327. TCI_B_MAGENTA,
  328. TCI_B_MAGENTA_RED,
  329. TCI__IGNORE, // ignore this param, for GetColorMode()
  330. //--- alternative names
  331. TCI_BLACK = TCI_GRAY0,
  332. TCI_DARKGRAY = TCI_GRAY2,
  333. TCI_LIGHTGRAY = TCI_GRAY5,
  334. TCI_WHITE = TCI_GRAY7,
  335. //--- counters
  336. TCI__N = TCI__IGNORE, // number of supported colors
  337. TCI__N_FONT = TCI_B_RED, // number of recommended font colors
  338. TCI__N_BG = TCI__N, // number of recommended background colors
  339. TCI__N_GREY = TCI_RED, // number of gray tones
  340. #else // !SUPPORT_36_COLORS
  341. TCI_BLACK, // gray scale
  342. TCI_DARKGRAY,
  343. TCI_LIGHTGRAY,
  344. TCI_WHITE,
  345. TCI_RED, // normal colors in rainbow order
  346. TCI_ORANGE,
  347. TCI_YELLOW,
  348. TCI_GREEN,
  349. TCI_CYAN,
  350. TCI_BLUE,
  351. TCI_MAGENTA,
  352. TCI_B_RED, // bold colors in rainbow order
  353. TCI_B_ORANGE,
  354. TCI_B_YELLOW,
  355. TCI_B_GREEN,
  356. TCI_B_CYAN,
  357. TCI_B_BLUE,
  358. TCI_B_MAGENTA,
  359. TCI__IGNORE, // ignore this param, for GetColorMode()
  360. //--- counters
  361. TCI__N = TCI__IGNORE, // number of supported colors
  362. TCI__N_FONT = TCI_B_RED, // number of recommended font colors
  363. TCI__N_BG = TCI__N, // number of recommended background colors
  364. TCI__N_GREY = TCI_RED, // number of gray tones
  365. #endif // !SUPPORT_36_COLORS
  366. }
  367. TermColorIndex_t;
  368. #if SUPPORT_36_COLORS
  369. extern const char ColorIndexName[TCI__N][16];
  370. #else
  371. extern const char ColorIndexName[TCI__N][11];
  372. #endif
  373. extern const int Color18Index[18+1]; // 18 elements + -1 as terminator
  374. //-----------------------------------------------------------------------------
  375. // [[TermColorIndexEx_t]]
  376. typedef enum TermColorIndexEx_t
  377. {
  378. TCE_GREY0,
  379. TCE_GREY1,
  380. TCE_GREY2,
  381. TCE_GREY3,
  382. TCE_GREY4,
  383. TCE_GREY5,
  384. TCE_GREY6,
  385. TCE_GREY7,
  386. TCE_GREY8,
  387. TCE_GREY9,
  388. TCE_RED_MAGENTA,
  389. TCE_RED,
  390. TCE_RED_ORANGE,
  391. TCE_ORANGE_RED,
  392. TCE_ORANGE,
  393. TCE_ORANGE_YELLOW,
  394. TCE_YELLOW_ORANGE,
  395. TCE_YELLOW,
  396. TCE_YELLOW_GREEN,
  397. TCE_GREEN_YELLOW,
  398. TCE_GREEN,
  399. TCE_GREEN_CYAN,
  400. TCE_CYAN_GREEN,
  401. TCE_CYAN,
  402. TCE_CYAN_BLUE,
  403. TCE_BLUE_CYAN,
  404. TCE_BLUE,
  405. TCE_BLUE_MAGENTA,
  406. TCE_MAGENTA_BLUE,
  407. TCE_MAGENTA,
  408. TCE_MAGENTA_RED,
  409. TCE_B_RED_MAGENTA,
  410. TCE_B_RED,
  411. TCE_B_RED_ORANGE,
  412. TCE_B_ORANGE_RED,
  413. TCE_B_ORANGE,
  414. TCE_B_ORANGE_YELLOW,
  415. TCE_B_YELLOW_ORANGE,
  416. TCE_B_YELLOW,
  417. TCE_B_YELLOW_GREEN,
  418. TCE_B_GREEN_YELLOW,
  419. TCE_B_GREEN,
  420. TCE_B_GREEN_CYAN,
  421. TCE_B_CYAN_GREEN,
  422. TCE_B_CYAN,
  423. TCE_B_CYAN_BLUE,
  424. TCE_B_BLUE_CYAN,
  425. TCE_B_BLUE,
  426. TCE_B_BLUE_MAGENTA,
  427. TCE_B_MAGENTA_BLUE,
  428. TCE_B_MAGENTA,
  429. TCE_B_MAGENTA_RED,
  430. TCE__N,
  431. TCE_BLACK = TCE_GREY0,
  432. TCE_WHITE = TCE_GREY9,
  433. }
  434. TermColorIndexEx_t;
  435. //-----------------------------------------------------------------------------
  436. // [[TermColorWarn_t]]
  437. typedef enum TermColorWarn_t
  438. {
  439. TCW_MARK,
  440. TCW_INFO,
  441. TCW_HINT,
  442. TCW_WARN,
  443. TCW_ERR,
  444. TCW_BAD,
  445. TCW__N,
  446. }
  447. TermColorWarn_t;
  448. //-----------------------------------------------------------------------------
  449. // [[TermColorId_t]]
  450. typedef struct TermColorId_t
  451. {
  452. union
  453. {
  454. u8 col[TCE__N];
  455. u8 gray[N_COLORSET_GREY];
  456. };
  457. }
  458. TermColorId_t;
  459. // values 0x00-0x07 for standard, 0x10-0x17 for bold
  460. extern const TermColorId_t color8id;
  461. // values for 38;5;x or 48;5;x
  462. extern const TermColorId_t color256id;
  463. //-----------------------------------------------------------------------------
  464. // [[ColorSet_t]]
  465. typedef struct ColorSet_t
  466. {
  467. //--- header
  468. ColorMode_t col_mode;
  469. u8 colorize;
  470. u16 n_colors;
  471. //--- some strings for formatted output if colors active
  472. ccp space; // pointer to 'EmptyString' or to 'Space200';
  473. ccp tab; // pointer to 'EmptyString' or to 'TAB20';
  474. ccp lf; // pointer to 'EmptyString' or to 'LF20';
  475. //--- data, always begins with 'reset'
  476. // [[new-color]]
  477. ccp reset; // always first color!!
  478. ccp setup;
  479. ccp run;
  480. ccp abort;
  481. ccp finish;
  482. ccp script;
  483. ccp open;
  484. ccp close;
  485. ccp file;
  486. ccp job;
  487. ccp debug;
  488. ccp log;
  489. union
  490. {
  491. ccp warn_level[TCW__N];
  492. struct
  493. {
  494. ccp mark;
  495. ccp info;
  496. ccp hint;
  497. ccp warn;
  498. ccp err;
  499. ccp bad;
  500. };
  501. };
  502. ccp name;
  503. ccp op;
  504. ccp value;
  505. ccp success;
  506. ccp error;
  507. ccp fail;
  508. ccp fail2;
  509. ccp fatal;
  510. ccp hl_line[N_COLORSET_HL_LINE];
  511. ccp order[N_COLORSET_ORDER];
  512. ccp select;
  513. ccp differ;
  514. ccp stat_line;
  515. ccp warn_line;
  516. ccp proc_line;
  517. ccp cite;
  518. ccp status;
  519. ccp highlight;
  520. ccp hide;
  521. ccp heading;
  522. ccp caption;
  523. ccp section;
  524. ccp syntax;
  525. ccp cmd;
  526. ccp option;
  527. ccp param;
  528. ccp on;
  529. ccp off;
  530. ccp black;
  531. ccp b_black;
  532. ccp white;
  533. ccp b_white;
  534. ccp grey[N_COLORSET_GREY];
  535. ccp red_magenta;
  536. ccp red;
  537. ccp red_orange;
  538. ccp orange_red;
  539. ccp orange;
  540. ccp orange_yellow;
  541. ccp yellow_orange;
  542. ccp yellow;
  543. ccp yellow_green;
  544. ccp green_yellow;
  545. ccp green;
  546. ccp green_cyan;
  547. ccp cyan_green;
  548. ccp cyan;
  549. ccp cyan_blue;
  550. ccp blue_cyan;
  551. ccp blue;
  552. ccp blue_magenta;
  553. ccp magenta_blue;
  554. ccp magenta;
  555. ccp magenta_red;
  556. ccp b_red_magenta;
  557. ccp b_red;
  558. ccp b_red_orange;
  559. ccp b_orange_red;
  560. ccp b_orange;
  561. ccp b_orange_yellow;
  562. ccp b_yellow_orange;
  563. ccp b_yellow;
  564. ccp b_yellow_green;
  565. ccp b_green_yellow;
  566. ccp b_green;
  567. ccp b_green_cyan;
  568. ccp b_cyan_green;
  569. ccp b_cyan;
  570. ccp b_cyan_blue;
  571. ccp b_blue_cyan;
  572. ccp b_blue;
  573. ccp b_blue_magenta;
  574. ccp b_magenta_blue;
  575. ccp b_magenta;
  576. ccp b_magenta_red;
  577. ccp matrix[TCI__N_FONT][TCI__N_BG];
  578. //--- line support (mutable)
  579. ccp line_begin; // use this at beginning of line
  580. ccp line_reset; // use this in line to reset line color
  581. ccp line_end; // use this at end of line
  582. }
  583. ColorSet_t;
  584. //-----------------------------------------------------------------------------
  585. // [[ColorSelect_t]]
  586. typedef enum ColorSelect_t
  587. {
  588. COLSEL_COLOR = 0x01, // standard colors
  589. COLSEL_B_COLOR = 0x02, // bold colors
  590. COLSEL_GREY = 0x04, // grey scale
  591. COLSEL_NAME1 = 0x08, // semantic color names, part 1
  592. COLSEL_NAME2 = 0x10, // semantic color names, part 2
  593. COLSEL_NAME = 0x18, // semantic color names, both parts
  594. COLSEL_F_ALT = 0x20,
  595. COLSEL_M_COLOR = COLSEL_COLOR|COLSEL_B_COLOR,
  596. COLSEL_M_NONAME = COLSEL_COLOR|COLSEL_B_COLOR|COLSEL_GREY,
  597. COLSEL_M_MODE = 0x0f,
  598. COLSEL_M_ALL = 0x1f,
  599. }
  600. ColorSelect_t;
  601. //-----------------------------------------------------------------------------
  602. ccp GetTextMode
  603. (
  604. // returns a pointer to an internal circulary buffer (not GetCircBuf())
  605. ColorMode_t col_mode, // if < COLMD_ON: return an empty string
  606. TermTextMode_t mode // mode for text generation
  607. );
  608. //-----------------------------------------------------------------------------
  609. // [[GetColorOption_t]]
  610. typedef enum GetColorOption_t
  611. {
  612. GCM_ALT = 0x01, // use recommended font color if font+bg is not readable
  613. GCM_SHORT = 0x02, // allow alternative short codes (codes 30-37 and 40-47)
  614. }
  615. GetColorOption_t;
  616. ccp GetColorMode
  617. (
  618. // returns a pointer to an internal circulary buffer (not GetCircBuf())
  619. ColorMode_t col_mode, // if < COLMD_ON: return an empty string
  620. TermColorIndex_t font_index, // index of font color
  621. TermColorIndex_t bg_index, // index of background color
  622. GetColorOption_t option // execution options
  623. );
  624. //-----------------------------------------------------------------------------
  625. typedef void (*PrintColorFunc)
  626. (
  627. // On start of group: col_name := NULL, col_string := NULL
  628. FILE *f, // valid output file
  629. int indent, // normalized indention of output
  630. const ColorSet_t *cs, // valid color set, never NULL
  631. ccp col_name, // name of color
  632. ccp col_string // escape string for the color
  633. );
  634. //-----------------------------------------------------------------------------
  635. const ColorSet_t * GetColorSet ( ColorMode_t col_mode );
  636. const ColorSet_t * GetColorSet0(void);
  637. const ColorSet_t * GetColorSet8(void);
  638. const ColorSet_t * GetColorSet256(void);
  639. const ColorSet_t * GetColorSetAuto ( bool force_on );
  640. const ColorSet_t * GetFileColorSet ( FILE *f );
  641. bool SetupColorSet ( ColorSet_t *cc, FILE *f );
  642. int GetColorOffsetByName ( ccp name ); // returns -1 if not found
  643. // returns EmptyString[] if not found
  644. ccp GetColorByName ( const ColorSet_t *colset, ccp name );
  645. ccp GetColorByOffsetOrName ( const ColorSet_t *colset, int * offset, ccp name );
  646. extern const char EmptyString[];
  647. static inline ccp GetColorByOffset ( const ColorSet_t *colset, int offset )
  648. { return offset > 0 ? *(ccp*)((u8*)colset+offset) : EmptyString; }
  649. static inline ccp GetColorByValidOffset ( const ColorSet_t *colset, int offset )
  650. { return *(ccp*)((u8*)colset+offset); }
  651. // both will change line_col & line_reset
  652. void ResetLineColor ( const ColorSet_t *colset );
  653. void SetLineColor ( const ColorSet_t *colset, int level );
  654. void PrintColorSet
  655. (
  656. FILE *f, // valid output file
  657. int indent, // indention of output
  658. const ColorSet_t *cs, // valid color set; if NULL: use std colors
  659. ColorSelect_t select, // select color groups
  660. uint format // output format
  661. // 0: colored list
  662. // 1: shell definitions
  663. );
  664. void PrintColorSetHelper
  665. (
  666. FILE *f, // valid output file
  667. int indent, // indention of output
  668. const ColorSet_t *cs, // valid color set; if NULL: use std colors
  669. PrintColorFunc func, // output function, never NULL
  670. ColorSelect_t select, // select color groups
  671. int multi_assign // >0: Allow A=B="..."; >1: reorder records too
  672. );
  673. //
  674. ///////////////////////////////////////////////////////////////////////////////
  675. /////////////// color helpers ///////////////
  676. ///////////////////////////////////////////////////////////////////////////////
  677. extern u32 ColorTab_M0_M15[16]; // first 16 colors of "\e[m"
  678. // return a m256 index
  679. u8 ConvertColorRGB3ToM256 ( u8 r, u8 g, u8 b );
  680. u8 ConvertColorRGBToM256 ( u32 rgb );
  681. u32 ConvertColorM256ToRGB ( u8 m256 );
  682. static inline uint SingleColorToM6 ( u8 col )
  683. { return col > 114 ? ( col - 35 ) / 40 : col > 47; }
  684. //
  685. ///////////////////////////////////////////////////////////////////////////////
  686. /////////////// ViewColor*() ///////////////
  687. ///////////////////////////////////////////////////////////////////////////////
  688. // [[ColorView_t]] : parameters to view color sets
  689. typedef struct ColorView_t
  690. {
  691. FILE *f; // valid output file
  692. int indent; // indention of output
  693. const struct TerminalInfo_t
  694. *term; // not NULL: us it to find 'colset'
  695. ColorMode_t col_mode; // color mode -> NormalizeColorMode()
  696. const ColorSet_t *colset; // not NULL: ignore 'col_mode'
  697. TermColorIndex_t std_col; // standard bg or fg color, TCI__IGNORE allowed
  698. int order; // 0|'r' | 1|'g' | 2|'b'
  699. GetColorOption_t col_option; // color option
  700. uint mode; // function specific mode (modulo 60)
  701. bool print_alt; // print alternative names
  702. bool allow_sep; // allow separator lines
  703. void *user_ptr; // any pointer
  704. int user_int; // any number or id
  705. }
  706. ColorView_t;
  707. ///////////////////////////////////////////////////////////////////////////////
  708. static inline void InitializeColorView ( ColorView_t *cv )
  709. { DASSERT(cv); memset(cv,0,sizeof(*cv)); cv->std_col = TCI__IGNORE; }
  710. void SetupColorView ( ColorView_t *cv );
  711. void SetupColorViewMode ( ColorView_t *cv, ColorMode_t col_mode );
  712. void ViewColorsAttrib8 ( ColorView_t *cv );
  713. void ViewColorsCombi8 ( ColorView_t *cv );
  714. void ViewColors18 ( ColorView_t *cv );
  715. void ViewColorsDC ( ColorView_t *cv, bool optimized );
  716. void ViewColors256 ( ColorView_t *cv );
  717. void ViewColorsPredef ( ColorView_t *cv, ColorSelect_t select );
  718. #define VIEW_COLOR_PAGES 10
  719. void ViewColorsPage ( ColorView_t *cv, uint page );
  720. ///////////////////////////////////////////////////////////////////////////////
  721. ///////////////////////////////////////////////////////////////////////////////
  722. // legacy functions (deprecated) ==> wrapper for ViewColors*()
  723. void PrintTextModes
  724. (
  725. // => ViewColorsAttrib8() & ViewColorsCombi8()
  726. FILE *f, // valid output file
  727. int indent, // indention of output
  728. ColorMode_t col_mode // color mode -> NormalizeColorMode()
  729. );
  730. void PrintColorModes
  731. (
  732. FILE *f, // valid output file
  733. int indent, // indention of output
  734. ColorMode_t col_mode, // color mode -> NormalizeColorMode()
  735. GetColorOption_t option // execution options (GCM_ALT ignored)
  736. );
  737. //
  738. ///////////////////////////////////////////////////////////////////////////////
  739. /////////////// std*, col* ///////////////
  740. ///////////////////////////////////////////////////////////////////////////////
  741. ////////////////////////////////////////////////////////////////////////////
  742. //
  743. // We have 5 standard output files
  744. //
  745. // stdout, colout:
  746. // Standard output like usual.
  747. //
  748. // stderr, colerr:
  749. // Standard error output like usual. It is usually not redirected by
  750. // RedirectStdFiles() and points always to its original.
  751. //
  752. // stdlog, collog:
  753. // Logging output, may be NULL to tell: no logging.
  754. // RedirectStdFiles() don't cange it, if it is NULL.
  755. //
  756. // stdmsg, colmsg:
  757. // Special output for catcable messages. Initialized by SetupStdMsg()
  758. // and set to the first not NULL of stdout, stderr and stdlog.
  759. //
  760. // stdwrn, colwrn:
  761. // Output file of PrintError() functions.
  762. // Initialized to a copy of stderr.
  763. //
  764. ////////////////////////////////////////////////////////////////////////////
  765. extern FILE *stdlog; // NULL or logfile
  766. extern bool close_stdlog; // true: close file behing 'stdlog'
  767. extern FILE *stdmsg; // 'stdlog' or 'stdout' or 'stderr' or NULL
  768. extern FILE *stdwrn; // used by PrintError(), initialized with 'stderr'
  769. extern ColorMode_t colorize_stdout; // <0:no color, 0:auto, >0:force color
  770. extern ColorMode_t colorize_stderr;
  771. extern ColorMode_t colorize_stdlog;
  772. extern ColorMode_t colorize_stdmsg;
  773. extern ColorMode_t colorize_stdwrn;
  774. extern const ColorSet_t *colout; // color set for 'stdout'
  775. extern const ColorSet_t *colerr; // color set for 'stderr'
  776. extern const ColorSet_t *collog; // color set for 'stdlog'
  777. extern const ColorSet_t *colmsg; // color set for 'stdmsg'
  778. extern const ColorSet_t *colwrn; // color set for 'stdwrn'
  779. //-----------------------------------------------------------------------------
  780. ColorMode_t GetFileColorized ( FILE *f );
  781. static inline bool IsFileColorized ( FILE *f )
  782. { return GetFileColorized(f) >= COLMD_ON; }
  783. enumError OpenStdLog
  784. (
  785. ccp fname, // If NULL or empty: don't open and use 'fallback'
  786. // If first char == '+': open file in append mode
  787. FILE *fallback // Fall back to this file, if no file name given
  788. // If NULL: just clode 'stdlog'.
  789. );
  790. void CloseStdLog(void);
  791. void SetupStdMsg(void);
  792. //-----------------------------------------------------------------------------
  793. // [[SavedStdFiles_t]]
  794. typedef struct SavedStdFiles_t
  795. {
  796. //-- 5 output streams
  797. FILE *std_out;
  798. FILE *std_err;
  799. FILE *std_log;
  800. FILE *std_msg;
  801. FILE *std_wrn;
  802. //-- 5 related color sets
  803. const ColorSet_t *col_out;
  804. const ColorSet_t *col_err;
  805. const ColorSet_t *col_log;
  806. const ColorSet_t *col_msg;
  807. const ColorSet_t *col_wrn;
  808. //--- misc
  809. uint stdout_seq_count;
  810. //--- support for CatchStdFiles()
  811. FILE *f; // NULL or result of open_memstream()
  812. char *data; // data pointer
  813. size_t size; // size of 'data'
  814. uint marker; // marker, free member, e.g. grow_buffer->used
  815. }
  816. SavedStdFiles_t;
  817. void SaveStdFiles ( SavedStdFiles_t *ssf );
  818. void RestoreStdFiles ( SavedStdFiles_t *ssf );
  819. void RedirectStdFiles
  820. (
  821. SavedStdFiles_t *ssf, // not NULL: save old output here
  822. FILE *f, // use this as out,err,log and msg, never NULL
  823. const ColorSet_t *colset,// new colset; if NULL: use GetFileColorSet(f)
  824. bool err_too // true: redirect stderr too
  825. );
  826. #ifndef __APPLE__
  827. enumError CatchStdFiles
  828. (
  829. SavedStdFiles_t *ssf, // save old output and data here, never NULL
  830. const ColorSet_t *colset // new colset; if NULL: no color support
  831. );
  832. #endif
  833. // call XFREE(ssf->data) after 'data' is used.
  834. void TermCatchStdFiles ( SavedStdFiles_t *ssf );
  835. //
  836. ///////////////////////////////////////////////////////////////////////////////
  837. /////////////// E N D ///////////////
  838. ///////////////////////////////////////////////////////////////////////////////
  839. #endif // DCLIB_COLOR_H