From 88dbc368d17a63f0012985937352b0cb8e4c562e Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sun, 26 Feb 2023 13:59:34 +0100 Subject: [PATCH] Fix formatting-related panic --- CHANGELOG.md | 6 ++++++ packages/lib/src/globals/fs.rs | 3 +-- packages/lib/src/globals/net.rs | 3 +-- packages/lib/src/lua/stdio/formatting.rs | 15 +++++---------- packages/lib/src/tests.rs | 5 ++++- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc6bec1..123b797 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Fixed + +- Fixed crash when using `stdio.color("reset")` or `stdio.style("reset")` in a CI environment or non-interactive terminal + ## `0.5.1` - February 25th, 2023 ### Added diff --git a/packages/lib/src/globals/fs.rs b/packages/lib/src/globals/fs.rs index 4aa19f1..aec68fd 100644 --- a/packages/lib/src/globals/fs.rs +++ b/packages/lib/src/globals/fs.rs @@ -45,8 +45,7 @@ async fn fs_read_dir(_: &'static Lua, path: String) -> LuaResult> { .map(|inner_path| { inner_path .trim() - .strip_prefix(&dir_string_prefix) - .unwrap() + .trim_start_matches(&dir_string_prefix) .to_owned() }) .collect::>(); diff --git a/packages/lib/src/globals/net.rs b/packages/lib/src/globals/net.rs index 767d49f..7c524a1 100644 --- a/packages/lib/src/globals/net.rs +++ b/packages/lib/src/globals/net.rs @@ -37,8 +37,7 @@ pub fn create(lua: &'static Lua) -> LuaResult { fn create_user_agent_header() -> String { let (github_owner, github_repo) = env!("CARGO_PKG_REPOSITORY") - .strip_prefix("https://github.com/") - .unwrap() + .trim_start_matches("https://github.com/") .split_once('/') .unwrap(); format!("{github_owner}-{github_repo}-cli") diff --git a/packages/lib/src/lua/stdio/formatting.rs b/packages/lib/src/lua/stdio/formatting.rs index d132c8a..042fed2 100644 --- a/packages/lib/src/lua/stdio/formatting.rs +++ b/packages/lib/src/lua/stdio/formatting.rs @@ -57,12 +57,12 @@ pub fn format_style(style: Option<&'static Style>) -> String { } else if let Some(style) = style { // HACK: We have no direct way of referencing the ansi color code // of the style that console::Style provides, and we also know for - // sure that styles always include the reset sequence at the end + // sure that styles always include the reset sequence at the end, + // unless we are in a CI environment on non-interactive terminal style .apply_to("") .to_string() - .strip_suffix(STYLE_RESET_STR) - .unwrap() + .trim_end_matches(STYLE_RESET_STR) .to_string() } else { STYLE_RESET_STR.to_string() @@ -248,8 +248,7 @@ pub fn pretty_format_luau_error(e: &LuaError, colorized: bool) -> String { if is_override { if !trace_override || traceback.lines().count() > full_trace.len() { full_trace = traceback - .strip_prefix("override traceback:") - .unwrap() + .trim_start_matches("override traceback:") .to_string(); trace_override = true; } @@ -269,11 +268,7 @@ pub fn pretty_format_luau_error(e: &LuaError, colorized: bool) -> String { "{}\n{}\n{}\n{}", pretty_format_luau_error(root_cause, colorized), stack_begin, - if full_trace.starts_with("stack traceback:") { - full_trace.strip_prefix("stack traceback:\n").unwrap() - } else { - &full_trace - }, + full_trace.trim_start_matches("stack traceback:\n"), stack_end ) } diff --git a/packages/lib/src/tests.rs b/packages/lib/src/tests.rs index 407a49c..fb2dc63 100644 --- a/packages/lib/src/tests.rs +++ b/packages/lib/src/tests.rs @@ -33,7 +33,10 @@ macro_rules! create_tests { .map(ToString::to_string) .collect::>() ); - let script_name = full_name.strip_suffix(".luau").unwrap(); + let script_name = full_name + .trim_end_matches(".luau") + .trim_end_matches(".lua") + .to_string(); let exit_code = lune.run(&script_name, &script).await?; Ok(exit_code) }