summaryrefslogtreecommitdiffstats
path: root/src/utils.rs
blob: 5bc1237995eb61e1834fd2344bb18a63f3674d2f (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
26
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<()> {
    ClipboardContext::new()
        .and_then(|mut ctx| ctx.set_contents(src.to_string()))
        .map_err(|err| anyhow!("Failed to copy, {err}"))
}

pub fn paste() -> Result<String> {
    ClipboardContext::new()
        .and_then(|mut ctx| ctx.get_contents())
        .map_err(|err| anyhow!("Failed to paste, {err}"))
}

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