/[ascend]/trunk/ascend/general/color.c
ViewVC logotype

Annotation of /trunk/ascend/general/color.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2784 - (hide annotations) (download) (as text)
Mon Jun 23 03:18:45 2014 UTC (6 years, 3 months ago) by jpye
File MIME type: text/x-csrc
File size: 3146 byte(s)
Fix color detection for TERM='xterm-256color' on Fedora 20
1 jpye 2530 /* ASCEND modelling environment
2     Copyright (C) 2012 John Pye
3    
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2, or (at your option)
7     any later version.
8    
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13    
14 jpye 2784 You should have received a copy of the GNU General Public License
15 jpye 2648 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 jpye 2530 *//* @file
17     Platform-independent console colour output.
18     */
19    
20     #include "color.h"
21     #include "config.h"
22    
23     #ifdef __WIN32__
24     # include <windows.h>
25     # include <conio.h>
26     #endif
27    
28     #ifdef ASC_XTERM_COLORS
29     static int color_test(){
30 jpye 2558 /* 1: use xterm; 2: use windows; -1: no color */
31 jpye 2530 static int use_xterm_color = 0;
32     char *term;
33     if(!use_xterm_color){
34     term = getenv("TERM");
35    
36     # ifdef __WIN32__
37     if(term!=NULL){
38     if(strcmp(term,"cygwin")==0){
39     Asc_FPrintf(stderr,"\n\n\nUsing Windows color codes\n\n\n");
40 jpye 2558 use_xterm_color=2;
41     }else if(strcmp(term,"xterm")==0){
42     Asc_FPrintf(stderr,"\n\n\nUsing xterm color codes\n\n\n");
43 jpye 2530 use_xterm_color=1;
44     }
45     }
46     # else
47     if(term!=NULL){
48 jpye 2784 if(strcmp(term,"xterm")==0 || strcmp(term,"xterm-256color")==0){
49 jpye 2530 /* MSYS (rxvt), putty, xterm. */
50     use_xterm_color=1;
51     }else{
52     /* unrecognised $TERM */
53     use_xterm_color=-1;
54     }
55     }else{
56     /* no $TERM var */
57     use_xterm_color=-1;
58     }
59     # endif
60     }
61     return use_xterm_color;
62     }
63    
64    
65     /* ANSI color codes from integer bitfield input... */
66    
67     int color_on(FILE *f, int colorcode){
68     int use_color = color_test();
69 jpye 2558 int fg, bg, bold;
70     # ifdef __WIN32__
71     HANDLE Hnd;
72     # endif
73 jpye 2530
74 jpye 2558 switch(use_color){
75     case 1:
76     fg = colorcode & (ASC_FG_GREY);
77     bg = (colorcode & (ASC_BG_GREY)) >> 4;
78     bold = colorcode & ASC_FG_BRIGHT;
79     # ifdef __WIN32__
80     /* on this platform, we have to switch red and blue bits for both fg and bg */
81     fg = fg - (fg & 5) + ((1 & fg)<<2) + ((fg & 4)>>2);
82     bg = bg - (bg & 5) + ((1 & bg)<<2) + ((bg & 4)>>2);
83     # endif
84 jpye 2531 fprintf(f, "\033[3%d%sm",fg,(bold?";1":""));
85     if(bg){
86     fprintf(f, "\0334%dm",bg);
87     }
88 jpye 2558 break;
89     # ifdef __WIN32__
90     case 2:
91     Hnd = GetStdHandle(STD_OUTPUT_HANDLE);
92     if(Hnd == INVALID_HANDLE_VALUE)return 0;
93     SetConsoleTextAttribute(Hnd, (WORD)colorcode);
94     break;
95     #endif
96 jpye 2530 }
97     return 0;
98     }
99    
100     int color_off(FILE *f){
101     int use_color = color_test();
102 jpye 2558 # ifdef __WIN32__
103     HANDLE Hnd;
104     # endif
105 jpye 2530
106 jpye 2558 switch(use_color){
107     case 1:
108 jpye 2530 fprintf(f, "\033[0m");
109 jpye 2558 break;
110     # ifdef __WIN32__
111     case 2:
112     Hnd = GetStdHandle(STD_OUTPUT_HANDLE);
113     if(Hnd == INVALID_HANDLE_VALUE)return 0;
114     SetConsoleTextAttribute(Hnd, (WORD)ASC_FG_GREY);
115     break;
116     # endif
117 jpye 2530 }
118     return 0;
119     }
120    
121     #else
122     /* if ASC_XTERM_COLORS is turned off, we're not using any colors ever */
123     int color_on(FILE *f, int colorcode){return 0;}
124     int color_off(FILE *f){return 0;}
125     #endif /*ASC_XTERM_COLORS*/

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22