@@ -81,11 +81,6 @@ const char * const kColors[] = {
8181
8282const int kColorCount = sizeof (kColors ) / sizeof (kColors [0 ]);
8383
84- static void resetColor (void )
85- {
86- fputs ("\033[0m" , stdout ); /* is this correct? */
87- }
88-
8984/* 32-bit fnv1, not 1a */
9085static unsigned fnv (const char * str )
9186{
@@ -100,12 +95,28 @@ static unsigned fnv(const char * str)
10095 return ret ;
10196}
10297
98+ #define COLOR_RESET_STRING "\033[0m"
99+
103100static void printColoredByHash (const char * str )
104101{
102+ char buff [1024 ];
105103 const int idx = fnv (str ) % kColorCount ;
106- fputs (kColors [idx ], stdout );
107- fputs (str , stdout ); /* not puts to not get a newline */
108- resetColor ();
104+
105+ if (strlen (str ) > 1000 )
106+ {
107+ /* too long, print 3 times */
108+ fputs (kColors [idx ], stdout );
109+ fputs (str , stdout ); /* not puts to not get a newline */
110+ fputs (COLOR_RESET_STRING , stdout );
111+ }
112+ else
113+ {
114+ /* prepare the word and color and reset, then print it out */
115+ strcpy (buff , kColors [idx ]);
116+ strcat (buff , str );
117+ strcat (buff , COLOR_RESET_STRING );
118+ fputs (buff , stdout );
119+ }
109120}
110121
111122static int mygetline (char * buff , int len , int * toomuch )
@@ -198,7 +209,7 @@ static int printhelp(const char * argv0)
198209 puts (c + 1 ); /* skip the ESC char to not interpret this as control sequence */
199210 } /* for each color */
200211
201- resetColor ( );
212+ fputs ( COLOR_RESET_STRING , stdout );
202213 return 0 ;
203214}
204215
0 commit comments