Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/std_io/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use mluau::prelude::*;

use regex::Regex;

pub fn process_debug_values(value: LuaValue, result: &mut String, depth: usize) -> LuaResult<()> {
pub fn process_debug_values(result: &mut String, value: &LuaValue, depth: usize) -> LuaResult<()> {
let left_padding = " ".repeat(2 * depth);
match value {
LuaValue::Table(t) => {
Expand All @@ -12,7 +12,7 @@ pub fn process_debug_values(value: LuaValue, result: &mut String, depth: usize)
for pair in t.pairs::<LuaValue, LuaValue>() {
let (k, v) = pair?;
result.push_str(&format!(" {left_padding}{:#?} = ", k));
process_debug_values(v, result, depth + 1)?;
process_debug_values(result, &v, depth + 1)?;
result.push('\n');
}
result.push_str(&format!("{left_padding}}}"));
Expand Down Expand Up @@ -52,7 +52,7 @@ fn debug(luau: &Lua, stuff: LuaMultiValue) -> LuaResult<LuaString> {
let mut multi_values = stuff.clone();

while let Some(value) = multi_values.pop_front() {
process_debug_values(value, &mut result, 0)?;
process_debug_values(&mut result, &value, 0)?;
if !multi_values.is_empty() {
result += ", ";
}
Expand Down
88 changes: 50 additions & 38 deletions src/std_io/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use mluau::prelude::*;
use super::format;
use crate::std_err::WrappedError;

pub fn debug_print(luau: &Lua, mut multivalue: LuaMultiValue) -> LuaResult<LuaString> {
pub fn debug_print(luau: &Lua, multivalue: LuaMultiValue) -> LuaMultiResult {
let function_name = "dp(...: any)";
let mut result = String::from("");
let mut result = String::new();

while let Some(value) = multivalue.pop_front() {
format::process_debug_values(value, &mut result, 0)?;
if !multivalue.is_empty() {
for (index, value) in multivalue.iter().enumerate() {
format::process_debug_values(&mut result, value, 0)?;
if index + 1 < multivalue.len() {
result.push_str(", ");
}
}
Expand All @@ -24,78 +24,90 @@ pub fn debug_print(luau: &Lua, mut multivalue: LuaMultiValue) -> LuaResult<LuaSt
colors::BOLD_RED, colors::RESET, debug_info.source.replace("string ", ""), debug_info.line, debug_info.function_name, colors::RESET,
&result
)?;
luau.create_string(&result)

Ok(multivalue)
}

pub fn simple_print_and_return(luau: &Lua, mut multivalue: LuaMultiValue) -> LuaValueResult {
pub fn simple_print_and_return(luau: &Lua, multivalue: LuaMultiValue) -> LuaMultiResult {
let formatter: LuaTable = format::cached_formatter(luau)?;
let format_simple: LuaFunction = formatter.raw_get("simple")?;
let mut result = String::from("");

while let Some(value) = multivalue.pop_front() {
match format_simple.call::<LuaString>(value) {
Ok(text) => {
let text = text.to_string_lossy();
result += &text;
let mut output = String::from("");

for (index, value) in multivalue.iter().enumerate() {
let formatted = match format_simple.call::<LuaValue>(value) {
Ok(LuaValue::String(text)) => text.to_string_lossy(),
Ok(other) => {
panic!("p: format.simple returned a non-string, got: {:?}", other);
},
Err(err) => {
return wrap_err!("p: error printing: {}", err);
}
};
if !multivalue.is_empty() {
result += ", ";

output.push_str(&formatted);

if index + 1 < multivalue.len() {
output.push_str(", ");
}
}

puts!("{}", &result)?;
let result = luau.create_string(&result)?;
Ok(LuaValue::String(result))
puts!("{}", &output)?;

Ok(multivalue)
}

pub fn pretty_print(luau: &Lua, mut multivalue: LuaMultiValue) -> LuaResult<()> {
pub fn pretty_print_and_return(luau: &Lua, multivalue: LuaMultiValue) -> LuaMultiResult {
let formatter = format::cached_formatter(luau)?;
let format_pretty: LuaFunction = formatter.raw_get("pretty")?;
let mut result = String::from("");

let mut output = String::from("");

while let Some(value) = multivalue.pop_front() {
match format_pretty.call::<LuaString>(value) {
Ok(text) => {
let text = text.to_string_lossy();
result += &text;
for (index, value) in multivalue.iter().enumerate() {
let formatted = match format_pretty.call::<LuaValue>(value) {
Ok(LuaValue::String(text)) => text.to_string_lossy(),
Ok(other) => {
panic!("pp: format.pretty returned a non-string, got: {:?}", other);
},
Err(err) => {
return wrap_err!("print: error printing: {}", err);
return wrap_err!("pp: error printing: {}", err);
}
};
if !multivalue.is_empty() {
result += ", ";

output.push_str(&formatted);

if index + 1 < multivalue.len() {
output.push_str(", ");
}
}
puts!("{}", &result)?;
Ok(())

puts!("{}", &output)?;

Ok(multivalue)
}

pub fn pretty_print_and_return(luau: &Lua, mut multivalue: LuaMultiValue) -> LuaResult<String> {
pub fn pretty_print(luau: &Lua, mut multivalue: LuaMultiValue) -> LuaResult<()> {
let formatter = format::cached_formatter(luau)?;
let format_pretty: LuaFunction = formatter.raw_get("pretty")?;

let mut result = String::from("");

let mut output = String::from("");

while let Some(value) = multivalue.pop_front() {
match format_pretty.call::<LuaString>(value) {
Ok(text) => {
let text = text.to_string_lossy();
result += &text;
output += &text;
},
Err(err) => {
return wrap_err!("pp: error printing: {}", err);
return wrap_err!("print: error printing: {}", err);
}
};
if !multivalue.is_empty() {
result += ", ";
output += ", ";
}
}
puts!("{}", &result)?;
Ok(result)
puts!("{}", &output)?;
Ok(())
}

pub fn clear(_luau: &Lua, _value: LuaValue) -> LuaValueResult {
Expand Down
23 changes: 23 additions & 0 deletions tests/luau/globals/output.luau
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ print(vector.create(1, 2, 3))
local array_with_many_values = {}; for i = 1, 1001 do table.insert(array_with_many_values, i) end
-- print(array_with_many_values)

local env = require("@std/env")
local fs = require("@std/fs")

local src = fs.readfile("./src/err.rs")
Expand All @@ -120,3 +121,25 @@ local with_newline_strings = {
src = src,
}
print(with_newline_strings)

local cats2 = pp(cats)
assert(typeof(cats2) == "table", "pp should return table when passed table")

local cats3 = p(cats)
assert(typeof(cats3) == "table", "p should return table when passed table")

local buffy2 = buffer.create(12)
buffer.writef32(buffy2, 0, 22.5)
local buffy3 = dp(buffy2)
assert(typeof(buffy3) == "buffer", "dp should return buffer when passed buffer")

-- local badstr = "\x12, \x1b \27 \x12 \2 \33"

local badstr = buffer.tostring(fs.readbytes(env.executable_path, 1, 400))

print("below this should have utf8 replacement char")
print(badstr)
print("below this is dp output")
dp(badstr)

-- dp(format.pretty({ cats = badstr }))