stonktop 0.3.0

A top-like terminal UI for monitoring stock and cryptocurrency prices
Documentation
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Configuration file handling with TOML support.
//!
//! Because hardcoding your portfolio would be too easy.

use crate::models::Holding;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

/// Application configuration loaded from TOML file.
/// Where you define which assets will keep you up at night.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
    /// General settings
    #[serde(default)]
    pub general: GeneralConfig,

    /// Watchlist symbols
    #[serde(default)]
    pub watchlist: WatchlistConfig,

    /// Holdings/portfolio configuration
    #[serde(default)]
    pub holdings: Vec<HoldingConfig>,

    /// Display settings
    #[serde(default)]
    pub display: DisplayConfig,

    /// Color scheme
    #[serde(default)]
    pub colors: ColorConfig,

    /// Groups of symbols
    #[serde(default)]
    pub groups: HashMap<String, Vec<String>>,
}

/// General application settings.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeneralConfig {
    /// Refresh interval in seconds
    #[serde(default = "default_refresh_interval")]
    pub refresh_interval: f64,

    /// API timeout in seconds
    #[serde(default = "default_timeout")]
    pub timeout: u64,

    /// Default currency for display
    #[serde(default = "default_currency")]
    pub currency: String,
}

impl Default for GeneralConfig {
    fn default() -> Self {
        Self {
            refresh_interval: default_refresh_interval(),
            timeout: default_timeout(),
            currency: default_currency(),
        }
    }
}

fn default_refresh_interval() -> f64 {
    5.0
}
fn default_timeout() -> u64 {
    10
}
fn default_currency() -> String {
    "USD".to_string()
}

/// Watchlist configuration.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct WatchlistConfig {
    /// List of symbols to watch
    #[serde(default)]
    pub symbols: Vec<String>,
}

/// Single holding configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HoldingConfig {
    /// Ticker symbol
    pub symbol: String,
    /// Number of shares/units
    pub quantity: f64,
    /// Cost basis per share
    pub cost_basis: f64,
}

impl From<HoldingConfig> for Holding {
    fn from(config: HoldingConfig) -> Self {
        Holding {
            symbol: config.symbol,
            quantity: config.quantity,
            cost_basis: config.cost_basis,
        }
    }
}

/// Display settings.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DisplayConfig {
    /// Show summary header
    #[serde(default = "default_true")]
    pub show_header: bool,

    /// Show fundamentals (open, high, low, etc.)
    #[serde(default)]
    pub show_fundamentals: bool,

    /// Show holdings view
    #[serde(default)]
    pub show_holdings: bool,

    /// Show separators between groups
    #[serde(default = "default_true")]
    pub show_separators: bool,

    /// Default sort field
    #[serde(default)]
    pub sort_by: String,

    /// Sort in descending order
    #[serde(default = "default_true")]
    pub sort_descending: bool,
}

impl Default for DisplayConfig {
    fn default() -> Self {
        Self {
            show_header: true,
            show_fundamentals: false,
            show_holdings: false,
            show_separators: true,
            sort_by: "change_percent".to_string(),
            sort_descending: true,
        }
    }
}

fn default_true() -> bool {
    true
}

/// Color configuration using hex codes.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ColorConfig {
    /// Color for positive changes
    #[serde(default = "default_gain_color")]
    pub gain: String,

    /// Color for negative changes
    #[serde(default = "default_loss_color")]
    pub loss: String,

    /// Color for neutral/unchanged
    #[serde(default = "default_neutral_color")]
    pub neutral: String,

    /// Header background color
    #[serde(default = "default_header_color")]
    pub header: String,

    /// Border color
    #[serde(default = "default_border_color")]
    pub border: String,
}

impl Default for ColorConfig {
    fn default() -> Self {
        Self {
            gain: default_gain_color(),
            loss: default_loss_color(),
            neutral: default_neutral_color(),
            header: default_header_color(),
            border: default_border_color(),
        }
    }
}

fn default_gain_color() -> String {
    "#00ff00".to_string()
}
fn default_loss_color() -> String {
    "#ff0000".to_string()
}
fn default_neutral_color() -> String {
    "#ffffff".to_string()
}
fn default_header_color() -> String {
    "#1e90ff".to_string()
}
fn default_border_color() -> String {
    "#444444".to_string()
}

impl Config {
    /// Load configuration from file.
    pub fn load(path: &PathBuf) -> Result<Self> {
        let content = fs::read_to_string(path)
            .with_context(|| format!("Failed to read config file: {}", path.display()))?;

        let config: Config = toml::from_str(&content)
            .with_context(|| format!("Failed to parse config file: {}", path.display()))?;

        Ok(config)
    }

    /// Load configuration from default location or create default.
    pub fn load_or_default() -> Self {
        if let Some(path) = Self::default_config_path() {
            if path.exists() {
                match Self::load(&path) {
                    Ok(config) => return config,
                    Err(e) => {
                        eprintln!("Warning: Failed to load config: {}", e);
                    }
                }
            }
        }
        Config::default()
    }

    /// Get the default configuration file path.
    pub fn default_config_path() -> Option<PathBuf> {
        dirs::config_dir().map(|p| p.join("stonktop").join("config.toml"))
    }

