Skip to content

Commit

Permalink
feat: allow automatically running commands at launch #25
Browse files Browse the repository at this point in the history
  • Loading branch information
BrewingWeasel committed Nov 8, 2023
1 parent 429284a commit 54f3296
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::{
error::Error,
process::{Command, Output},
};

pub fn run_command(real_command: &str) -> Result<Output, Box<dyn Error>> {
if cfg!(target_os = "windows") {
Ok(Command::new("cmd").args(["/C", real_command]).output()?)
} else {
Ok(Command::new("sh").args(["-c", real_command]).output()?)
}
}
13 changes: 3 additions & 10 deletions src-tauri/src/dictionary.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use shared::*;
use std::{error::Error, fs, process::Command};
use std::{error::Error, fs};
use tauri::State;

use crate::SakinyjeState;
use crate::{commands::run_command, SakinyjeState};

fn get_def_from_file(
lemma: &str,
Expand Down Expand Up @@ -47,14 +47,7 @@ async fn get_def_url(lemma: &str, url: &str) -> Result<String, Box<dyn Error>> {

async fn get_def_command(lemma: &str, cmd: &str) -> Result<String, Box<dyn Error>> {
let real_command = cmd.replacen("{word}", lemma, 1);
let output = if cfg!(target_os = "windows") {
Command::new("cmd").args(["/C", &real_command]).output()?
} else {
Command::new("sh")
.args(["-c", &real_command])
.output()
.expect("failed to execute process")
};
let output = run_command(&real_command)?;
Ok(String::from_utf8(output.stdout)?)
}

Expand Down
9 changes: 9 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
use crate::{add_to_anki::add_to_anki, dictionary::get_defs, language_parsing::parse_text};
use ankiconnect::get_anki_card_statuses;
use chrono::{DateTime, Utc};
use commands::run_command;
use serde::{Deserialize, Serialize};
use shared::{SakinyjeResult, Settings};
use std::{collections::HashMap, fs};
use tauri::{async_runtime::block_on, GlobalWindowEvent, Manager, State, WindowEvent};

mod add_to_anki;
mod ankiconnect;
mod commands;
mod dictionary;
mod language_parsing;

Expand Down Expand Up @@ -60,6 +62,13 @@ impl Default for SharedInfo {
// TODO: handle error
}
}

if let Some(cmds) = &settings.to_run {
for cmd in cmds {
_ = run_command(cmd);
}
}

to_save.last_launched = new_time;
Self { to_save, settings }
}
Expand Down

0 comments on commit 54f3296

Please sign in to comment.