@@ -80,7 +80,7 @@ static bool thread_pool_submit_batch(thread_pool_t *pool, void *(*func)(void *),
8080#define LARGE_FILE_THRESHOLD (64 * 1024 * 1024) // 64MB threshold for advanced optimizations
8181#define SINGLE_THREAD_FILE_SIZE_THRESHOLD MIN_CHUNK_SIZE
8282#define ADAPTIVE_THREAD_FILE_SIZE_THRESHOLD 0
83- #define VERSION "2.1 .0"
83+ #define VERSION "2.2 .0"
8484#ifndef PATH_MAX
8585#define PATH_MAX 4096
8686#endif
@@ -501,11 +501,13 @@ size_t print_matching_items(const char *filename, const char *text, size_t text_
501501 const char * color_filename = KREP_COLOR_FILENAME ;
502502 const char * color_reset = KREP_COLOR_RESET ;
503503 const char * color_separator = KREP_COLOR_SEPARATOR ;
504+ const char * color_line_number = KREP_COLOR_LINE_NUMBER ;
504505 const char * color_text = KREP_COLOR_TEXT ;
505506 const char * color_match = KREP_COLOR_MATCH ;
506507
507508 // Precompute lengths to avoid repeated strlen calls
508509 size_t len_color_reset = color_output_enabled ? strlen (color_reset ) : 0 ;
510+ size_t len_color_line_number = color_output_enabled ? strlen (color_line_number ) : 0 ;
509511 size_t len_color_text = color_output_enabled ? strlen (color_text ) : 0 ;
510512 size_t len_color_match = color_output_enabled ? strlen (color_match ) : 0 ;
511513
@@ -688,7 +690,7 @@ size_t print_matching_items(const char *filename, const char *text, size_t text_
688690 size_t required_estimate = filename_prefix_len + lineno_len + len + 1 ; // +1 for newline
689691 if (color_output_enabled )
690692 {
691- required_estimate += len_color_match + len_color_reset ;
693+ required_estimate += len_color_line_number + len_color_match + ( len_color_reset * 2 ) ;
692694 }
693695
694696 // Flush the batch buffer to stdout if the new entry won't fit (use estimate)
@@ -714,7 +716,15 @@ size_t print_matching_items(const char *filename, const char *text, size_t text_
714716 }
715717
716718 // 2. Copy line number
719+ if (color_output_enabled )
720+ {
721+ safe_append_to_batch (& current_write_ptr , batch_buffer_end , & o_batch_pos , O_BATCH_BUFFER_SIZE , color_line_number , len_color_line_number );
722+ }
717723 safe_append_to_batch (& current_write_ptr , batch_buffer_end , & o_batch_pos , O_BATCH_BUFFER_SIZE , lineno_buffer , lineno_len );
724+ if (color_output_enabled )
725+ {
726+ safe_append_to_batch (& current_write_ptr , batch_buffer_end , & o_batch_pos , O_BATCH_BUFFER_SIZE , color_reset , len_color_reset );
727+ }
718728
719729 // 3. Start color for match (if enabled)
720730 if (color_output_enabled )
@@ -1126,46 +1136,62 @@ double get_time(void)
11261136// Print usage information
11271137void print_usage (const char * program_name )
11281138{
1129- printf ("krep v%s - A high-performance string search utility\n\n" , VERSION );
1130- printf ("Usage: %s [OPTIONS] PATTERN [FILE | DIRECTORY]\n" , program_name );
1131- printf (" or: %s [OPTIONS] -e PATTERN [-e PATTERN...] [FILE | DIRECTORY]\n" , program_name );
1132- printf (" or: %s [OPTIONS] -f FILE [FILE | DIRECTORY]\n" , program_name );
1133- printf (" or: %s [OPTIONS] -s PATTERN STRING_TO_SEARCH\n" , program_name );
1134- printf (" or: %s [OPTIONS] PATTERN < FILE\n" , program_name );
1135- printf (" or: cat FILE | %s [OPTIONS] PATTERN\n\n" , program_name );
1136- printf ("OPTIONS:\n" );
1137- printf (" -i Perform case-insensitive matching.\n" );
1138- printf (" -c Count matching lines. Only a count of lines is printed.\n" );
1139- printf (" -o Only matching. Print only the matched parts of lines, one per line.\n" );
1140- printf (" -e PATTERN Specify pattern. Can be used multiple times (treated as OR for literal, combined for regex).\n" );
1141- printf (" -f FILE Read patterns from FILE, one per line. Use '-' for stdin.\n" );
1142- printf (" -E Interpret PATTERN(s) as POSIX Extended Regular Expression(s).\n" );
1143- printf (" If multiple -e used with -E, they are combined with '|'.\n" );
1144- printf (" -F Interpret PATTERN(s) as fixed strings (literal). Default if not -E.\n" );
1145- printf (" -r Search directories recursively. Skips binary files and common dirs.\n" );
1146- printf (" --gitignore Respect .gitignore files when searching recursively (-r).\n" );
1147- printf (" --algo=ALGO Force search algorithm: 'auto' (default), 'bm', 'kmp'.\n" );
1148- printf (" -t NUM Use NUM threads for file search (default: auto-detect cores).\n" );
1149- printf (" -s Search in STRING_TO_SEARCH instead of FILE or DIRECTORY.\n" );
1150- printf (" --color[=WHEN] Control color output ('always', 'never', 'auto'). Default: 'auto'.\n" );
1151- printf (" --no-simd Explicitly disable SIMD acceleration.\n" );
1152- printf (" -v Show version information and exit.\n" );
1153- printf (" -h, --help Show this help message and exit.\n" );
1154- printf (" -m NUM Stop reading a file after NUM matching lines.\n" );
1155- printf (" -w Select only matches that form whole words.\n\n" );
1156- printf ("EXIT STATUS:\n" );
1157- printf (" 0 if matches were found,\n" );
1158- printf (" 1 if no matches were found,\n" );
1159- printf (" 2 if an error occurred.\n\n" );
1160- printf ("EXAMPLES:\n" );
1139+ bool style = isatty (STDOUT_FILENO );
1140+ const char * title = style ? KREP_COLOR_HELP_TITLE : "" ;
1141+ const char * section = style ? KREP_COLOR_HELP_SECTION : "" ;
1142+ const char * option = style ? KREP_COLOR_HELP_OPTION : "" ;
1143+ const char * muted = style ? KREP_COLOR_HELP_MUTED : "" ;
1144+ const char * reset = style ? KREP_COLOR_RESET : "" ;
1145+
1146+ printf ("%skrep v%s%s\n" , title , VERSION , reset );
1147+ printf ("%sFast search with polished terminal output.%s\n\n" , muted , reset );
1148+
1149+ printf ("%sUsage%s\n" , section , reset );
1150+ printf (" %s [OPTIONS] PATTERN [FILE | DIRECTORY]\n" , program_name );
1151+ printf (" %s [OPTIONS] -e PATTERN [-e PATTERN...] [FILE | DIRECTORY]\n" , program_name );
1152+ printf (" %s [OPTIONS] -f FILE [FILE | DIRECTORY]\n" , program_name );
1153+ printf (" %s [OPTIONS] -s PATTERN STRING_TO_SEARCH\n" , program_name );
1154+ printf (" %s [OPTIONS] PATTERN < FILE\n" , program_name );
1155+ printf (" cat FILE | %s [OPTIONS] PATTERN\n\n" , program_name );
1156+
1157+ printf ("%sSearch%s\n" , section , reset );
1158+ printf (" %s-i%s Perform case-insensitive matching.\n" , option , reset );
1159+ printf (" %s-e PATTERN%s Specify pattern. Reusable for multiple patterns.\n" , option , reset );
1160+ printf (" %s-f FILE%s Read patterns from FILE (use '-' for stdin).\n" , option , reset );
1161+ printf (" %s-E%s Use POSIX Extended Regular Expressions.\n" , option , reset );
1162+ printf (" %s-F%s Use fixed strings (default unless -E).\n" , option , reset );
1163+ printf (" %s-w%s Match whole words only.\n\n" , option , reset );
1164+
1165+ printf ("%sScope & Performance%s\n" , section , reset );
1166+ printf (" %s-r%s Search directories recursively.\n" , option , reset );
1167+ printf (" %s--gitignore%s Respect .gitignore when used with -r.\n" , option , reset );
1168+ printf (" %s--algo=ALGO%s Force algorithm: auto (default), bm, kmp.\n" , option , reset );
1169+ printf (" %s-t NUM%s Set thread count (default: auto).\n" , option , reset );
1170+ printf (" %s--no-simd%s Disable SIMD acceleration.\n\n" , option , reset );
1171+
1172+ printf ("%sOutput & UX%s\n" , section , reset );
1173+ printf (" %s-o%s Print only matching parts, one per line.\n" , option , reset );
1174+ printf (" %s-c%s Print only match counts.\n" , option , reset );
1175+ printf (" %s-m NUM%s Stop after NUM matching lines per file.\n" , option , reset );
1176+ printf (" %s-s%s Search in STRING_TO_SEARCH.\n" , option , reset );
1177+ printf (" %s--color[=WHEN]%s Color mode: always, never, auto (default).\n" , option , reset );
1178+ printf (" %s-v%s Show version information.\n" , option , reset );
1179+ printf (" %s-h, --help%s Show this help page.\n\n" , option , reset );
1180+
1181+ printf ("%sExit Status%s\n" , section , reset );
1182+ printf (" 0 match found\n" );
1183+ printf (" 1 no match found\n" );
1184+ printf (" 2 error\n\n" );
1185+
1186+ printf ("%sExamples%s\n" , section , reset );
11611187 printf (" %s \"search term\" input.log\n" , program_name );
11621188 printf (" %s -i -c ERROR large_log.txt\n" , program_name );
11631189 printf (" %s -t 8 -o '[0-9]+' data.log | sort | uniq -c\n" , program_name );
11641190 printf (" %s -E \"^[Ee]rror: .*failed\" system.log\n" , program_name );
11651191 printf (" %s -r \"MyClass\" /path/to/project\n" , program_name );
11661192 printf (" %s -r --gitignore \"TODO\" /path/to/project\n" , program_name );
1167- printf (" %s -e Error -e Warning app.log\n" , program_name ); // Find lines with Error OR Warning
1168- printf (" echo 'pattern' | %s -f - target.txt\n" , program_name ); // Read pattern from stdin
1193+ printf (" %s -e Error -e Warning app.log\n" , program_name );
1194+ printf (" echo 'pattern' | %s -f - target.txt\n" , program_name );
11691195}
11701196
11711197// Helper for case-insensitive comparison using the lookup table
0 commit comments