feat: some python packages
This commit is contained in:
@@ -23,14 +23,14 @@ const char* log_level_to_string(LogLevel level) {
|
||||
|
||||
const char* log_level_to_color(LogLevel level) {
|
||||
switch (level) {
|
||||
case LOG_LEVEL_TRACE: return COLOR_GREEN;
|
||||
case LOG_LEVEL_DEBUG: return COLOR_BLUE;
|
||||
case LOG_LEVEL_LOG: return COLOR_CYAN;
|
||||
case LOG_LEVEL_INFO: return COLOR_GREEN;
|
||||
case LOG_LEVEL_NOTICE: return COLOR_CYAN;
|
||||
case LOG_LEVEL_WARN: return COLOR_YELLOW;
|
||||
case LOG_LEVEL_EXCEPTION: return COLOR_RED;
|
||||
default: return COLOR_RESET;
|
||||
case LOG_LEVEL_TRACE: return OPTIONAL_COLOR(COLOR_GREEN);
|
||||
case LOG_LEVEL_DEBUG: return OPTIONAL_COLOR(COLOR_BLUE);
|
||||
case LOG_LEVEL_LOG: return OPTIONAL_COLOR(COLOR_CYAN);
|
||||
case LOG_LEVEL_INFO: return OPTIONAL_COLOR(COLOR_GREEN);
|
||||
case LOG_LEVEL_NOTICE: return OPTIONAL_COLOR(COLOR_CYAN);
|
||||
case LOG_LEVEL_WARN: return OPTIONAL_COLOR(COLOR_YELLOW);
|
||||
case LOG_LEVEL_EXCEPTION: return OPTIONAL_COLOR(COLOR_RED);
|
||||
default: return OPTIONAL_COLOR(COLOR_RESET);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ char* raise_message(
|
||||
static char timeStr[20];
|
||||
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &tm_info);
|
||||
|
||||
fprintf(stderr, "%s %s%s%s %s:%d ", timeStr, log_level_to_color(level), log_level_to_string(level), COLOR_RESET, file, line);
|
||||
fprintf(stderr, "%s %s%s%s %s:%d ", timeStr, log_level_to_color(level), log_level_to_string(level), OPTIONAL_COLOR(COLOR_RESET), file, line);
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
|
||||
@@ -74,12 +74,14 @@ void hmpl_render_section_tags(Arena *arena, char **text_ptr, Json *context, cons
|
||||
char start_pattern[32];
|
||||
snprintf(start_pattern, sizeof(start_pattern), "{{%s", prefix_start);
|
||||
Slice start_slice = slice_create(char, start_pattern, strlen(start_pattern), 0, strlen(start_pattern));
|
||||
raise_trace("start_slice: %s", start_slice.data);
|
||||
|
||||
// Create a mutable copy of separator_pattern
|
||||
char separator_copy[32];
|
||||
strncpy(separator_copy, separator_pattern, sizeof(separator_copy) - 1);
|
||||
separator_copy[sizeof(separator_copy) - 1] = '\0';
|
||||
Slice separator_slice = slice_create(char, separator_copy, strlen(separator_copy), 0, strlen(separator_copy));
|
||||
raise_trace("separator_slice: %s", separator_slice.data);
|
||||
if (separator_slice.len == 0) {
|
||||
raise_exception("Unexpected usage: separator pattern cannot be empty");
|
||||
}
|
||||
@@ -134,6 +136,7 @@ void hmpl_render_section_tags(Arena *arena, char **text_ptr, Json *context, cons
|
||||
char *close_tag_pattern = arena_alloc(arena, start_slice.len + key_length + 3); // +3 for "{{" and "}}"
|
||||
snprintf(close_tag_pattern, start_slice.len + key_length + 3,
|
||||
"{{%s%s}}", prefix_end, key);
|
||||
raise_trace("close_tag_pattern: %s", close_tag_pattern);
|
||||
|
||||
// Find closing tag
|
||||
size_t after_opening_end = (opening_tag_end - (char*)after_separator.data) + 2;
|
||||
|
||||
@@ -107,7 +107,7 @@ case "$MODE" in
|
||||
# shellcheck disable=SC2086
|
||||
cc $CFLAGS $OPTFLAGS -pedantic -I. "$test_file" -Ltarget -lhmpl $LDFLAGS -o "$exe"
|
||||
if [ "$RUN_TESTS" -eq 1 ]; then
|
||||
"$exe"
|
||||
LOG_LEVEL=TRACE ./"$exe"
|
||||
fi
|
||||
done
|
||||
;;
|
||||
|
||||
30
package/c/prettify/default.nix
Normal file
30
package/c/prettify/default.nix
Normal file
@@ -0,0 +1,30 @@
|
||||
{ stdenv, gcc, lib, bash, inotify-tools }:
|
||||
|
||||
stdenv.mkDerivation {
|
||||
pname = "prettify";
|
||||
version = "1.0";
|
||||
src = ./.;
|
||||
doCheck = false;
|
||||
|
||||
nativeBuildInputs = [ gcc ];
|
||||
|
||||
buildPhase = ''
|
||||
ls
|
||||
${bash}/bin/sh ./make.sh build
|
||||
'';
|
||||
|
||||
checkPhase = ''
|
||||
${bash}/bin/sh ./make.sh check
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/lib $out/include
|
||||
cp target/libhectic.a $out/lib/
|
||||
cp hectic.h $out/include/
|
||||
'';
|
||||
|
||||
meta = {
|
||||
description = "prettify";
|
||||
license = lib.licenses.mit;
|
||||
};
|
||||
}
|
||||
95
package/c/prettify/main.c
Executable file
95
package/c/prettify/main.c
Executable file
@@ -0,0 +1,95 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <hectic.h>
|
||||
#include <strings.h>
|
||||
#include <stdlib.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#define MAX_LINE 1024
|
||||
|
||||
typedef struct {
|
||||
const char *keyword;
|
||||
const char *color;
|
||||
} KeywordColor;
|
||||
|
||||
static KeywordColor keyword_colors[] = {
|
||||
{"LOG", COLOR_GREEN},
|
||||
{"DEBUG", COLOR_BLUE},
|
||||
{"ERROR", COLOR_RED},
|
||||
{"INFO", COLOR_GREEN},
|
||||
{"WARNING", COLOR_YELLOW},
|
||||
{"NOTICE", COLOR_CYAN},
|
||||
{"HINT", COLOR_MAGENTA},
|
||||
{"DETAIL", COLOR_CYAN},
|
||||
{"STATEMENT", COLOR_CYAN},
|
||||
{"EXCEPTION", COLOR_MAGENTA},
|
||||
{"FATAL", COLOR_MAGENTA},
|
||||
};
|
||||
|
||||
void print_usage(const char *prog_name) {
|
||||
fprintf(stderr, "Usage: %s [options]\n", prog_name);
|
||||
fprintf(stderr, "Options:\n");
|
||||
fprintf(stderr, " -i, --ignore-case Ignore case when matching keywords (default)\n");
|
||||
fprintf(stderr, " -h, --help Display this help message\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Default to case-insensitive matching
|
||||
int ignore_case = 0;
|
||||
|
||||
// Define long options
|
||||
static struct option long_options[] = {
|
||||
{"ignore-case", no_argument, NULL, 'i'},
|
||||
{"help", no_argument, NULL, 'h'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
int opt;
|
||||
while ((opt = getopt_long(argc, argv, "ih", long_options, NULL)) != -1) {
|
||||
switch (opt) {
|
||||
case 'i':
|
||||
ignore_case = 1;
|
||||
break;
|
||||
case 'h':
|
||||
default:
|
||||
print_usage(argv[0]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
char line[MAX_LINE];
|
||||
while (fgets(line, sizeof(line), stdin)) {
|
||||
line[strcspn(line, "\n")] = 0;
|
||||
char *space = strchr(line, ' ');
|
||||
char token[MAX_LINE];
|
||||
if (space) {
|
||||
int len = space - line;
|
||||
strncpy(token, line, len);
|
||||
token[len] = '\0';
|
||||
} else {
|
||||
strcpy(token, line);
|
||||
}
|
||||
const char *color = "";
|
||||
int count = sizeof(keyword_colors) / sizeof(keyword_colors[0]);
|
||||
for (int i = 0; i < count; i++) {
|
||||
// Use either case-sensitive or case-insensitive comparison based on the option
|
||||
int match = ignore_case ?
|
||||
strcasecmp(token, keyword_colors[i].keyword) == 0 :
|
||||
strcmp(token, keyword_colors[i].keyword) == 0;
|
||||
|
||||
if (match) {
|
||||
color = keyword_colors[i].color;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (color[0] != '\0')
|
||||
printf("%s%s%s", color, token, COLOR_RESET);
|
||||
else
|
||||
printf("%s", token);
|
||||
if (space)
|
||||
printf("%s", space);
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
105
package/c/prettify/make.sh
Normal file
105
package/c/prettify/make.sh
Normal file
@@ -0,0 +1,105 @@
|
||||
#!/bin/sh
|
||||
# Usage: make.sh [build|check] [--norun] [--debug] [--color]
|
||||
# Options:
|
||||
# build Build the library and app (default if no mode is provided).
|
||||
# watch Build the library and app and watch for changes.
|
||||
# run Build and run the app.
|
||||
# check Build tests; runs them unless --norun is specified.
|
||||
# --norun (check only) Build tests but do not run them.
|
||||
# --debug Build with -O0 (debug mode).
|
||||
# --color Pass -fdiagnostics-color=always to compiler.
|
||||
# help, --help Show this help message.
|
||||
|
||||
check_dependencies() {
|
||||
for dep in cc ar pager; do
|
||||
if ! command -v "$dep" >/dev/null 2>&1; then
|
||||
echo "Error: Required dependency '$dep' not found." >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
check_dependencies
|
||||
|
||||
print_help() {
|
||||
cat <<EOF
|
||||
Usage: $0 [build|check] [--norun] [--debug] [--color]
|
||||
build Build the library and app (default).
|
||||
watch Build the library and app and watch for changes.
|
||||
run Build and run the app.
|
||||
check Build tests; runs them unless --norun is specified.
|
||||
--norun (check only) Build tests but do not run them.
|
||||
--debug Build with debug flags (-O0).
|
||||
--color Force colored compiler diagnostics.
|
||||
help, --help Display this help message.
|
||||
EOF
|
||||
}
|
||||
|
||||
# Show help if requested
|
||||
case "$1" in
|
||||
help|--help)
|
||||
print_help
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
|
||||
# Default flags
|
||||
RUN_TESTS=1
|
||||
OPTFLAGS="-O2"
|
||||
CFLAGS="-Wall -Wextra -Werror -pedantic -fsanitize=address"
|
||||
LDFLAGS="-lhectic"
|
||||
STD_FLAGS="-std=c99"
|
||||
COLOR_FLAG=""
|
||||
|
||||
MODE="${1:-build}"
|
||||
shift
|
||||
|
||||
# Process options
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--norun)
|
||||
RUN_TESTS=0
|
||||
;;
|
||||
--debug)
|
||||
OPTFLAGS="-O0"
|
||||
;;
|
||||
--color)
|
||||
COLOR_FLAG="-fdiagnostics-color=always"
|
||||
;;
|
||||
*)
|
||||
echo "Unknown option: $1"
|
||||
print_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
if [ -n "$COLOR_FLAG" ]; then
|
||||
CFLAGS="$CFLAGS $COLOR_FLAG"
|
||||
fi
|
||||
|
||||
build() {
|
||||
mkdir -p target
|
||||
echo "# Build app"
|
||||
# shellcheck disable=SC2086
|
||||
cc $CFLAGS $OPTFLAGS main.c $LDFLAGS -lhectic -o target/prettify
|
||||
}
|
||||
|
||||
case "$MODE" in
|
||||
watch)
|
||||
find . -type d | nix run .#watch -- 'sh ./make.sh build && sh ./make.sh check' -i -p '*.c' -p '*.h' 2>&1
|
||||
;;
|
||||
build)
|
||||
build
|
||||
;;
|
||||
run)
|
||||
build && ./target/prettify
|
||||
;;
|
||||
check)
|
||||
echo "No tests to run"
|
||||
;;
|
||||
*)
|
||||
print_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
@@ -21,8 +21,9 @@
|
||||
#define MAX_PATTERNS 32
|
||||
#define POLL_INTERVAL_MS 100
|
||||
|
||||
// Global flag to indicate if we're running in pager mode
|
||||
// Global flags
|
||||
int pager_mode = 0;
|
||||
int ignore_errors = 0;
|
||||
FILE *output_stream = NULL;
|
||||
int running = 1;
|
||||
|
||||
@@ -37,11 +38,13 @@ void print_usage(const char *prog_name) {
|
||||
fprintf(stderr, "Options:\n");
|
||||
fprintf(stderr, " -p <pattern> File pattern to watch (can be used multiple times)\n");
|
||||
fprintf(stderr, " -P Enable pager-friendly output (refresh mode)\n");
|
||||
fprintf(stderr, " -i, --ignore-errors Continue watching even when command returns non-zero\n");
|
||||
fprintf(stderr, " -h Show this help message\n");
|
||||
fprintf(stderr, "Examples:\n");
|
||||
fprintf(stderr, " %s 'make' -p '*.c' -p '*.h' ./src\n", prog_name);
|
||||
fprintf(stderr, " find . -type d | %s 'echo changed' -p '*.py'\n", prog_name);
|
||||
fprintf(stderr, " %s -P 'make' -p '*.c' -p '*.h' ./src | less\n", prog_name);
|
||||
fprintf(stderr, " %s -i 'sh ./make.sh check' -p '*.c' -p '*.h' .\n", prog_name);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -174,10 +177,16 @@ int main(int argc, char *argv[]) {
|
||||
char **patterns = malloc(MAX_PATTERNS * sizeof(char*));
|
||||
int num_patterns = 0;
|
||||
|
||||
// Define long options
|
||||
static struct option long_options[] = {
|
||||
{"ignore-errors", no_argument, NULL, 'i'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
optind = 2;
|
||||
int opt;
|
||||
|
||||
while ((opt = getopt(argc, argv, "p:Ph")) != -1) {
|
||||
while ((opt = getopt_long(argc, argv, "p:Phi", long_options, NULL)) != -1) {
|
||||
switch (opt) {
|
||||
case 'p':
|
||||
if (num_patterns < MAX_PATTERNS) {
|
||||
@@ -190,6 +199,9 @@ int main(int argc, char *argv[]) {
|
||||
case 'P':
|
||||
pager_mode = 1;
|
||||
break;
|
||||
case 'i':
|
||||
ignore_errors = 1;
|
||||
break;
|
||||
case 'h':
|
||||
print_usage(argv[0]);
|
||||
break;
|
||||
@@ -439,8 +451,15 @@ int main(int argc, char *argv[]) {
|
||||
if (any_changes) {
|
||||
int res = system(command);
|
||||
if (res != 0) {
|
||||
perror("system");
|
||||
exit(EXIT_FAILURE);
|
||||
time_t current_time = time(NULL);
|
||||
fprintf(output_stream, "Command failed at %s", ctime(¤t_time));
|
||||
fprintf(output_stream, "Command: %s (exit code: %d)\n", command, res);
|
||||
fflush(output_stream);
|
||||
|
||||
if (!ignore_errors) {
|
||||
fprintf(output_stream, "Exiting due to command failure. Use -i to ignore errors.\n");
|
||||
running = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,15 +107,7 @@ case "$MODE" in
|
||||
fi
|
||||
;;
|
||||
check)
|
||||
mkdir -p target/test
|
||||
for test_file in test/*.c; do
|
||||
exe="target/test/$(basename "${test_file%.c}")"
|
||||
# shellcheck disable=SC2086
|
||||
cc $CFLAGS $OPTFLAGS -pedantic "$test_file" $LDFLAGS -o "$exe"
|
||||
if [ "$RUN_TESTS" -eq 1 ]; then
|
||||
"$exe"
|
||||
fi
|
||||
done
|
||||
echo "No tests to run"
|
||||
;;
|
||||
*)
|
||||
print_help
|
||||
|
||||
Reference in New Issue
Block a user