LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: libinary

控制台下编程如何使用颜色

[复制链接]
发表于 2003-5-26 19:12:34 | 显示全部楼层
是不是和终端设置的参数有关。终端的类型是linux吗?
发表于 2003-5-26 19:19:54 | 显示全部楼层
不要骂我懒啊~~

  1. #include <stdio.h>

  2. int main()
  3. {
  4.   printf("\033[01;32mHello, World.\033[00m\n");
  5.   return 0;
  6. }
复制代码
发表于 2003-5-26 19:27:34 | 显示全部楼层
只有标准linux终端和X终端支持这些功能。需要设置TERM为linux。
http://www.linuxsir.net/bbs/show ... =%C3%FC%C1%EE%D0%D0
发表于 2003-5-26 19:53:25 | 显示全部楼层
hehe, i copyed it mainly from a linux programming book.
 楼主| 发表于 2003-5-26 20:24:23 | 显示全部楼层
在网上找到一个ansi.h,贴出来:
  1. // File : /include/ansi.h
  2. // 这是MUDLIB的标准颜色设置,由Jjgod汉化
  3. #define ESC "\033"
  4. #define CSI ESC"["
  5. /* 前景色 */
  6. #define BLK ESC"[30m" /* 黑色 */
  7. #define RED ESC"[31m" /* 红色 */
  8. #define GRN ESC"[32m" /* 绿色 */
  9. #define YEL ESC"[33m" /* 黄色 */
  10. #define BLU ESC"[34m" /* 蓝色 */
  11. #define MAG ESC"[35m" /* 紫色 */
  12. #define CYN ESC"[36m" /* 青色 */
  13. #define WHT ESC"[37m" /* 白色 */
  14. /* 加强前景色 */
  15. #define HIR ESC"[1;31m" /* 亮红 */
  16. #define HIG ESC"[1;32m" /* 亮绿 */
  17. #define HIY ESC"[1;33m" /* 亮黄 */
  18. #define HIB ESC"[1;34m" /* 亮蓝 */
  19. #define HIM ESC"[1;35m" /* 亮紫 */
  20. #define HIC ESC"[1;36m" /* 亮青 */
  21. #define HIW ESC"[1;37m" /* 亮白 */
  22. /* 加强背景色 */
  23. #define HBRED ESC"[41;1m" /* 亮红 */
  24. #define HBGRN ESC"[42;1m" /* 亮绿 */
  25. #define HBYEL ESC"[43;1m" /* 亮黄 */
  26. #define HBBLU ESC"[44;1m" /* 亮蓝 */
  27. #define HBMAG ESC"[45;1m" /* 亮紫 */
  28. #define HBCYN ESC"[46;1m" /* 亮青 */
  29. #define HBWHT ESC"[47;1m" /* 亮白 */
  30. /* 背景色 */
  31. #define BBLK ESC"[40m" /*黑色 */
  32. #define BRED ESC"[41m" /*红色 */
  33. #define BGRN ESC"[42m" /*绿色 */
  34. #define BYEL ESC"[43m" /* 黄色 */
  35. #define BBLU ESC"[44m" /*蓝色 */
  36. #define BMAG ESC"[45m" /*紫色 */
  37. #define BCYN ESC"[46m" /*青色 */
  38. // #define BWHT ESC"[47m" /* 白色 */
  39. #define NOR ESC"[2;37;0m" /* 返回原色 */
  40. /* 新增的Ansi颜色定义字符。由 Gothic april 23,1993 */
  41. /* 注意:这些操作符是为VT100终端设计的。 */
  42. /* 在MUD中,它们可能不一定全部都能正常使用。 */
  43. #define BOLD ESC"[1m" /* 打开粗体 */
  44. #define CLR ESC"[2J" /* 清屏 */
  45. #define HOME ESC"[H" /* 发送光标到原处 */
  46. #define REF CLRHOME /* 清屏和清除光标 */
  47. #define BIGTOP ESC"#3" /* Dbl height characters, top half */
  48. #define BIGBOT ESC"#4" /* Dbl height characters, bottem half */
  49. #define SAVEC ESC"[s" /* Save cursor position */
  50. #define REST ESC"[u" /* Restore cursor to saved position */
  51. //#define REVINDEX ESC"M" /* Scroll screen in opposite direction */
  52. #define SINGW ESC"#5" /* Normal, single-width characters */
  53. #define DBL ESC"#6" /* Creates double-width characters */
  54. #define FRTOP ESC"[2;25r" /* 冻结首行 */
  55. #define FRBOT ESC"[1;24r" /* 冻结底部一行 */
  56. #define UNFR ESC"[r" /* 首行和底部一行解冻 */
  57. #define BLINK ESC"[5m" /* 不断闪亮模式 */
  58. #define U ESC"[4m" /* 下划线模式 */
  59. #define REV ESC"[7m" /* 打开反白模式 */
  60. #define HIREV ESC"[1,7m" /* 亮色彩反白显示 */
  61. /* Binary Li 添加 */
  62. #define NOM ESC"[0m" /* 正常 */
