~arivigo/sline

ref: 22b5cfa710ec05734e8a0cf898c38c74e00a723c sline/history.c -rw-r--r-- 714 bytes
22b5cfa7Ariadna Vigo Solved history ordering issues when deleting entries 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* See LICENSE for copyright and license details. */

#include <string.h>

#include "history.h"
#include "strlcpy.h"

#define HISTORY_SIZE 50

char *history[HISTORY_SIZE];
int hist_top, hist_pos;
size_t hist_entry_size;

const char *
history_get(int pos)
{
	if (pos < 0 || pos > hist_top)
		return NULL;

	return history[pos];
}

void
history_next(void)
{
	if (strlen(history[hist_top]) == 0)
		return;

	++hist_top;
	if (hist_top >= HISTORY_SIZE)
		history_rotate();
}

void
history_rotate(void)
{
	int i;

	for (i = 1; i < HISTORY_SIZE; ++i)
		strlcpy(history[i - 1], history[i], hist_entry_size);

	--hist_top;
}

void
history_set(int pos, const char *input)
{
	strlcpy(history[pos], input, hist_entry_size);
}