Hello world curses app
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,4 +1,6 @@
|
|||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
||||||
|
|
||||||
*.o
|
bazel-*
|
||||||
|
|
||||||
|
tags
|
||||||
|
|||||||
11
BUILD
Normal file
11
BUILD
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
cc_binary(
|
||||||
|
name = "ncac",
|
||||||
|
srcs = [
|
||||||
|
"ncac.c",
|
||||||
|
"ncac.h",
|
||||||
|
],
|
||||||
|
deps = [
|
||||||
|
"//ui",
|
||||||
|
"@system_include//:curses"
|
||||||
|
],
|
||||||
|
)
|
||||||
11
WORKSPACE
Normal file
11
WORKSPACE
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
new_local_repository(
|
||||||
|
name = "system_include",
|
||||||
|
path = "/usr/lib",
|
||||||
|
build_file_content = """
|
||||||
|
cc_library(
|
||||||
|
name = "curses",
|
||||||
|
srcs = ["libcurses.dylib"],
|
||||||
|
visibility = ["//visibility:public"],
|
||||||
|
)
|
||||||
|
""",
|
||||||
|
)
|
||||||
54
ncac.c
Normal file
54
ncac.c
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#include "ncac.h"
|
||||||
|
|
||||||
|
#include <curses.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <term.h>
|
||||||
|
|
||||||
|
#include "ui/base.h"
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
(void)argc;
|
||||||
|
(void)argv;
|
||||||
|
setup();
|
||||||
|
|
||||||
|
char input;
|
||||||
|
|
||||||
|
int curs_x = 0;
|
||||||
|
int curs_y = 0;
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
switch (input = getch()) {
|
||||||
|
case 'a':
|
||||||
|
draw_text(&curs_x, &curs_y, "Hello, world!");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
finish(0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void finish(int sig) {
|
||||||
|
(void)sig;
|
||||||
|
endwin();
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
// initialize ncurses
|
||||||
|
initscr();
|
||||||
|
|
||||||
|
cbreak();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// don't echo input
|
||||||
|
noecho();
|
||||||
|
|
||||||
|
nonl();
|
||||||
|
|
||||||
|
// install handlers
|
||||||
|
signal(SIGINT, &finish);
|
||||||
|
}
|
||||||
15
ncac.h
Normal file
15
ncac.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef _NCAC_H_
|
||||||
|
#define _NCAC_H_
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cleans up before exit. Installed on SIGINT
|
||||||
|
*/
|
||||||
|
void finish();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets up ncurses and internal data structures
|
||||||
|
*/
|
||||||
|
void setup();
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _NCAC_H_
|
||||||
7
ui/BUILD
Normal file
7
ui/BUILD
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
cc_library(
|
||||||
|
name="ui",
|
||||||
|
srcs = ["base.c"],
|
||||||
|
hdrs = ["base.h"],
|
||||||
|
deps = ["@system_include//:curses"],
|
||||||
|
visibility = ["//visibility:public"],
|
||||||
|
)
|
||||||
17
ui/base.c
Normal file
17
ui/base.c
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include "base.h"
|
||||||
|
|
||||||
|
#include <curses.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void draw_text(int *x, int *y, char *text) {
|
||||||
|
size_t len = strlen(text);
|
||||||
|
if (!len) return;
|
||||||
|
|
||||||
|
mvaddch(*x, *y, text[0]);
|
||||||
|
|
||||||
|
for (size_t i=1; i < strlen(text); i++) {
|
||||||
|
addch(text[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
getyx(stdscr, *y, *x);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user