summaryrefslogtreecommitdiff
path: root/src/parser.rs
blob: 06afce7b9cb18cc0f50d86a458d32c56dbfd7497 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use std::{
    fs::{self, File},
    io::Read,
};

use reqwest::Client;
use toml;
use serde::{Deserialize, Serialize};
use serde_json;
use serde_yml; 

// use crate::configmanager::Config;
use crate::shared::*;

fn get_config_path() -> Result<String, Box<dyn std::error::Error>> {
    let home = std::env::var("HOME")?;
    Ok(format!("{}/.config/WeatherFetch/Config.toml", home))
}

pub async fn parse_weather() -> Result<WeatherData, Box<dyn std::error::Error>> {
    let config = get_config()?;
    let client = Client::new();
    let response = client
        .get("https://api.open-meteo.com/v1/forecast")
        .query(&[
            ("latitude", config.lat.to_string()),
            ("longitude", config.lon.to_string()),
            ("current", "temperature_2m,wind_speed_10m".to_string()),
            (
                "hourly",
                "temperature_2m,relative_humidity_2m,wind_speed_10m".to_string(),
            ),
            ("timezone", config.timezone.unwrap_or("Europe/London".to_string())),
        ])
        .send()
        .await?;

    let response_text = response.text().await?;

    let weather_data: WeatherData = serde_json::from_str(&response_text)?;

    let home = std::env::var("HOME")?;
    let cache_dir = format!("{}/.cache/WeatherFetch", home);
    fs::create_dir_all(&cache_dir)?;

    let cache_path = format!("{}/weather.json", cache_dir);
    let json_data = serde_json::to_string_pretty(&weather_data)?;
    fs::write(&cache_path, json_data)?;

    Ok(weather_data)
}

// TODO: Add exclude processing
#[derive(Debug, Deserialize)]
pub struct Config {
    lat: f64,
    lon: f64,
    exclude: String,
    timezone: Option<String>,
}

pub fn get_config() -> Result<Config, Box<dyn std::error::Error>> {
    let config_path = get_config_path()?;

    if File::open(&config_path).is_err() {
        generate_config()?;
    }

    let mut file = File::open(&config_path)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;

    let config: Config = toml::from_str(&content)?;

    Ok(config)
}

/*
pub fn generate_cachedir() -> Result<(), Box<dyn std::error::Error>> {
    let home = std::env::var("HOME")?;
    let cache_path = format!("{}/.cache/WeatherFetch/", home);

    let _ = fs::create_dir(cache_path);

    Ok(())
}
*/

/// Just writing example config to ~/.config/WeatherFetch/Config.toml
///     let _ = generate_config()
/// Result: ~/.config/WeatherFetch/Config.toml:
///     lat = 55.75
///     lon = 37.62
///     exclude = ""
pub fn generate_config() -> Result<(), Box<dyn std::error::Error>> {
    let config_path = get_config_path()?;
    let config = "lat = 55.75\nlon = 37.62\nexclude = \"\"\ntimezone = \"Europe/Moscow\"";
    let path = std::path::Path::new(&config_path);
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }
    fs::write(path, config)?;
    println!("Config file generated at: {}", config_path);
    Ok(())
}

/// For usage like type in `let`
#[derive(Serialize, Deserialize, Debug)]
struct Arts {
    #[serde(rename = "Arts")]
    arts: ArtsData,
}

/// Strings with arts from arts.yaml
#[derive(Serialize, Deserialize, Debug)]
struct ArtsData {
    sun: String,
    snow: String,
    rain: String,
}

/// Weather type checker, crutch, it hurts me to look at it
pub fn determine_weather_type(temp: f32, humidity: Option<u32>) -> &'static str {
    if temp < 0.0 {
        return "snow";
    }

    if let Some(hum) = humidity {
        if hum > 70 && temp < 25.0 {
            return "rain";
        }
    }

    "sun"
}

/// Arts loader with exception wrappers
fn load_arts(debug: bool) -> Result<ArtsData, Box<dyn std::error::Error>> {
    let home = std::env::var("HOME")?;
    let arts_path = format!("{}/.config/WeatherFetch/arts.yaml", home);
    
    let content = match fs::read_to_string(&arts_path) {
        Ok(c) => c,
        Err(_) => {
            // if arts.yaml not found
            return Ok(ArtsData {
                sun: "☀️ - ts emoji means program cant found ~/.config/WeatherFetch/arts.yaml"
                    .to_string(),
                snow: "❄️ - ts emoji means program cant found ~/.config/WeatherFetch/arts.yaml"
                    .to_string(),
                rain: "🌧️ - ts emoji means program cant found ~/.config/WeatherFetch/arts.yaml"
                    .to_string(),
            });
        }
    };

    if debug == true {
        println!("--- RAW FILE CONTENT START ---\n{}\n--- RAW FILE CONTENT END ---", content);
    } 

    let arts: Arts = serde_yml::from_str(&content)?;
    Ok(arts.arts)
}

fn process_placeholders(art: &str) -> String {
    let strart = art.to_string(); 
    let processed_art = strart.replace("{0}", " ") 
        .replace("<Yellow>", "\x1b[0;33m")
        .replace("<Blue>", "\x1b[0;34m")
        .replace("<Purple>", "\x1b[0;35m")
        .replace("<end>", "\x1b[0m");
    processed_art
}

/// Choosing and retuns art (String)
/// Usage:
///     let data: WeatherData = parse_cached()?;
///     prepare_art(&data);
pub fn prepare_art(weather_data: &WeatherData, debug: bool) -> Result<String, Box<dyn std::error::Error>> {
    let arts = load_arts(debug)?;

    let weather_type = determine_weather_type(
        weather_data.current.temperature_2m,
        weather_data.hourly.relative_humidity_2m.first().copied(),
    );

    let selected_art = match weather_type {
        "snow" => &arts.snow,
        "rain" => &arts.rain,
        "sun" => &arts.sun,
        _ => &arts.sun,
    };
    
    if debug == true {
        println!("-- RAW SELECTED ART (debug):\n{:?}\n", selected_art);
        println!("-- RAW SELECTED ART (display):\n{}\n", selected_art);
    }

    let processed_art = process_placeholders(selected_art);

    if debug == true { 
        println!("-- PROCESSED ART (debug):\n{:?}\n", processed_art);
        println!("-- PROCESSED ART (display):");
        println!("contains <Yellow>? {}", selected_art.contains("<Yellow>"));
        println!("contains <Purple>? {}", selected_art.contains("<Purple>"));
        println!("contains <Blue>? {}", selected_art.contains("<Blue>"));
        println!("processed contains \\x1b? {}", processed_art.contains("\x1b"));
    }
    Ok(processed_art)
}

/// Remove ANSI escape codes from string and return visible length
pub fn visible_length(s: &str) -> usize {
    let mut count = 0;
    let mut chars = s.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == '\x1b' && chars.peek() == Some(&'[') {
            // Found ANSI escape sequence, skip until 'm'
            chars.next(); // consume '['
            while let Some(&next_ch) = chars.peek() {
                chars.next(); // consume character
                if next_ch == 'm' {
                    break; // end of ANSI sequence
                }
            }
        } else {
            count += 1;
        }
    }

    count
}


/// Left-pad a string with spaces to the specified visible width, accounting for ANSI codes
pub fn pad_with_ansi(s: &str, width: usize) -> String {
    let visible_len = visible_length(s);
    let padding_needed = if width > visible_len { width - visible_len } else { 0 };
    let padding = " ".repeat(padding_needed);
    format!("{}{}", s, padding)
}