-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.c
More file actions
78 lines (68 loc) · 2.12 KB
/
Copy pathcommand.c
File metadata and controls
78 lines (68 loc) · 2.12 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once
#include "global.h"
#include <stdint.h>
#include <string.h>
#define CMD_HISTORY_MAX 10
static struct {
char history[CMD_HISTORY_MAX][BUF_SIZE]; // ~40KB
int count;
int cursor;
} command_history;
static void string_insert_at(char *text, const int idx, const char c) {
for (int i = strlen(text) + 1; i > idx; --i) {
text[i] = text[i - 1];
}
text[idx] = c;
}
static void string_remove_at(char *text, const int idx) {
for (int i = idx;; i++) {
text[i] = text[i + 1];
if (text[i] == '\0') {
return;
}
}
}
void command_mode_enter(void) {
g_msg[0] = '\0';
g_msg_line_count = 0;
g_current_command.cursor = 0;
g_current_command.chars[0] = '\0';
g_current_command.len = 0;
command_history.cursor = command_history.count;
}
void command_history_prev(void) {
if (!OPT_CMD_HISTORY || command_history.cursor == 0)
return;
int oldest = command_history.count > CMD_HISTORY_MAX ? command_history.count - CMD_HISTORY_MAX : 0;
if (command_history.cursor <= oldest)
return;
command_history.cursor--;
strlcpy(g_current_command.chars, command_history.history[command_history.cursor % CMD_HISTORY_MAX],
sizeof(g_current_command.chars));
const int len = strlen(g_current_command.chars);
g_current_command.len = len;
g_current_command.cursor = len;
}
void command_history_next(void) {
if (!OPT_CMD_HISTORY || command_history.cursor >= command_history.count)
return;
command_history.cursor++;
if (command_history.cursor >= command_history.count) {
g_current_command.chars[0] = '\0';
g_current_command.len = 0;
g_current_command.cursor = 0;
return;
}
strlcpy(g_current_command.chars, command_history.history[command_history.cursor % CMD_HISTORY_MAX],
sizeof(g_current_command.chars));
const int len = strlen(g_current_command.chars);
g_current_command.len = len;
g_current_command.cursor = len;
}
void command_history_add(void) {
if (!OPT_CMD_HISTORY)
return;
strlcpy(command_history.history[command_history.count % CMD_HISTORY_MAX], g_current_command.chars,
sizeof(command_history.history[0]));
command_history.count++;
}