write to location

This commit is contained in:
Luka Jankovic 2025-07-26 01:51:24 +02:00
parent fe34c61b12
commit d273544702
3 changed files with 19 additions and 8 deletions

View file

@ -18,8 +18,8 @@
#include "kubo_command.h" #include "kubo_command.h"
static inline void kubo_command_exit(struct kubo_context *context); static inline void kubo_command_exit(struct kubo_context *context, char *rest);
static inline void kubo_command_write(struct kubo_context *context); static inline void kubo_command_write(struct kubo_context *context, char *rest);
static const struct kubo_command_data kubo_commands[] = { static const struct kubo_command_data kubo_commands[] = {
{":q", kubo_command_exit}, {":w", kubo_command_write}}; {":q", kubo_command_exit}, {":w", kubo_command_write}};
@ -42,23 +42,31 @@ void kubo_command_pop() {
} }
void kubo_command_accept_cmd(struct kubo_context *context) { void kubo_command_accept_cmd(struct kubo_context *context) {
char *cmd = kubo_command_get_str();
cmd = strtok(cmd, " ");
char *rest = strtok(NULL, " ");
for (size_t i = 0; i < kubo_commands_size; i++) { for (size_t i = 0; i < kubo_commands_size; i++) {
if (strcmp(kubo_commands[i].input, kubo_char_arr_build_str(&command)) == if (strcmp(cmd, kubo_commands[i].input) == 0) {
0) { kubo_commands[i].function(context, rest);
kubo_commands[i].function(context);
return; return;
} }
} }
free(cmd);
} }
char *kubo_command_get_str() { return kubo_char_arr_build_str(&command); } char *kubo_command_get_str() { return kubo_char_arr_build_str(&command); }
void kubo_command_clear() { kubo_char_arr_clear(&command); } void kubo_command_clear() { kubo_char_arr_clear(&command); }
static inline void kubo_command_exit(struct kubo_context *context) { static inline void kubo_command_exit(struct kubo_context *context, char *rest) {
(void)rest;
context->exit_pending = true; context->exit_pending = true;
} }
static inline void kubo_command_write(struct kubo_context *context) { static inline void kubo_command_write(struct kubo_context *context, char *rest) {
if (rest) {
context->file_name = rest;
}
kubo_file_write(context); kubo_file_write(context);
} }

View file

@ -26,7 +26,7 @@
#include "kubo_context.h" #include "kubo_context.h"
#include "kubo_file.h" #include "kubo_file.h"
typedef void (*kubo_command_function)(struct kubo_context *context); typedef void (*kubo_command_function)(struct kubo_context *context, char *rest);
struct kubo_command_data { struct kubo_command_data {
const char *input; const char *input;
@ -40,6 +40,7 @@ void kubo_command_append_char(int c);
void kubo_command_pop(); void kubo_command_pop();
void kubo_command_accept_cmd(struct kubo_context *context); void kubo_command_accept_cmd(struct kubo_context *context);
void kubo_command_clear(); void kubo_command_clear();
char *kubo_command_get_str(); char *kubo_command_get_str();
#endif #endif

View file

@ -25,4 +25,6 @@ void kubo_command_bar_render(int x, int y, int font_size) {
x += MeasureText(cmd, font_size); x += MeasureText(cmd, font_size);
x += 2; x += 2;
DrawRectangle(x, y, MeasureText("x", font_size), font_size, WHITE); DrawRectangle(x, y, MeasureText("x", font_size), font_size, WHITE);
free(cmd);
} }