feat: some python packages

This commit is contained in:
2025-04-05 17:04:29 +00:00
parent 34c798b222
commit 686344e356
9 changed files with 351 additions and 63 deletions

View 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
View 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
View 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