Skip to content

Commit

Permalink
Merge pull request #38 from Hamatti/configurable-highlights
Browse files Browse the repository at this point in the history
Add support for configurable highlights for individual players
  • Loading branch information
Hamatti authored Mar 5, 2022
2 parents e5edbbe + 280c23d commit 02be94e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.2.0 - 2022-03-04

### Added

- Support for configurable highlight feature for individual players

## 1.1.1 - 2021-11-01

### Fixed
Expand Down
33 changes: 32 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nhl-235"
version = "1.1.1"
version = "1.2.0"
authors = ["Juha-Matti Santala <[email protected]>"]
edition = "2018"
license = "MIT"
Expand All @@ -19,4 +19,5 @@ tokio = { version = "1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
structopt = "0.3.13"
atty = "0.2"
atty = "0.2"
dirs = "4.0"
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ Store the file with filename `235` in a folder that is in the path.
235
```

### Highlight favorite players

235 (from `1.2.0` onwards) supports configurable highlights of individual players.

To do this, you first need to create a config file to your home directory called `.235.config` and then call the script with

```
235 --highlight
```

### Current version

```
Expand Down
60 changes: 48 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
#[macro_use]
extern crate colour;
use atty::Stream;
use dirs::home_dir;
use itertools::{EitherOrBoth::*, Itertools};
use reqwest::Error;
use std::collections::HashMap;
use std::fs::File;
use std::io::Error as StdError;
use std::io::Read;
use std::process;
use structopt::StructOpt;

Expand Down Expand Up @@ -47,6 +51,8 @@ struct Cli {
version: bool,
#[structopt(long)]
nocolors: bool,
#[structopt(long)]
highlight: bool,
}

fn main() {
Expand All @@ -58,10 +64,15 @@ fn main() {
if args.version {
println!("{}", env!("CARGO_PKG_VERSION"));
} else {
let highlights = if args.highlight {
read_highlight_config().unwrap_or_default()
} else {
Vec::new()
};
match fetch_games() {
Ok(scores) => {
let parsed_games = parse_games(scores);
print_games(parsed_games, use_colors);
print_games(parsed_games, use_colors, &highlights);
}
Err(err) => {
handle_request_error(err);
Expand All @@ -70,6 +81,23 @@ fn main() {
}
}

fn read_highlight_config() -> Result<Vec<String>, StdError> {
let mut config_file = home_dir().unwrap();
config_file.push(".235.config");

let mut file = File::open(config_file.as_path())?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;

let highlights: Vec<String> = contents
.split('\n')
.map(str::to_string)
.filter(|s| s != "")
.collect();

Ok(highlights)
}

fn handle_request_error(e: reqwest::Error) {
if e.is_connect() {
println!("ERROR: Can't connect to the API. It might be because your Internet connection is down.");
Expand Down Expand Up @@ -150,12 +178,12 @@ fn parse_games(scores: APIResponse) -> Vec<Option<Game>> {
}

/// Handler function to print multiple Games
fn print_games(games: Vec<Option<Game>>, use_colors: bool) {
fn print_games(games: Vec<Option<Game>>, use_colors: bool, highlights: &[String]) {
match games.len() {
0 => println!("No games today."),
_ => {
games.into_iter().for_each(|game| match game {
Some(game) => print_game(&game, use_colors),
Some(game) => print_game(&game, use_colors, &highlights),
None => (),
});
}
Expand Down Expand Up @@ -263,7 +291,7 @@ fn extract_scorer_name(name: &str) -> String {
name.join(" ")
}

fn print_game(game: &Game, use_colors: bool) {
fn print_game(game: &Game, use_colors: bool, highlights: &[String]) {
let home_scores: Vec<&Goal> = game
.goals
.iter()
Expand Down Expand Up @@ -326,9 +354,9 @@ fn print_game(game: &Game, use_colors: bool) {
let score_pairs = home_scores.into_iter().zip_longest(away_scores.into_iter());
for pair in score_pairs {
match pair {
Both(home, away) => print_both_goals(home, away, use_colors),
Left(home) => print_home_goal(home, use_colors),
Right(away) => print_away_goal(away, use_colors),
Both(home, away) => print_both_goals(home, away, use_colors, highlights),
Left(home) => print_home_goal(home, use_colors, highlights),
Right(away) => print_away_goal(away, use_colors, highlights),
}
}

Expand All @@ -337,9 +365,9 @@ fn print_game(game: &Game, use_colors: bool) {
// If we later add assists by Finns, this needs to be rewritten.
if let Some(shootout_goal) = shootout_scorer {
if shootout_goal.team == game.home {
print_home_goal(shootout_goal, use_colors)
print_home_goal(shootout_goal, use_colors, highlights)
} else {
print_away_goal(shootout_goal, use_colors)
print_away_goal(shootout_goal, use_colors, highlights)
}
}
println!();
Expand All @@ -361,11 +389,13 @@ fn print_game(game: &Game, use_colors: bool) {
}
}

fn print_both_goals(home: &Goal, away: &Goal, use_colors: bool) {
fn print_both_goals(home: &Goal, away: &Goal, use_colors: bool, highlights: &[String]) {
let home_message = format!("{:<15} {:>2} ", home.scorer, home.minute);
if atty::is(Stream::Stdout) && use_colors {
if home.special {
magenta!("{}", home_message);
} else if highlights.contains(&home.scorer) {
yellow!("{}", home_message);
} else {
cyan!("{}", home_message);
}
Expand All @@ -377,6 +407,8 @@ fn print_both_goals(home: &Goal, away: &Goal, use_colors: bool) {
if atty::is(Stream::Stdout) && use_colors {
if away.special {
magenta_ln!("{}", away_message);
} else if highlights.contains(&away.scorer) {
yellow_ln!("{}", away_message);
} else {
cyan_ln!("{}", away_message);
}
Expand All @@ -385,11 +417,13 @@ fn print_both_goals(home: &Goal, away: &Goal, use_colors: bool) {
}
}

fn print_home_goal(home: &Goal, use_colors: bool) {
fn print_home_goal(home: &Goal, use_colors: bool, highlights: &[String]) {
let message = format!("{:<15} {:>2}", home.scorer, home.minute);
if atty::is(Stream::Stdout) && use_colors {
if home.special {
magenta_ln!("{}", message);
} else if highlights.contains(&home.scorer) {
yellow_ln!("{}", message);
} else {
cyan_ln!("{}", message);
}
Expand All @@ -398,14 +432,16 @@ fn print_home_goal(home: &Goal, use_colors: bool) {
}
}

fn print_away_goal(away: &Goal, use_colors: bool) {
fn print_away_goal(away: &Goal, use_colors: bool, highlights: &[String]) {
let message = format!(
"{:<15} {:>2} {:<15} {:>2}",
"", "", away.scorer, away.minute
);
if atty::is(Stream::Stdout) && use_colors {
if away.special {
magenta_ln!("{}", message);
} else if highlights.contains(&away.scorer) {
yellow_ln!("{}", message);
} else {
cyan_ln!("{}", message);
}
Expand Down

0 comments on commit 02be94e

Please sign in to comment.