Recurse Center Day 7: Basics of ncurses
This is a draft post that I have prematurely published. Currently, I am attending RC and I want to write as much as possible, log my daily learnings and activities. But, I also don't want to spend time on grammar and prose, so I am publishing all the posts which usually I'd have kept in my draft folder.
Ask stupid questions!
I read this lovely article on asking stupid questions by Nikita Kazakov. A TLDR would be:
B Tree
I started hand drawing the B Tree algorithm (I will add the pictures shortly), it immensely helped to understand the basics. Right now, I updated my code to do inserts in root and handle root splits.
B Tree Invariants
I started making a small comic/drawing explaining the properties of B Tree. I will update it here.
ncurses
I paired with Sarah today to work on her 2D ‘Snake’ game which is being written in C. We worked on a bug where it was not detecting the arrow keys properly. While doing that, I learned a couple of new things about ncurses and the terminal:
When arrows keys are pressed, they are displayed as
\033[A
(up),\033[B
(down) etc. They are not multiple characters and the keyboard sends a single integer value representing them e.g. 259 (up), 258 (down) etc. These values could be OS specific, so better use the values from ncurses to match. Here is a sample program:#include <curses.h> int main() { initscr(); keypad(stdscr, TRUE); int c = getch(); if (c == KEY_UP) { // notice this KEY_UP? this comes from ncurses printf("you pressed UP! (%d)\n", c); } }
No need to load the key presses into char or any other data structure. Loading it into
int
is enough, it can capture the arrow keysIf you are on macOS, you need to call the
keypad
method, like shown in the snippet earlier. I found this info in this Stackoverflow postI found this nice article called Interfacing with the key board which was very helpful. It comes with a sample program, that was enough to write my own