Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an append-only log to store "commands" #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
#include "commands.h"
#include "controller.h"

command_stack_t *init_command_stack(void) {
command_stack_t *cs = malloc(sizeof(command_stack_t));
command_log_t *init_command_log(void) {
command_log_t *cs = malloc(sizeof(command_log_t));
cs->top = NULL;
cs->bottom = NULL;

return cs;
}

void destroy_command_stack(command_stack_t *cs) {
void destroy_command_log(command_log_t *cs) {
command_t *c = cs->bottom, *tmp;

while (c) {
Expand All @@ -37,10 +37,7 @@ bool is_nav_command(command_t *c) {
return t == HANDLE_MOVE || t == HANDLE_MOVE_TO_EDGE;
}

// -------------------------
// -- History Stack methods
// -------------------------
command_t *append_command(command_stack_t *cs, command_t *c) {
command_t *append_command(command_log_t *cs, command_t *c) {
// assert valid state
assert((!cs->top && !cs->bottom) || (cs->top && cs->bottom));

Expand All @@ -61,7 +58,7 @@ command_t *append_command(command_stack_t *cs, command_t *c) {
return c;
}

command_t *pop_command(command_stack_t *cs) {
command_t *pop_command(command_log_t *cs) {
assert((!cs->top && !cs->bottom) || (cs->top && cs->bottom));

if (!cs->top && !cs->bottom) {
Expand Down
14 changes: 7 additions & 7 deletions src/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ typedef struct command {
struct command *prev;
} command_t;

typedef struct command_stack {
typedef struct command_log {
struct command *bottom;
struct command *top;
} command_stack_t;
} command_log_t;

command_stack_t *init_command_stack(void);
command_log_t *init_command_log(void);

void destroy_command_stack(command_stack_t *cs);
void destroy_command_log(command_log_t *);

command_t *init_command(COMMAND_TYPE t, COMMAND_PAYLOAD p);

bool is_nav_command(command_t *c);
bool is_nav_command(command_t *);

command_t *append_command(command_stack_t *cs, command_t *c);
command_t *append_command(command_log_t *cs, command_t *c);

command_t *pop_command(command_stack_t *cs);
command_t *pop_command(command_log_t *cs);

#endif
6 changes: 3 additions & 3 deletions src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static void dispatch_command(state_t *st, command_t *c) {
}

void replay_history(state_t *st) {
command_stack_t *hs = st->hs;
command_log_t *hs = st->hs;
command_t *c = hs->bottom;

// TODO copy init buffer but not reconstruct it
Expand All @@ -210,8 +210,8 @@ void apply_command(state_t *st, COMMAND_TYPE t, COMMAND_PAYLOAD p) {
dispatch_command(st, c);

// FIXME kill this when implemented redo
destroy_command_stack(st->rs);
st->rs = init_command_stack();
destroy_command_log(st->rs);
st->rs = init_command_log();
}

/*
Expand Down
8 changes: 4 additions & 4 deletions src/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ state_t *init_state(const char *filename) {
st->scr = init_screen(LINES);

// history stack
st->hs = init_command_stack();
st->hs = init_command_log();
// redo stack
st->rs = init_command_stack();
st->rs = init_command_log();

st->status_row = init_row(NULL);
st->prev_key = '\0';
Expand All @@ -31,8 +31,8 @@ void destroy_state(state_t *st) {
destroy_buffer(st->buf);
destroy_screen(st->scr);
destroy_row(st->status_row);
destroy_command_stack(st->hs);
destroy_command_stack(st->rs);
destroy_command_log(st->hs);
destroy_command_log(st->rs);
free(st);
}

Expand Down
4 changes: 2 additions & 2 deletions src/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ typedef struct state {
buffer_t *buf;
screen_t *scr;

command_stack_t *hs;
command_stack_t *rs;
command_log_t *hs;
command_log_t *rs;

size_t cx, cy;
size_t top_row;
Expand Down
8 changes: 4 additions & 4 deletions tests/commands-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include "../src/commands.h"

static void test_command_stack(void) {
command_stack_t *cs = init_command_stack();
static void test_command_log(void) {
command_log_t *cs = init_command_log();
COMMAND_PAYLOAD p;

COMMAND_TYPE t1 = HANDLE_APPEND_ROW;
Expand Down Expand Up @@ -76,11 +76,11 @@ static void test_command_stack(void) {
assert(c1->next == NULL);
free(tmp);

destroy_command_stack(cs);
destroy_command_log(cs);
}

int main(void) {
test_command_stack();
test_command_log();

return 0;
}