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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! The kill ring, after Emacs: every substantial deletion or block copy is
//! kept, and ^KP ("put") pastes the newest — pressed again immediately, it
//! cycles the put text through older and older clippings.
use std::collections::VecDeque;
const MAX_ITEMS: usize = 60;
pub struct KillRing {
/// Front is newest.
items: VecDeque<String>,
}
/// An in-progress put-cycle: where the last put landed and which ring index
/// it used, so the next ^KP can swap it for the following item.
#[derive(Debug, Clone, Copy)]
pub struct PutCycle {
pub at: usize,
pub chars: usize,
pub index: usize,
}
impl KillRing {
pub fn new() -> Self {
KillRing {
items: VecDeque::new(),
}
}
pub fn push(&mut self, text: String) {
if text.is_empty() {
return;
}
// Mirror to the OS clipboard; ignore failures (headless, etc.).
if let Ok(mut cb) = arboard::Clipboard::new() {
let _ = cb.set_text(&text);
}
self.items.push_front(text);
self.items.truncate(MAX_ITEMS);
}
/// Newest item, falling back to the OS clipboard when the ring is empty.
pub fn top(&mut self) -> Option<String> {
if self.items.is_empty() {
if let Ok(text) = arboard::Clipboard::new().and_then(|mut cb| cb.get_text()) {
if !text.is_empty() {
self.items.push_front(text);
}
}
}
self.items.front().cloned()
}
pub fn get(&self, index: usize) -> Option<&String> {
self.items.get(index)
}
pub fn len(&self) -> usize {
self.items.len()
}
}
impl Default for KillRing {
fn default() -> Self {
Self::new()
}
}