    /// Save configuration to file.
    /// For when you finally decide to commit to your investment strategy.
    #[allow(dead_code)] // Used by unit tests; --init uses sample_config() directly
    pub fn save(&self, path: &PathBuf) -> Result<()> {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).with_context(|| {
                format!("Failed to create config directory: {}", parent.display())
            })?;
        }

        let content = toml::to_string_pretty(self).context("Failed to serialize configuration")?;

        fs::write(path, content)
            .with_context(|| format!("Failed to write config file: {}", path.display()))?;

        Ok(())
    }

    /// Get all symbols from watchlist and holdings.
    pub fn all_symbols(&self) -> Vec<String> {
        let mut symbols: Vec<String> = self.watchlist.symbols.clone();

        // Add holding symbols
        for holding in &self.holdings {
            if !symbols.contains(&holding.symbol) {
                symbols.push(holding.symbol.clone());
            }
        }

        // Add group symbols
        for group_symbols in self.groups.values() {
            for symbol in group_symbols {
                if !symbols.contains(symbol) {
                    symbols.push(symbol.clone());
                }
            }
        }

        symbols
    }

    /// Get holdings as Holding structs.
    pub fn get_holdings(&self) -> Vec<Holding> {
        self.holdings.iter().cloned().map(Into::into).collect()
    }
}

/// Generate a sample configuration file content.
pub fn sample_config() -> &'static str {
    r##"# Stonktop Configuration File
# A top-like terminal UI for stock and crypto prices

[general]
# Refresh interval in seconds
refresh_interval = 5.0
# API timeout in seconds
timeout = 10
# Default currency for display
currency = "USD"

[watchlist]
# Symbols to track
symbols = [
    "AAPL",
    "GOOGL",
    "MSFT",
    "AMZN",
    "NVDA",
    "BTC-USD",
    "ETH-USD",
]

# Portfolio holdings (optional)
[[holdings]]
symbol = "AAPL"
quantity = 10
cost_basis = 150.00

[[holdings]]
symbol = "BTC-USD"
quantity = 0.5
cost_basis = 30000.00

[display]
# Show summary header
show_header = true
# Show fundamental data (open, high, low)
show_fundamentals = false
# Show portfolio holdings
show_holdings = false
# Show separators between groups
show_separators = true
# Default sort field: symbol, name, price, change, change_percent, volume, market_cap
sort_by = "change_percent"
# Sort in descending order
sort_descending = true

[colors]
# Colors in hex format
gain = "#00ff00"
loss = "#ff0000"
neutral = "#ffffff"
header = "#1e90ff"
border = "#444444"

# Symbol groups (for organizing watchlists)
[groups]
tech = ["AAPL", "GOOGL", "MSFT", "NVDA"]
crypto = ["BTC-USD", "ETH-USD", "SOL-USD"]
"##
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_empty_toml_defaults() {
        let mut tmp = NamedTempFile::new().unwrap();
        write!(tmp, "").unwrap();
        let config = Config::load(&tmp.path().to_path_buf()).unwrap();
        assert!(config.watchlist.symbols.is_empty());
        assert!(config.holdings.is_empty());
        assert_eq!(config.general.refresh_interval, 5.0);
    }

    #[test]
    fn test_malformed_toml_error() {
        let mut tmp = NamedTempFile::new().unwrap();
        write!(tmp, "this is not valid {{ toml }}").unwrap();
        assert!(Config::load(&tmp.path().to_path_buf()).is_err());
    }

    #[test]
    fn test_missing_holdings_section() {
        let mut tmp = NamedTempFile::new().unwrap();
        write!(tmp, "[watchlist]\nsymbols = [\"AAPL\"]").unwrap();
        let config = Config::load(&tmp.path().to_path_buf()).unwrap();
        assert!(config.holdings.is_empty());
        assert_eq!(config.watchlist.symbols, vec!["AAPL"]);
    }

    #[test]
    fn test_all_symbols_deduplication() {
        let config = Config {
            watchlist: WatchlistConfig {
                symbols: vec!["AAPL".to_string(), "GOOGL".to_string()],
            },
            holdings: vec![HoldingConfig {
                symbol: "AAPL".to_string(), // duplicate
                quantity: 10.0,
                cost_basis: 150.0,
            }],
            groups: {
                let mut m = HashMap::new();
                m.insert(
                    "tech".to_string(),
                    vec!["AAPL".to_string(), "MSFT".to_string()],
                );
                m
            },
            ..Config::default()
        };
        let symbols = config.all_symbols();
        // AAPL should appear only once
        assert_eq!(symbols.iter().filter(|s| *s == "AAPL").count(), 1);
        assert!(symbols.contains(&"GOOGL".to_string()));
        assert!(symbols.contains(&"MSFT".to_string()));
    }

    #[test]
    fn test_save_round_trip() {
        let config = Config {
            watchlist: WatchlistConfig {
                symbols: vec!["AAPL".to_string()],
            },
            ..Config::default()
        };
        let tmp = NamedTempFile::new().unwrap();
        config.save(&tmp.path().to_path_buf()).unwrap();
        let loaded = Config::load(&tmp.path().to_path_buf()).unwrap();
        assert_eq!(loaded.watchlist.symbols, config.watchlist.symbols);
    }

    #[test]
    fn test_default_config_path() {
        let path = Config::default_config_path();
        assert!(path.is_some());
        let p = path.unwrap();
        assert!(p.to_string_lossy().contains("stonktop"));
    }

    #[test]
    fn test_sample_config_valid_toml() {
        let sample = sample_config();
        let _config: Config = toml::from_str(sample).expect("sample config should be valid TOML");
    }
}