Skip to content

Commit e711d4f

Browse files
authored
Merge pull request #190 from henninggross/feature/sort-timetable
sort timetable
2 parents b57448b + 54bb936 commit e711d4f

File tree

7 files changed

+33
-6
lines changed

7 files changed

+33
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Changelog
22

3+
## v4.3.0 (2024-02-10)
4+
5+
* New feature for sorting timetables [#189](https://github.com/gerardcl/renfe-cli/issues/189)
6+
37
## v4.2.1 (2024-02-02)
48

5-
* Fix navigation with proper date handling [#184](https://github.com/gerardcl/renfe-cli/issues/187)
9+
* Fix navigation with proper date handling [#187](https://github.com/gerardcl/renfe-cli/issues/187)
610

711
## v4.2.0 (2023-11-26)
812

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "renfe-cli"
3-
version = "4.2.1"
3+
version = "4.3.0"
44
edition = "2021"
55
license = "BSD-3-Clause"
66
description = "CLI for searching Renfe train timetables in the Spanish country"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The navigation through the site happens in the following steps:
3232
4. Writes down and selects the month to search for
3333
5. Writes down and selects the year to search for
3434
6. Clicks on search button
35-
7. Parses the HTML data and prints the timetable
35+
7. Parses the HTML data, optionally sorts the connections and prints the timetable
3636

3737
```bash
3838
$ renfe-cli -h
@@ -45,6 +45,7 @@ Options:
4545
-m, --month MONTH Set Month to search timetable for (default: today's month)
4646
-y, --year YEAR Set Year to search timetable for (default: today's year)
4747
-w, --wait SECONDS Set Wait time in seconds for Renfe search result page (default: 2)
48+
-s, --sort Option to sort the timetable by Duration
4849
-h, --help Print this help menu
4950
```
5051

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ maintainers = [
1414
description = "Get faster Renfe Spanish Trains timetables in your terminal."
1515
readme = "README.md"
1616
license = {file = "LICENSE"}
17-
keywords = ["timetables", "trains", "renfe", "cli", "rust"]
17+
keywords = ["timetables", "trains", "renfe", "cli", "rust", "pyo3", "maturin"]
1818
classifiers = [
1919
"Programming Language :: Rust",
2020
"Programming Language :: Python :: Implementation :: CPython",

src/cli.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub fn main() -> PyResult<()> {
3636
.opt_str("w")
3737
.unwrap_or(2.to_string())
3838
.parse::<u64>()?;
39+
let sorted: bool = matches.opt_present("s");
3940

4041
println!(
4142
"Today is: {}-{}-{}",
@@ -45,7 +46,7 @@ pub fn main() -> PyResult<()> {
4546
);
4647
println!("Searching timetable for date: {}-{}-{}", year, month, day);
4748

48-
let timetable = search_timetable(origin, destination, day, month, year, wait)?;
49+
let timetable = search_timetable(origin, destination, day, month, year, wait, sorted)?;
4950

5051
print_timetable(timetable);
5152
Ok(())
@@ -92,6 +93,7 @@ fn set_opts() -> Options {
9293
"Set Wait time in seconds for Renfe search result page (default: 2)",
9394
"SECONDS",
9495
);
96+
opts.optflag("s", "sort", "Option to sort the timetable by Duration");
9597
opts.optflag("h", "help", "Print this help menu");
9698

9799
opts

src/timetable.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ fn to_renfe_month(month: String) -> String {
5555
months[month.as_str()].to_owned()
5656
}
5757

58+
fn get_duration_from_renfe_string(s: &str) -> u16 {
59+
let splits: Vec<&str> = s.split(' ').collect();
60+
if s.contains('h') {
61+
let hours = splits[0].parse::<u16>().unwrap();
62+
let minutes = splits[2].parse::<u16>().unwrap();
63+
hours * 60 + minutes
64+
} else {
65+
splits[0].parse::<u16>().unwrap()
66+
}
67+
}
68+
5869
#[pyfunction]
5970
pub fn search_timetable(
6071
origin: String,
@@ -63,6 +74,7 @@ pub fn search_timetable(
6374
month: String,
6475
year: String,
6576
wait: u64,
77+
sorted: bool,
6678
) -> PyResult<Vec<Vec<String>>> {
6779
println!("loading headless chrome browser");
6880
let browser = Browser::new(LaunchOptions {
@@ -188,6 +200,13 @@ pub fn search_timetable(
188200
tracks.push(row);
189201
}
190202

203+
if sorted {
204+
println!("sorting timetable");
205+
tracks.sort_by(|a, b| {
206+
get_duration_from_renfe_string(&a[3]).cmp(&get_duration_from_renfe_string(&b[3]))
207+
});
208+
}
209+
191210
Ok(tracks)
192211
}
193212

@@ -225,6 +244,7 @@ mod tests {
225244
now.month().to_string(),
226245
now.year().to_string(),
227246
15,
247+
false,
228248
)?);
229249

230250
Ok(())

0 commit comments

Comments
 (0)