11 lines
3.5 KiB
JSON
Raw Normal View History

2025-01-04 07:20:50 +00:00
{
"extension": ".h",
2025-01-04 07:44:34 +00:00
"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.",
2025-01-04 07:20:50 +00:00
"filename": "line.h",
"path": "line.h",
"directory": "",
2025-01-04 07:44:34 +00:00
"grade": 8,
"size": 1777,
"line_count": 62
2025-01-04 07:20:50 +00:00
}