Skip to content

Commit

Permalink
Merge pull request #393 from sagiegurari/0.9.3
Browse files Browse the repository at this point in the history
0.9.3
  • Loading branch information
sagiegurari authored Jan 19, 2024
2 parents 4d01661 + fcd261e commit d1c47dc
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 95 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## CHANGELOG

### v0.9.3

* Fix: if/else condition with a command that accepts empty values #390
* Enhancement: dump commands will print to output if no output variable is defined

### v0.9.2 (2023-11-18)

* Enhancement: \[Breaking Change\] add support for renaming directories via mv command #374
Expand Down
6 changes: 5 additions & 1 deletion duckscript_sdk/src/sdk/std/debug/dump_instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ impl Command for CommandImpl {
_arguments: Vec<String>,
_state: &mut HashMap<String, StateValue>,
_variables: &mut HashMap<String, String>,
_output_variable: Option<String>,
output_variable: Option<String>,
instructions: &Vec<Instruction>,
_commands: &mut Commands,
_line: usize,
) -> CommandResult {
let string_value = format!("{:#?}", instructions).to_string();

if output_variable.is_none() {
println!("{}", string_value);
}

CommandResult::Continue(Some(string_value))
}
}
Expand Down
6 changes: 5 additions & 1 deletion duckscript_sdk/src/sdk/std/debug/dump_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ impl Command for CommandImpl {
_arguments: Vec<String>,
state: &mut HashMap<String, StateValue>,
_variables: &mut HashMap<String, String>,
_output_variable: Option<String>,
output_variable: Option<String>,
_instructions: &Vec<Instruction>,
_commands: &mut Commands,
_line: usize,
) -> CommandResult {
let string_value = format!("{:#?}", state).to_string();

if output_variable.is_none() {
println!("{}", string_value);
}

CommandResult::Continue(Some(string_value))
}
}
Expand Down
6 changes: 5 additions & 1 deletion duckscript_sdk/src/sdk/std/debug/dump_variables/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ impl Command for CommandImpl {
_arguments: Vec<String>,
_state: &mut HashMap<String, StateValue>,
variables: &mut HashMap<String, String>,
_output_variable: Option<String>,
output_variable: Option<String>,
_instructions: &Vec<Instruction>,
_commands: &mut Commands,
_line: usize,
) -> CommandResult {
let string_value = format!("{:#?}", variables).to_string();

if output_variable.is_none() {
println!("{}", string_value);
}

CommandResult::Continue(Some(string_value))
}
}
Expand Down
2 changes: 1 addition & 1 deletion duckscript_sdk/src/sdk/std/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) fn load(commands: &mut Commands) -> Result<(), ScriptError> {
commands.set(noop::create(PACKAGE))?;
commands.set(not::create(PACKAGE))?;
commands.set(print::create(PACKAGE))?;
commands.set(println::create(PACKAGE)?)?;
commands.set(println::create(PACKAGE))?;
commands.set(read::create(PACKAGE))?;
commands.set(release::create(PACKAGE))?;

Expand Down
114 changes: 59 additions & 55 deletions duckscript_sdk/src/sdk/std/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,64 @@ fn add_color(
}
}

pub(crate) fn run_print(arguments: Vec<String>) -> CommandResult {
// collect options
let mut styles = vec![];
let mut text_color = None;
let mut background_color = None;
let mut index = 0;
let mut looking_for = LookingFor::Flag;
for argument in &arguments {
index = index + 1;

match looking_for {
LookingFor::Flag => match argument.as_str() {
"--style" | "-s" => looking_for = LookingFor::Style,
"--color" | "-c" => looking_for = LookingFor::TextColor,
"--background-color" | "-bgc" => looking_for = LookingFor::BackgroundColor,
_ => break,
},
LookingFor::Style => {
styles.push(argument.to_string());
looking_for = LookingFor::Flag;
}
LookingFor::TextColor => {
text_color = Some(argument.to_string());
looking_for = LookingFor::Flag;
}
LookingFor::BackgroundColor => {
background_color = Some(argument.to_string());
looking_for = LookingFor::Flag;
}
}
}
if index > 0 {
index = index - 1;
}

// generate whole string
let mut string = String::new();
let mut count = 0;
for argument in &arguments[index..] {
count = count + 1;
string.push_str(argument);
string.push(' ');
}
if count > 0 {
string.remove(string.len() - 1);
}

// add colors
let mut styled_string = string.normal();
styled_string = add_color(styled_string, text_color, false);
styled_string = add_color(styled_string, background_color, true);
styled_string = add_styles(styled_string, styles);

print!("{}", styled_string);

CommandResult::Continue(Some(count.to_string()))
}

