This PR addresses several issues with the ansi command and improves error handling throughout the codebase.
- Issue: The
ansicommand had incorrect color reversal when using thestrikeattribute - Fix: Corrected the attribute parsing logic in
fill_modifiers()function to properly handle ANSI style attributes - Files:
crates/nu-color-config/src/nu_style.rs
- Issue: Invalid ANSI attributes were silently ignored instead of providing helpful error messages
- Fix: Added comprehensive error handling with descriptive messages for both invalid attribute codes and names
- Files:
crates/nu-color-config/src/nu_style.rs,crates/nu-command/src/strings/ansi/ansi_.rs
- Issue: Code contained several
unwrap()andexpect()calls that could panic - Fix: Replaced with safe alternatives using
unwrap_or(),unwrap_or_else(), and proper error propagation - Files:
crates/nu-color-config/src/nu_style.rs,crates/nu-command/src/strings/ansi/ansi_.rs
- Issue: Code duplication and inconsistent error messages
- Fix: Extracted constants for valid attributes, created helper functions, and improved maintainability
- Files:
crates/nu-color-config/src/nu_style.rs
Before (buggy behavior):
# This would incorrectly reverse colors when using strike
$ ansi --escape { fg: "#ff0000" bg: "#000000" attr: "strike" }After (correct behavior):
# Now correctly applies strike-through without color reversal
$ ansi --escape { fg: "#ff0000" bg: "#000000" attr: "strike" }Invalid attribute codes now show helpful errors:
$ ansi --escape { fg: "#ff0000" attr: "x" }
Error: nu::shell::error
× Invalid ANSI attribute code
help: Valid codes are: b (bold), i (italic), u (underline), s (strike), d (dimmed), r (reverse), h (hidden), l (blink), n (normal)Invalid attribute names now show helpful errors:
$ ansi --escape { fg: "#ff0000" attr: "invalid" }
Error: nu::shell::error
× Invalid ANSI attribute name
help: Valid names are: bold, italic, underline, strike, dimmed, reverse, hidden, blink, normalValid attribute combinations work correctly:
# Multiple attributes as codes
$ ansi --escape { fg: "#ff0000" attr: "biu" } # bold + italic + underline
# Multiple attributes as names
$ ansi --escape { fg: "#00ff00" attr: "bold italic" }
# Mixed codes and names
$ ansi --escape { fg: "#0000ff" attr: "b,underline" }- All existing tests pass
- Manual testing confirms proper error messages for invalid attributes
- ANSI command works correctly with valid attributes
- Clippy passes with no warnings
None. All changes maintain backward compatibility.
- Fixed:
ansicommand now correctly handles thestrikeattribute without color reversal - Improved: Better error messages when invalid ANSI attributes are provided
- Enhanced: More robust error handling throughout the ANSI styling system