复制代码
 楼主| 发表于 2003-5-26 20:29:49 | 显示全部楼层
写个程序验证一下:
  1. #include <stdio.h>
  2. #include "ansi.h"
  3. int
  4. main(void)
  5. {
  6.   char *fg[] = {BLK, RED, GRN, YEL, BLU, MAG, CYN, WHT,
  7.                 HIR, HIG, HIY, HIB, HIM, HIC, HIW};
  8.   char *bg[] = {HBRED, HBGRN, HBYEL, HBBLU, HBMAG, HBCYN, HBWHT,
  9.                 BBLK, BRED, BGRN, BYEL, BBLU, BMAG, BCYN};
  10.   const int nfg = 15, nbg = 14;
  11.   int i, j;
  12.   for(i = 0; i < nfg; i++){
  13.     printf(fg[i]);
  14.     for(j = 0; j < nbg; j++){
  15.       printf(bg[j]);
  16.       printf(" xxx ");
  17.     }
  18.     printf(NOR"\n");
  19.   }
  20.   printf(NOR);
  21.   exit(0);
  22. }
复制代码
那个运行后显示终端只支持一半的颜色(看看效果就知道了),上网搜了一下,说支持前景编号 (30-37) 和背景编号 (40-47),其他资料还没找到,我大概看了一下ls的代码,它的颜色显示也是用这种方法,大家可以看看printenv里显示的LS_COLORS环境变量。
发表于 2003-5-26 20:36:22 | 显示全部楼层
这是 ANSI 终端的标准吧?
发表于 2003-5-26 20:45:01 | 显示全部楼层
最初由 libinary 发表
我是说象ls --color=auto一样在控制台下显示颜色,curses要initscr,调用以后就清屏了,和ls之类的不一样,viper和无双版主说的应该可行,原来在DOS下就用的ansi.h的\033[之类的,不过我用了
echo \[\033[01;32m\]怎么不顶用,显示
[033[01
bash: 32m]: command not found
用echo "\[\033[01;32m\]"
echo -e \[\033[01;32m\]都不行
现在正在查。

的确,调用initscr之后会清除屏幕上的字符,但是调用endwin()之后,以前清除的东西又会回来的。
 楼主| 发表于 2003-5-27 14:27:06 | 显示全部楼层
大概测试了一下,把没有效果的去掉了:
  1. /*  ansi.h
  2. *  Binary Li根据Jjgod汉化的ansi.h编辑得来
  3. *  2003-5-27
  4. */
  5. #define ESC "\033"
  6. /* 前景色 */
  7. #define BLK ESC"[30m" /* 黑色 */
  8. #define RED ESC"[31m" /* 红色 */
  9. #define GRN ESC"[32m" /* 绿色 */
  10. #define YEL ESC"[33m" /* 黄色 */
  11. #define BLU ESC"[34m" /* 蓝色 */
  12. #define MAG ESC"[35m" /* 紫色 */
  13. #define CYN ESC"[36m" /* 青色 */
  14. #define WHT ESC"[37m" /* 白色 */
  15. /* 背景色 */
  16. #define BBLK ESC"[40m" /* 黑色 */
  17. #define BRED ESC"[41m" /* 红色 */
  18. #define BGRN ESC"[42m" /* 绿色 */
  19. #define BYEL ESC"[43m" /* 黄色 */
  20. #define BBLU ESC"[44m" /* 蓝色 */
  21. #define BMAG ESC"[45m" /* 紫色 */
  22. #define BCYN ESC"[46m" /* 青色 */
  23. #define BWHT ESC"[47m" /* 白色 */
  24. /* 特殊控制 */
  25. #define BOLD ESC"[1m" /* 打开粗体 */
  26. #define CLR ESC"[2J" /* 清屏 */
  27. #define HOME ESC"[H" /* 发送光标到原处 */
  28. #define REF CLR HOME /* 清屏和清除光标 */
  29. #define REVINDEX ESC"M" /* 向回滚动一行 */
  30. #define FRTOP ESC"[2;25r" /* 冻结首行 */      /*                   */
  31. #define FRBOT ESC"[1;24r" /* 冻结底部一行 */  /* 测试"底部"为25行   */
  32. #define UNFR ESC"[r" /* 首行和底部一行解冻 */ /*                   */
  33. #define U ESC"[4m" /* 下划线模式 */
  34. #define REV ESC"[7m" /* 打开反白模式 */
  35. #define HIREV ESC"[1,7m" /* 亮色彩反白显示 */
  36. #define NOM ESC"[0m" /* 正常 */
复制代码
 楼主| 发表于 2003-5-27 14:31:48 | 显示全部楼层
看了一下www.vt100.net,控制串还真多,而且不是每个都管用的,每一个都测试一遍够可怕的。
curses库真是厉害,把那么多终端类型都处理好了,确实是好。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表