summaryrefslogtreecommitdiffstats
path: root/src/utils.rs
blob: b248d92ce48a5e376b6851c7a754802aa962d54c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use anyhow::{anyhow, Result};
use chrono::prelude::*;
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}"))
}

pub fn now() -> String {
    let now = Local::now();
    now.to_rfc3339_opts(SecondsFormat::Secs, false)
}