-
Notifications
You must be signed in to change notification settings - Fork 99
/
ex_3.rs
87 lines (71 loc) · 1.58 KB
/
ex_3.rs
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
/*
Copyright © 2013 Free Software Foundation, Inc
See licensing in LICENSE file
File: examples/ex_3.rs
Author: Jesse 'Jeaye' Wilkerson
Description:
Implementation of a very simple pager.
Usage:
./bin/ex_3 <rust file>
Example:
./bin/ex_3 examples/ex_3.rs
*/
extern crate ncurses;
use std::env;
use std::io::Read;
use std::fs;
use std::path::Path;
use ncurses::*;
fn open_file() -> fs::File
{
let args : Vec<_> = env::args().collect();
if args.len() != 2
{
println!("Usage:\n\t{} <rust file>", args[0]);
println!("Example:\n\t{} examples/ex_3.rs", args[0]);
panic!("Exiting");
}
let reader = fs::File::open(Path::new(&args[1]));
reader.ok().expect("Unable to open file")
}
fn prompt()
{
addstr("<-Press Any Key->").unwrap();
getch();
}
fn main()
{
let reader = open_file().bytes();
/* Start ncurses. */
initscr();
keypad(stdscr(), true);
noecho();
/* Get the screen bounds. */
let mut max_x = 0;
let mut max_y = 0;
getmaxyx(stdscr(), &mut max_y, &mut max_x);
/* Read the whole file. */
for ch in reader
{
if ch.is_err()
{ break; }
let ch = ch.unwrap();
/* Get the current position on the screen. */
let mut cur_x = 0;
let mut cur_y = 0;
getyx(stdscr(), &mut cur_y, &mut cur_x);
if cur_y == (max_y - 1)
{
/* Status bar at the bottom. */
prompt();
/* Once a key is pressed, clear the screen and continue. */
clear();
mv(0, 0);
}
addch(ch as chtype);
}
/* Terminate ncurses. */
mv(max_y -1, 0);
prompt();
endwin();
}