Skip to content

Commit a879735

Browse files
committed
Add -G option that add an orange TGV
Inspired from https://github.com/mtoyoda/sl/pull/62/files
1 parent e8c8c62 commit a879735

File tree

6 files changed

+98
-8
lines changed

6 files changed

+98
-8
lines changed

src/bin/sl.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ 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;
1723
let y = ncurses::LINES() / 2;
@@ -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

@@ -102,19 +111,33 @@ fn main() {
102111
ncurses::leaveok(ncurses::stdscr(), true);
103112
ncurses::scrollok(ncurses::stdscr(), false);
104113

105-
for x in (-85..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)