Skip to content

Commit 8ccb02a

Browse files
committed
cli now opens the html file in browser
Signed-off-by: dvishal485 <dvishal485@gmail.com>
1 parent 8ced73f commit 8ccb02a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/leetcode_api/html_opener.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::process::{Command, Stdio};
2+
pub(crate) fn open_html(file_path: &str) {
3+
if cfg!(target_os = "windows") {
4+
let _ = Command::new("cmd")
5+
.args(&["/C", &format!("start {}", file_path)])
6+
.stdout(Stdio::null())
7+
.stderr(Stdio::null())
8+
.spawn();
9+
} else if cfg!(target_os = "macos") {
10+
let _ = Command::new("open")
11+
.arg(file_path)
12+
.stdout(Stdio::null())
13+
.stderr(Stdio::null())
14+
.spawn();
15+
} else if cfg!(target_os = "linux") {
16+
let _ = Command::new("xdg-open")
17+
.arg(file_path)
18+
.stdout(Stdio::null())
19+
.stderr(Stdio::null())
20+
.spawn();
21+
} else {
22+
eprintln!("HTML Renderer is not supported on this platform.");
23+
};
24+
}

src/leetcode_api/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod execution;
22
mod helpers;
3+
pub mod html_opener;
34
pub mod leetcode;
45
pub mod submission;
56
pub mod user;

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::file_parser::codefile::CodeFile;
2+
use crate::leetcode_api::html_opener::open_html;
23
use crate::utils::{execute_testcases, submit};
34
use clap::{Parser, Subcommand};
45
use colored::Colorize;
@@ -120,6 +121,7 @@ fn main() -> ExitCode {
120121
let filename = "daily_challenge.html";
121122
if let Ok(_) = std::fs::write(filename, question.content) {
122123
println!("Saved question as HTML to {}", filename.cyan());
124+
open_html(filename);
123125
ExitCode::SUCCESS
124126
} else {
125127
eprintln!("Error saving question as HTML");
@@ -153,6 +155,7 @@ fn main() -> ExitCode {
153155
// save to filename
154156
if let Ok(_) = std::fs::write(&filename, question.content) {
155157
println!("Saved question as HTML to {}", filename.cyan());
158+
open_html(&filename);
156159
ExitCode::SUCCESS
157160
} else {
158161
eprintln!("Error saving question as HTML");

0 commit comments

Comments
 (0)