From 5095d2cc9ff1ee07fa01acb00b900d64074c71bf Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 24 May 2022 10:59:19 +0200 Subject: [PATCH] [Refactor] Move hour name calculation to the clock module --- src/clock.rs | 10 ++++++++++ src/svg_clock.rs | 13 +++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/clock.rs b/src/clock.rs index 59c76e4..1a01783 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -6,6 +6,12 @@ use suncalc::SunTimes; use crate::config::Config; +const HOUR_NAMES: [&str; 24] = [ + "Candle", "Ice", "Comet", "Thimble", "Root", "Mist", "Sprout", "Rainbow", "Worm", "Bud", + "Blossom", "Ladybug", "Geese", "Dust", "Peach", "Fog", "Acorn", "Gourd", "Soup", "Crow", + "Mushroom", "Thunder", "Frost", "Lantern", +]; + pub enum DayPart { LocalNow, UtcNow, @@ -98,3 +104,7 @@ pub fn get_hms(config: &Option, which: DayPart) -> (u8, u8, u8) { pub fn get_utc_offset() -> i32 { Local::now().offset().local_minus_utc() } + +pub fn get_utc_hour_name(hour: usize) -> &'static str { + HOUR_NAMES[hour] +} diff --git a/src/svg_clock.rs b/src/svg_clock.rs index 6a110d6..ee43858 100644 --- a/src/svg_clock.rs +++ b/src/svg_clock.rs @@ -13,14 +13,11 @@ use svg::{ }; use usvg::Tree; -use crate::clock::{get_hms, get_moon_phase, get_seconds_since_midnight, get_utc_offset, DayPart}; +use crate::clock::{ + get_hms, get_moon_phase, get_seconds_since_midnight, get_utc_hour_name, get_utc_offset, DayPart, +}; use crate::config::Config; -const HOUR_NAMES: [&str; 24] = [ - "Candle", "Ice", "Comet", "Thimble", "Root", "Mist", "Sprout", "Rainbow", "Worm", "Bud", - "Blossom", "Ladybug", "Geese", "Dust", "Peach", "Fog", "Acorn", "Gourd", "Soup", "Crow", - "Mushroom", "Thunder", "Frost", "Lantern", -]; const IMAGE_WIDTH: u32 = 700; const HOUR_NAME_FONT_SIZE: f32 = IMAGE_WIDTH as f32 * 0.019109; const OUTER_R: f32 = (IMAGE_WIDTH as f32) / 2.0 - 3.0 * HOUR_NAME_FONT_SIZE; @@ -90,7 +87,7 @@ fn create_temp_document(hour: usize) -> Document { let hour_name_text_path = TextPath::new() .set("xlink:href", "#hour-name-path") .set("startOffset", "50%") - .add(TextNode::new(HOUR_NAMES[hour])); + .add(TextNode::new(get_utc_hour_name(hour))); let hour_name_text = Text::new() .set("id", "hour-name") .set("text-anchor", "middle") @@ -598,7 +595,7 @@ pub fn gen_svg( "y", (current_box_height / 5.0) + (current_hour_name_font_size / 2.0), ) - .add(TextNode::new(HOUR_NAMES[utc_hour as usize])); + .add(TextNode::new(get_utc_hour_name(utc_hour as usize))); let current_time_text = Text::new() .set("font-size", current_time_font_size)