diff --git a/Cargo.toml b/Cargo.toml index 83bef41..19db930 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,11 @@ [package] name = "stack" -version = "1.5.0" +version = "1.8.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -powershell_script = "1.1.0" +opener = "0.6.1" rand = "0.8" reqwest = { version = "0.11.0", features = ["blocking"] } diff --git a/src/main.rs b/src/main.rs index 6960444..c37e076 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,10 @@ -use powershell_script::PsScriptBuilder; +use opener; use rand::seq::SliceRandom; use std::collections::HashMap; use std::env; -use std::fs::File; +use std::fs::{self, File}; use std::io::{self, Error, Read, Write}; +use std::path::Path; use std::thread; use std::thread::sleep; use std::time::{Duration, SystemTime, UNIX_EPOCH}; @@ -457,6 +458,7 @@ impl Executor { } } + // 文字列をUTF-8でエンコード "encode" => { let string = self.pop_stack().get_string(); if let Some(first_char) = string.chars().next() { @@ -571,13 +573,10 @@ impl Executor { "while" => { let cond = self.pop_stack().get_string(); let code = self.pop_stack().get_string(); - loop { - if { - self.evaluate_program(cond.clone()); - !self.pop_stack().get_bool() - } { - break; - } + while { + self.evaluate_program(cond.clone()); + self.pop_stack().get_bool() + } { self.evaluate_program(code.clone()); } } @@ -866,37 +865,47 @@ impl Executor { // ファイルを開く "open" => { - let _result = std::process::Command::new("cmd") - .args(&["/C", "start", "", self.pop_stack().get_string().as_str()]) - .spawn(); - } - - // シェルコマンドを実行 - "shell" => { - let ps = PsScriptBuilder::new() - .no_profile(true) - .non_interactive(true) - .hidden(false) - .print_commands(false) - .build(); - let _ = ps.run( - "[Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding('utf-8')", - ); - let result = ps.run(self.pop_stack().get_string().as_str()); - match result { - Ok(i) => self.stack.push(Type::String( - i.stdout() - .unwrap_or("".to_string()) - .as_str() - .trim() - .to_string(), - )), - Err(_) => { - self.log_print("エラー! シェルスクリプトの実行に失敗しました\n".to_string()) + let _result = opener::open(self.pop_stack().get_string()); + } + + //カレントディレクトリを変更 + "cd" => { + if let Err(err) = std::env::set_current_dir(self.pop_stack().get_string()) { + self.log_print(format!("エラー! {}\n", err)); + } + } + + // カレントディレクトリを表示 + "pwd" => { + if let Ok(current_dir) = std::env::current_dir() { + if let Some(path) = current_dir.to_str() { + self.stack.push(Type::String(String::from(path))); } } } + // ファイル一覧のリスト + "ls" => { + if let Ok(entries) = fs::read_dir(".") { + let value: Vec = entries + .filter_map(|entry| { + entry + .ok() + .and_then(|e| e.file_name().into_string().ok()) + .map(|x| Type::String(x)) + }) + .collect(); + self.stack.push(Type::List(value)); + } + } + + // フォルダかどうか判定 + "folder" => { + let path = self.pop_stack().get_string(); + let path = Path::new(path.as_str()); + self.stack.push(Type::Bool(path.is_dir())); + } + // コマンドとして認識されない場合は文字列とする _ => self.stack.push(Type::String(command)), }