#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SymbolSet {
pub breakpoint: &'static str,
pub breakpoint_disabled: &'static str,
pub current_instruction: &'static str,
pub changed: &'static str,
pub unchanged: &'static str,
pub error: &'static str,
pub warning: &'static str,
pub info: &'static str,
pub success: &'static str,
pub modified: &'static str,
pub stack_pointer: &'static str,
pub base_pointer: &'static str,
pub separator: &'static str,
pub ellipsis: &'static str,
pub selection: &'static str,
pub flag_set: &'static str,
pub flag_clear: &'static str,
pub scroll_track: &'static str,
pub scroll_thumb: &'static str,
}
impl SymbolSet {
pub const fn unicode() -> Self {
Self {
breakpoint: "\u{25cf}",
breakpoint_disabled: "\u{25cb}",
current_instruction: "\u{25b6}",
changed: "\u{2022}",
unchanged: " ",
error: "\u{2717}",
warning: "\u{26a0}",
info: "\u{2139}",
success: "\u{2713}",
modified: "\u{25cf}",
stack_pointer: "\u{2192}",
base_pointer: "\u{21b3}",
separator: "\u{2502}",
ellipsis: "\u{2026}",
selection: "\u{276f}",
flag_set: "\u{25a0}",
flag_clear: "\u{25a1}",
scroll_track: "\u{2502}",
scroll_thumb: "\u{2588}",
}
}
pub const fn ascii() -> Self {
Self {
breakpoint: "*",
breakpoint_disabled: "o",
current_instruction: ">",
changed: "!",
unchanged: " ",
error: "E",
warning: "W",
info: "i",
success: "+",
modified: "*",
stack_pointer: "->",
base_pointer: "=>",
separator: "|",
ellipsis: "...",
selection: ">",
flag_set: "[x]",
flag_clear: "[ ]",
scroll_track: "|",
scroll_thumb: "#",
}
}
pub const fn for_unicode(enabled: bool) -> Self {
if enabled {
Self::unicode()
} else {
Self::ascii()
}
}
pub const fn flag(&self, set: bool) -> &'static str {
if set {
self.flag_set
} else {
self.flag_clear
}
}
}
impl Default for SymbolSet {
fn default() -> Self {
Self::unicode()
}
}
pub fn detect_unicode_support() -> bool {
if std::env::var_os("RATASM_ASCII").is_some() {
return false;
}
["LC_ALL", "LC_CTYPE", "LANG"]
.iter()
.filter_map(|key| std::env::var(key).ok())
.find(|value| !value.is_empty())
.map(|value| {
let value = value.to_ascii_lowercase();
value.contains("utf-8") || value.contains("utf8")
})
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ascii_set_is_pure_ascii() {
let set = SymbolSet::ascii();
for glyph in [
set.breakpoint,
set.breakpoint_disabled,
set.current_instruction,
set.changed,
set.error,
set.warning,
set.info,
set.success,
set.modified,
set.stack_pointer,
set.base_pointer,
set.separator,
set.ellipsis,
set.selection,
set.flag_set,
set.flag_clear,
] {
assert!(glyph.is_ascii(), "glyph {glyph:?} is not ASCII");
}
}
#[test]
fn distinct_states_use_distinct_glyphs() {
for set in [SymbolSet::unicode(), SymbolSet::ascii()] {
assert_ne!(set.breakpoint, set.breakpoint_disabled);
assert_ne!(set.flag_set, set.flag_clear);
assert_ne!(set.changed, set.unchanged);
}
}
#[test]
fn flag_selects_by_state() {
let set = SymbolSet::unicode();
assert_eq!(set.flag(true), set.flag_set);
assert_eq!(set.flag(false), set.flag_clear);
}
}