This repository was archived by the owner on Dec 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathex_4.rs
More file actions
99 lines (85 loc) · 1.96 KB
/
ex_4.rs
File metadata and controls
99 lines (85 loc) · 1.96 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Copyright © 2013 Free Software Foundation, Inc
See licensing in LICENSE file
File: examples/ex_4.rs
Author: Jesse 'Jeaye' Wilkerson
Description:
Window creation and input example.
Use the cursor keys to move the window
around the screen.
*/
extern crate ncurses;
use ncurses::*;
static WINDOW_HEIGHT: i32 = 3;
static WINDOW_WIDTH: i32 = 10;
fn main()
{
/* Setup ncurses. */
initscr();
raw();
/* Allow for extended keyboard (like F1). */
keypad(stdscr(), true);
noecho();
/* Invisible cursor. */
curs_set(CURSOR_VISIBILITY::CURSOR_INVISIBLE);
/* Status/help info. */
addstr("Use the arrow keys to move").unwrap();
mvprintw(LINES() - 1, 0, "Press F1 to exit").unwrap();
refresh();
/* Get the screen bounds. */
let mut max_x = 0;
let mut max_y = 0;
getmaxyx(stdscr(), &mut max_y, &mut max_x);
/* Start in the center. */
let mut start_y = (max_y - WINDOW_HEIGHT) / 2;
let mut start_x = (max_x - WINDOW_WIDTH) / 2;
let mut win = create_win(start_y, start_x);
let mut ch = getch();
while ch != KEY_F(1)
{
match ch
{
KEY_LEFT =>
{
start_x -= 1;
destroy_win(win);
win = create_win(start_y, start_x);
},
KEY_RIGHT =>
{
start_x += 1;
destroy_win(win);
win = create_win(start_y, start_x);
},
KEY_UP =>
{
start_y -= 1;
destroy_win(win);
win = create_win(start_y, start_x);
},
KEY_DOWN =>
{
start_y += 1;
destroy_win(win);
win = create_win(start_y, start_x);
},
_ => { }
}
ch = getch();
}
endwin();
}
fn create_win(start_y: i32, start_x: i32) -> WINDOW
{
let win = newwin(WINDOW_HEIGHT, WINDOW_WIDTH, start_y, start_x);
box_(win, 0, 0);
wrefresh(win);
win
}
fn destroy_win(win: WINDOW)
{
let ch = ' ' as chtype;
wborder(win, ch, ch, ch, ch, ch, ch, ch, ch);
wrefresh(win);
delwin(win);
}