From df5cc7cf1cf61077ac0dcd2b376e61b3ed7fc388 Mon Sep 17 00:00:00 2001 From: rillig Date: Thu, 2 Jul 2020 19:11:01 +0000 Subject: cgram(6): use standard cursor keys, use standard shuffle algorithm The previous shuffle algorithm asked for 100 random numbers, on average. The new algorithm asks exactly for 26 random numbers. Curses predefines numeric constants for keys, and there is no apparent reason not to use these standard keys for cursor movement. --- cgram/cgram.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/cgram/cgram.c b/cgram/cgram.c index 76ea55fb..5e5dd556 100644 --- a/cgram/cgram.c +++ b/cgram/cgram.c @@ -120,16 +120,13 @@ static void readquote(void) { } static void encode(void) { - int used[26]; - for (int i=0; i<26; i++) used[i] = 0; - int key[26]; - int keypos=0; - while (keypos < 26) { - int c = random()%26; - if (used[c]) continue; - key[keypos++] = c; - used[c] = 1; + for (int i=0; i<26; i++) key[i] = i; + for (int i=26; i>1; i--) { + int c = random() % i; + int t = key[i-1]; + key[i-1] = key[c]; + key[c] = t; } for (int y=0; y 0) { curx--; } @@ -256,9 +255,11 @@ static void loop(void) { } break; case 5: /* ^E */ + case KEY_END: curx = strlen(lines.v[cury]); break; case 6: /* ^F */ + case KEY_RIGHT: if (curx < strlen(lines.v[cury])) { curx++; } @@ -271,6 +272,7 @@ static void loop(void) { clear(); break; case 14: /* ^N */ + case KEY_DOWN: if (cury < lines.num-1) { cury++; } @@ -282,6 +284,7 @@ static void loop(void) { } break; case 16: /* ^P */ + case KEY_UP: if (cury > 0) { cury--; } @@ -335,6 +338,7 @@ int main(void) { encode(); opencurses(); + keypad(stdscr, TRUE); loop(); closecurses(); -- cgit v1.2.3-56-ge451