magi-code 0.96.1

Repository-aware CLI coding agent for terminal work
Documentation
use anyhow::Result;
use base64::{Engine as _, engine::general_purpose::STANDARD};
use std::io::{self, Write};

pub(crate) trait ClipboardWriter {
    fn copy(&mut self, text: &str) -> Result<()>;
}

#[derive(Debug, Default)]
pub(crate) struct Osc52Clipboard;

impl ClipboardWriter for Osc52Clipboard {
    fn copy(&mut self, text: &str) -> Result<()> {
        write_osc52_copy(io::stdout(), text)
    }
}

pub(crate) fn osc52_sequence(text: &str) -> String {
    format!("\x1b]52;c;{}\x07", STANDARD.encode(text.as_bytes()))
}

fn write_osc52_copy(mut writer: impl Write, text: &str) -> Result<()> {
    writer.write_all(osc52_sequence(text).as_bytes())?;
    writer.flush()?;
    Ok(())
}