Skip to content

Commit 5f48287

Browse files
authored
Merge pull request #5 from Nodraak/master
Fix compilation and add TGV
2 parents 245f28a + a879735 commit 5f48287

File tree

8 files changed

+142
-29
lines changed

8 files changed

+142
-29
lines changed

Cargo.lock

Lines changed: 36 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ name = "sl"
1111
[lib]
1212

1313
name = "sl"
14+
path = "src/sl.rs"
1415

1516
[dependencies]
1617

17-
getopts = "0.2.14"
18+
getopts = "0.2"
1819
libc = "0.2"
19-
20-
[dependencies.ncurses]
21-
22-
git = "https://github.com/jeaye/ncurses-rs.git"
20+
ncurses = "5.99"

src/bin/sl.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ use sl::Train;
1010
use sl::d51::SL;
1111
use sl::c51::C51;
1212
use sl::logo::Logo;
13+
use sl::tgv::TGV;
1314

14-
trait Render: Train + Copy {
15+
fn speed2delay(speed: u32) -> u32 {
16+
// if 4_000_000: 100 km/h -> 40 ms
17+
4_000_000/speed
18+
}
19+
20+
trait Render: Train {
1521
fn render(&self, x: i32) {
1622
let mut len = 0 as i32;
17-
let y = unsafe { ncurses::LINES } / 2;
23+
let y = ncurses::LINES() / 2;
1824
let body_iter = self.body().iter();
1925
let wheelset_iter = self.wheelset(x as usize).iter();
2026
let iter = body_iter.chain(wheelset_iter);
@@ -55,7 +61,7 @@ trait Render: Train + Copy {
5561
}
5662

5763
fn render_line(&self, y: i32, x: i32, line: &str) {
58-
let paint_len = ( unsafe { ncurses::COLS } - x) as usize;
64+
let paint_len = ( ncurses::COLS() - x) as usize;
5965
if paint_len < line.len() {
6066
ncurses::mvaddstr(y, x, &line[0..paint_len]);
6167
} else if x < 0 {
@@ -68,9 +74,11 @@ trait Render: Train + Copy {
6874
}
6975
}
7076

77+
impl Render for dyn Train {}
7178
impl Render for SL {}
7279
impl Render for C51 {}
7380
impl Render for Logo {}
81+
impl Render for TGV {}
7482

7583
fn main() {
7684
use libc::signal;
@@ -83,6 +91,7 @@ fn main() {
8391
let mut opts = Options::new();
8492
opts.optflag("l", "", "logo");
8593
opts.optflag("c", "", "C51");
94+
opts.optflag("G", "", "TGV");
8695
opts.optflag("a", "", "reserved for future use");
8796
opts.optflag("f", "", "reserved for future use");
8897

@@ -98,23 +107,37 @@ fn main() {
98107

99108
ncurses::noecho();
100109
ncurses::curs_set(ncurses::CURSOR_VISIBILITY::CURSOR_INVISIBLE);
101-
ncurses::nodelay( unsafe { ncurses::stdscr }, true);
102-
ncurses::leaveok( unsafe { ncurses::stdscr }, true);
103-
ncurses::scrollok( unsafe { ncurses::stdscr }, false);
110+
ncurses::nodelay(ncurses::stdscr(), true);
111+
ncurses::leaveok(ncurses::stdscr(), true);
112+
ncurses::scrollok(ncurses::stdscr(), false);
104113

105-
for x in (-85.. unsafe { ncurses::COLS } ).rev() {
106-
ncurses::clear();
114+
let train: Box<dyn Train> = {
107115
if matches.opt_present("l") {
108-
Logo.render(x)
116+
Box::new(Logo)
109117
} else if matches.opt_present("c") {
110-
C51.render(x)
118+
Box::new(C51)
119+
} else if matches.opt_present("G") {
120+
// First Non prototype TGV was orange.
121+
// Note: must be called after initscr().
122+
if ncurses::has_colors() {
123+
ncurses::start_color();
124+
ncurses::init_pair(1, ncurses::COLOR_YELLOW, ncurses::COLOR_BLACK);
125+
ncurses::attron(ncurses::COLOR_PAIR(1));
126+
}
127+
128+
Box::new(TGV)
111129
} else {
112-
SL.render(x)
113-
};
130+
Box::new(SL)
131+
}
132+
};
133+
134+
for x in (-85..ncurses::COLS()).rev() {
135+
ncurses::clear();
136+
train.render(x);
114137
ncurses::getch();
115138
ncurses::refresh();
116139
unsafe {
117-
usleep(40000);
140+
usleep(speed2delay(train.speed()));
118141
}
119142
}
120143
ncurses::endwin();

src/c51.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ use super::data::{C51WHL, C51BODY, COAL};
55
pub struct C51;
66

77
impl Train for C51 {
8+
fn speed(&self) -> u32 {
9+
100
10+
}
11+
812
fn body(&self) -> &'static [&'static str] {
913
&C51BODY
1014
}

src/d51.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ use super::data::{D51BODY, D51WHL, COAL};
55
pub struct SL;
66

77
impl Train for SL {
8+
fn speed(&self) -> u32 {
9+
85
10+
}
11+
812
fn body(&self) -> &'static [&'static str] {
913
&D51BODY
1014
}

src/data.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,29 @@ pub static LCAR: [&'static str; 6] = ["____________________ ",
9797
"|__________________| ",
9898
"|__________________| ",
9999
" (O) (O) "];
100+
101+
pub static TGVBODY: [&'static str; 7] = [
102+
r" ______________ / ",
103+
r" ____________/______________|__\_______ ",
104+
r" _____/ |IIIIIIIIIII|_______________________| ",
105+
r" __/__/_|_| ______________________________| ",
106+
r" __/ | | / |||||||||||| TGV || | ",
107+
r" /> [] | | /___________||||||||||||______|| | ",
108+
r" \_==================================================| ",
109+
];
110+
111+
pub static TGVWHL: [[&'static str; 1]; 2] = [
112+
[r" |___/==0==0==|_________________________/===0==0==\| "],
113+
[r" |___/0==0==0=|_________________________/=0==0==0=\| "],
114+
];
115+
116+
pub static TGVCAR: [&'static str; 8] = [
117+
r" ___________ ",
118+
r" |_====____|\________________________________________ ",
119+
r"o| |||| ___ | ",
120+
r"H|______________|___|_________________________________| ",
121+
r"H| |||| |[]| |[]1| [] [ ] [ ] [ ] [ ] | ",
122+
r"H|__||||_|__|___|___|_________________________________| ",
123+
r"D|==============|===|=================================| ",
124+
r" _/ 0==0 |____________________________________/ 0=| ",
125+
];

src/sl.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
pub mod d51;
22
pub mod c51;
33
pub mod logo;
4+
pub mod tgv;
45
mod data;
56

67
pub trait Train {
8+
/// Approximate speed in km/h
9+
fn speed(&self) -> u32 {
10+
100
11+
}
712
fn body(&self) -> &'static [&'static str];
813
fn wheelset(&self, x: usize) -> &'static [&'static str];
914
fn tender(&self) -> Option<&'static [&'static str]> {

src/tgv.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use super::Train;
2+
use super::data::{TGVWHL, TGVBODY, TGVCAR};
3+
4+
#[derive(Copy, Clone)]
5+
pub struct TGV;
6+
7+
impl Train for TGV {
8+
fn speed(&self) -> u32 {
9+
// in reality, TGV is even faster, but we want to be able to see it!
10+
300
11+
}
12+
13+
fn body(&self) -> &'static [&'static str] {
14+
&TGVBODY
15+
}
16+
17+
fn wheelset(&self, x: usize) -> &'static [&'static str] {
18+
&TGVWHL[(x % 2)]
19+
}
20+
21+
fn wagons(&self) -> u32 {
22+
2
23+
}
24+
25+
fn wagon(&self) -> Option<&'static [&'static str]> {
26+
Some(&TGVCAR)
27+
}
28+
}

0 commit comments

Comments
 (0)