summaryrefslogtreecommitdiffstats
path: root/src/utils.rs
blob: cb5d1f927eb52a2483db52e510d0563982a2ecc1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use anyhow::{anyhow, Result};
use copypasta::{ClipboardContext, ClipboardProvider};
use std::io::{stdout, Write};

pub fn dump<T: ToString>(text: T, newlines: usize) {
    print!("{}{}", text.to_string(), "\n".repeat(newlines));
    let _ = stdout().flush();
}

pub fn copy(src: &str) -> Result<()> {
    let mut ctx = ClipboardContext::new().map_err(|err| anyhow!("{err}"))?;
    ctx.set_contents(src.to_string())
        .map_err(|err| anyhow!("{err}"))
}

pub fn paste() -> Result<String> {
    let mut ctx = ClipboardContext::new().map_err(|err| anyhow!("{err}"))?;
    ctx.get_contents().map_err(|err| anyhow!("{err}"))
}