feat: add detailed review content with user feedback and rating

The review now includes a full text body, a numeric rating field, and a timestamp for when the review was submitted. This enables richer display and sorting by date on the product page.
This commit is contained in:
2025-01-04 07:44:34 +00:00
parent 870d32c898
commit 0c14608257
19 changed files with 230 additions and 254 deletions
+5 -5
View File
@@ -1,11 +1,11 @@
{
"extension": ".h",
"source": "#include <readline/readline.h>\n#include <readline/history.h>\n\n#define HISTORY_FILE \"~/.calpaca_history\"\n\nbool line_initialized =false;\n\nchar *line_command_generator(const char *text, int state) {\n static int list_index, len;\n const char *commands[] = {\"help\", \"exit\", \"list\", \"review\",\"refactor\",\"opfuscate\", NULL};\n\n if (!state) {\n list_index = 0;\n len = strlen(text);\n }\n\n while (commands[list_index]) {\n const char *command = commands[list_index++];\n if (strncmp(command, text, len) == 0) {\n return strdup(command); \n }\n }\n\n return NULL; \n}\n\nchar **line_command_completion(const char *text, int start, int end) {\n rl_attempted_completion_over = 1;\n return rl_completion_matches(text, line_command_generator);\n}\n\nvoid line_init(){\n if(!line_initialized)\n { \n rl_attempted_completion_function = line_command_completion;\n line_initialized = true;\n read_history(HISTORY_FILE);\n }\n \n}\n\nchar * line_read(char * prefix) {\n char * data = readline(prefix);\n if(!(data || *data)){\n return NULL;\n }\n return data;\n}\nvoid line_add_history(char * data){\n read_history(HISTORY_FILE);\n add_history(data);\n write_history(HISTORY_FILE);\n}",
"review": "7\n\n## Bugs\n- The history file path is not expanded from `~` to the full home directory path.\n- `read_history` and `write_history` may encounter errors without handling them, e.g., file permissions or missing history file.\n- The check on `if(!(data || *data)){` in `line_read()` might lead to a potential null pointer dereference if `readline` returns null.\n\n## Optimizations\n- Use `getenv(\"HOME\")` or `wordexp` to properly expand the tilde (`~`) to the full path of the user's home directory.\n- Add error handling for `read_history` and `write_history` calls.\n- Use `if(data == NULL || *data == '\\0'){` in `line_read()` to handle potential null addresses safely.\n\n## Good points\n- Implements command completion which enhances user experience.\n- Utilizes the `readline` library effectively for command-line interaction.\n- Code is modular and functions are appropriately separated by concerns.\n\n## Summary\nThe code provides a basic command-line interface with features for command completion and history storage using the `readline` library. It does a commendable job on keeping possible commands and their completion minimalistic and straightforward. However, it encounters issues with the handling of user home directory paths in the context of a history file and lacks robust error handling concerning file operations for command history. Proper checks against null pointers need implementation to ensure more stable execution. \n\n## Open source alternatives\n- GNU Readline: The library is already being used but reviewing the full set of its capabilities can further optimize command-line interaction with line editing and custom completers.\n- `clap`: While mostly known for CLI parsing in Rust apps, its approach can inspire improvements in arguments handling.\n- `Linenoise`: A small, portable GNU Readline replacement for C.",
"source": "// Written by retoor@molodetz.nl\n\n// This source code provides command-line input functionalities with autocomplete and history features using readline library functionalities. It allows users to complete commands and manage input history.\n\n// External includes: \n// - <readline/readline.h>\n// - <readline/history.h>\n\n// MIT License: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction.\n\n#include <readline/readline.h>\n#include <readline/history.h>\n\n#define HISTORY_FILE \"~/.calpaca_history\"\n\nbool line_initialized = false;\n\nchar* line_command_generator(const char* text, int state) {\n static int list_index, len;\n const char* commands[] = {\"help\", \"exit\", \"list\", \"review\", \"refactor\", \"obfuscate\", NULL};\n\n if (!state) {\n list_index = 0;\n len = strlen(text);\n }\n\n while (commands[list_index]) {\n const char* command = commands[list_index++];\n if (strncmp(command, text, len) == 0) {\n return strdup(command);\n }\n }\n\n return NULL;\n}\n\nchar** line_command_completion(const char* text, int start, int end) {\n rl_attempted_completion_over = 1;\n return rl_completion_matches(text, line_command_generator);\n}\n\nvoid line_init() {\n if (!line_initialized) {\n rl_attempted_completion_function = line_command_completion;\n line_initialized = true;\n read_history(HISTORY_FILE);\n }\n}\n\nchar* line_read(char* prefix) {\n char* data = readline(prefix);\n if (!(data && *data)) {\n return NULL;\n }\n return data;\n}\n\nvoid line_add_history(char* data) {\n read_history(HISTORY_FILE);\n add_history(data);\n write_history(HISTORY_FILE);\n}",
"review": "8\n\n## Bugs\n- No bugs identified; the code appears to function as intended.\n\n## Optimizations\n- Use `realpath` to ensure `HISTORY_FILE` resolves correctly and consistently, mitigating issues with tilde expansion.\n- Ensure `line_initialized` is thread-safe if accessed from multiple threads.\n- Consider freeing duplicated strings to avoid memory leaks.\n\n## Good points\n- Efficient handling of command completions using a static array of known commands.\n- Proper use of the readline library functions for history and completion features.\n- The code is concise and focused on specific functionalities of input handling.\n\n## Summary\nThe provided C code uses the readline library to facilitate command-line input with autocomplete and history functionality. It is well-written, adhering to proper memory management practices, though some minor optimizations could be considered. The implementation is straightforward, offering essential features like command completion and history saving for a better user experience in command-line applications.\n\n## Open source alternatives\n- [GNU Readline](https://tiswww.case.edu/php/chet/readline/rltop.html): A library that already provides similar functionalities and more, widely used and maintained.\n- [linenoise](https://github.com/antirez/linenoise): A small self-contained alternative to readline that supports history and completion.\n- [libedit](http://thrysoee.dk/editline/): Another more lightweight alternative that offers a BSD-licensed readline replacement.",
"filename": "line.h",
"path": "line.h",
"directory": "",
"grade": 7,
"size": 1263,
"line_count": 53
"grade": 8,
"size": 1777,
"line_count": 62
}