websocat 4.0.0-alpha3

Command-line client for web sockets, like netcat/curl/socat for ws://.
Documentation
use base64::Engine as _;

pub struct ScenarioPrinter {
    out: String,
    indent: usize,
}

impl Default for ScenarioPrinter {
    fn default() -> Self {
        Self::new()
    }
}

impl ScenarioPrinter {
    pub fn new() -> ScenarioPrinter {
        ScenarioPrinter {
            out: String::with_capacity(1024),
            indent: 0,
        }
    }

    pub fn print_line(&mut self, s: &str) {
        // 1000 to prevent excessible printing (i.e. memory consumption) in case
        // of a bug where excessible `decrease_indent` overflowed the usize.
        for _ in 0..(self.indent.min(1000)) {
            self.out.push_str("  ");
        }
        self.out.push_str(s);
        self.out.push('\n');
    }
    pub fn increase_indent(&mut self) {
        self.indent += 1;
    }
    pub fn decrease_indent(&mut self) {
        self.indent -= 1;
    }

    pub fn into_result(self) -> String {
        self.out
    }
}

pub struct StrLit<T: std::fmt::Display>(pub T);
impl<T: std::fmt::Display> std::fmt::Display for StrLit<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let tmp = format!("{}", self.0);
        if tmp.contains('"') || tmp.contains('\\') || tmp.contains('\n') {
            write!(
                f,
                "b64str(\"{}\")",
                base64::prelude::BASE64_STANDARD.encode(tmp)
            )
        } else {
            write!(f, "\"{}\"", &tmp)
        }
    }
}