feat(main): add --json output mode

Swadhin Biswas committed 7 months ago
c734462
1 parent eb22837
commit c734462
1 file changed
+50 -0
Showing 1 changed file+50-0
▼src/main.rs
+50-0
178
178
}
179
179
180
180
// Load config, then override with CLI args
181
+ let mut config = Config::load_or_default();
182
+ apply_args_to_config(&mut config, &args);
183
+
184
+ // Auto-detect terminal capabilities if backend is "auto"
185
+ if config.image_backend == "auto" {
186
+ let capabilities = image_cache::detect_terminal_capabilities();
187
+ config.image_backend = capabilities.backend.clone();
188
+ }
189
+
190
+ // Only use API image fetching when user explicitly configured it:
191
+ // image_backend must be a graphics backend (not "ascii" / "off")
192
+ // AND image_source must be "auto" (API mode)
193
+ // AND no custom_image already set by user
194
+ let uses_graphics_backend = matches!(
195
+ config.image_backend.as_str(),
196
+ "kitty" | "sixel" | "chafa" | "w3m" | "iterm2"
197
+ );
198
+ let uses_api_source = config.image_source == "auto";
199
+
200
+ if uses_graphics_backend && uses_api_source && config.custom_image.is_none() {
201
+ // Use cached image from previous run (instant, no delay)
202
+ if let Some(cached_img) = image_cache::get_available_image() {
203
+ config.custom_image = Some(cached_img.to_string_lossy().to_string());
204
+ }
205
+
206
+ // Download a new image in background for next run (non-blocking, zero delay)
207
+ image_cache::download_image_background();
208
+ }
209
+
210
+ // Gather system info
211
+ let sys_info = SystemInfo::collect();
212
+
213
+ if args.json {
214
+ print_json(&sys_info);
215
+ return;
216
+ }
217
+
218
+ // Render
219
+ let display = Display::new(&config);
220
+ display.render(&sys_info);
221
+}
222
+
223
+fn apply_args_to_config(config: &mut Config, args: &Args) {
224
+ // Boolean flags: only set if explicitly passed
225
+ if args.no_color || args.stdout {
226
+ config.no_color = true;
227
+ }
228
+ if args.stdout {
229
+ config.stdout = true;
230
+ config.bold = false;
181
231