#[derive(Clone)]
pub(crate) struct CommandImpl {
package: String,
Expand All @@ -157,61 +215,7 @@ impl Command for CommandImpl {
}

fn run(&self, arguments: Vec<String>) -> CommandResult {
// collect options
let mut styles = vec![];
let mut text_color = None;
let mut background_color = None;
let mut index = 0;
let mut looking_for = LookingFor::Flag;
for argument in &arguments {
index = index + 1;

match looking_for {
LookingFor::Flag => match argument.as_str() {
"--style" | "-s" => looking_for = LookingFor::Style,
"--color" | "-c" => looking_for = LookingFor::TextColor,
"--background-color" | "-bgc" => looking_for = LookingFor::BackgroundColor,
_ => break,
},
LookingFor::Style => {
styles.push(argument.to_string());
looking_for = LookingFor::Flag;
}
LookingFor::TextColor => {
text_color = Some(argument.to_string());
looking_for = LookingFor::Flag;
}
LookingFor::BackgroundColor => {
background_color = Some(argument.to_string());
looking_for = LookingFor::Flag;
}
}
}
if index > 0 {
index = index - 1;
}

// generate whole string
let mut string = String::new();
let mut count = 0;
for argument in &arguments[index..] {
count = count + 1;
string.push_str(argument);
string.push(' ');
}
if count > 0 {
string.remove(string.len() - 1);
}

// add colors
let mut styled_string = string.normal();
styled_string = add_color(styled_string, text_color, false);
styled_string = add_color(styled_string, background_color, true);
styled_string = add_styles(styled_string, styles);

print!("{}", styled_string);

CommandResult::Continue(Some(count.to_string()))
run_print(arguments)
}
}

Expand Down
55 changes: 40 additions & 15 deletions duckscript_sdk/src/sdk/std/println/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
use crate::types::command::create_alias_command;
use crate::sdk::std::print::run_print;
use crate::utils::pckg;
use duckscript::types::command::Command;
use duckscript::types::error::ScriptError;
use duckscript::types::command::{Command, CommandResult};

#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;

pub(crate) fn create(package: &str) -> Result<Box<dyn Command>, ScriptError> {
let name = pckg::concat(package, "Println");
let command = create_alias_command(
name,
vec!["println".to_string()],
include_str!("help.md").to_string(),
"println".to_string(),
include_str!("script.ds").to_string(),
0,
)?;

Ok(Box::new(command))
#[derive(Clone)]
pub(crate) struct CommandImpl {
package: String,
}

impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "Println")
}

fn aliases(&self) -> Vec<String> {
vec!["println".to_string()]
}

fn help(&self) -> String {
include_str!("help.md").to_string()
}

fn clone_and_box(&self) -> Box<dyn Command> {
Box::new((*self).clone())
}

fn run(&self, arguments: Vec<String>) -> CommandResult {
let result = run_print(arguments);

match result {
CommandResult::Continue(_) => println!(""),
_ => (),
};

return result;
}
}

pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}
21 changes: 20 additions & 1 deletion duckscript_sdk/src/sdk/std/println/mod_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
use super::*;
use crate::test;
use crate::test::CommandValidation;

#[test]
fn common_functions() {
test::test_common_command_functions(create("").unwrap());
test::test_common_command_functions(create(""));
}

#[test]
fn run_no_args() {
test::run_script_and_validate(
vec![create("")],
"out = println",
CommandValidation::Match("out".to_string(), "0".to_string()),
);
}

#[test]
fn run_multiple_args() {
test::run_script_and_validate(
vec![create("")],
"out = println 1 2 \"3 4\"",
CommandValidation::Match("out".to_string(), "3".to_string()),
);
}
13 changes: 0 additions & 13 deletions duckscript_sdk/src/sdk/std/println/script.ds

This file was deleted.

20 changes: 14 additions & 6 deletions duckscript_sdk/src/utils/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ mod eval_test;
fn parse(arguments: &Vec<String>) -> Result<Instruction, String> {
let mut line_buffer = String::new();
for argument in arguments {
if argument.contains(" ") {
line_buffer.push('"');
}
line_buffer.push_str(argument);
if argument.contains(" ") {
line_buffer.push('"');
if argument.is_empty() {
line_buffer.push_str("\"\"");
} else if argument.starts_with("\"") && argument.ends_with("\"") {
line_buffer.push('\\');
line_buffer.push_str(argument);
line_buffer.push('\\');
} else {
if argument.contains(" ") {
line_buffer.push('"');
}
line_buffer.push_str(argument);
if argument.contains(" ") {
line_buffer.push('"');
}
}
line_buffer.push(' ');
}
Expand Down
Loading

0 comments on commit d1c47dc

Please sign in to comment.