Skip to content

Commit

Permalink
fix: update color mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Nov 14, 2024
1 parent aa68944 commit dec83da
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ target/
.DS_Store
*.svg
!assets/*.svg
pkg/
pkg/
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "githubchart-rust"
version = "5.1.3"
version = "5.1.4"
authors = ["frytg"]
edition = "2021"
license = "MIT"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Use `cargo fmt` to format the code and `cargo test` to run the tests.
Alternatively, you can download a release binary from the [releases page](https://github.com/frytg/githubchart-rust/releases) and run it directly:

```sh
./githubchart output.svg -u frytg
./githubchart-rust output.svg -u frytg
```

## Build
Expand All @@ -43,7 +43,7 @@ cargo build --release
Test the binary with:

```sh
./target/release/githubchart release.svg -u frytg
./target/release/githubchart-rust release.svg -u frytg
```

## Build for Web
Expand Down
13 changes: 8 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ impl Chart {
pub const COLOR_SCHEMES: &[(&str, &[&str])] = &[
(
"default",
&["#eeeeee", "#c6e48b", "#7bc96f", "#239a3b", "#196127"],
&["#F0F3F8", "#9CE2A8", "#39C651", "#339944", "#20602A"],
),
(
"old",
&["#eeeeee", "#d6e685", "#8cc665", "#44a340", "#1e6823"],
"dark",
&["#191C1F", "#0A431D", "#0D5926", "#1AB34D", "#2BE168"],
),
(
"halloween",
Expand Down Expand Up @@ -89,11 +89,14 @@ pub async fn fetch_github_stats(
mod tests;

#[wasm_bindgen]
pub async fn generate_github_chart(username: &str, color_scheme: Option<String>) -> Result<String, JsValue> {
pub async fn generate_github_chart(
username: &str,
color_scheme: Option<String>,
) -> Result<String, JsValue> {
let stats = fetch_github_stats(username)
.await
.map_err(|e| JsValue::from_str(&e.to_string()))?;

let colors = match color_scheme.as_deref() {
Some(scheme) => COLOR_SCHEMES
.iter()
Expand Down
10 changes: 5 additions & 5 deletions src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ struct Point {
fn svg_add_points(grid: &[Vec<Point>], svg: &mut String, colors: &[&str]) {
for (x, row) in grid.iter().enumerate() {
for (y, point) in row.iter().enumerate() {
if point.score == -1 {
if point.score == -1 || !point.date.contains("-") {
continue;
}
svg.push_str(&format!(
r#"<rect x="{}" y="{}" width="10" height="10" style="fill:{};" data-score="{}" data-date="{}"/>"#,
r#"<rect x="{}" y="{}" rx="2" ry="2" width="10" height="10" style="fill:{};" data-score="{}" data-date="{}" />"#,
(x * CUBE_SIZE) + X_PAD,
(y * CUBE_SIZE) + Y_PAD,
point_color(point.score, colors),
Expand Down Expand Up @@ -105,9 +105,9 @@ fn svg_add_months(svg: &mut String, stats: &[(String, i32)]) {
fn point_color<'a>(score: i32, colors: &'a [&str]) -> &'a str {
let index = match score {
0 => 0,
1..=3 => 1,
4..=6 => 2,
7..=9 => 3,
1 => 1,
2 => 2,
3 => 3,
_ => 4,
};
colors.get(index).unwrap_or(&colors[0])
Expand Down
2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod tests {
assert_eq!(COLOR_SCHEMES[0].1.len(), 5);

// Test old scheme
assert_eq!(COLOR_SCHEMES[1].0, "old");
assert_eq!(COLOR_SCHEMES[1].0, "dark");
assert_eq!(COLOR_SCHEMES[1].1.len(), 5);

// Test halloween scheme
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use regex::Regex;
use githubchart_rust::{fetch_github_stats, Chart, COLOR_SCHEMES};
use regex::Regex;

#[tokio::test]
async fn test_github_stats_fetching() {
Expand Down

0 comments on commit dec83da

Please sign in to comment.