Skip to content

Commit 06851c8

Browse files
committed
feat: opt-in internal clipboard
1 parent 9eb152b commit 06851c8

6 files changed

Lines changed: 38 additions & 12 deletions

File tree

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ See [how-to-use.md](assets/how-to-use.md) in the `assets` folder.
4141
## Using the Clipboard
4242

4343
`hop` relies on external executables for clipboard management.
44+
If this is a problem for you, try setting `internal-clipboard` to `true` in your config.
4445

4546
#### Copying with Ctrl + C
4647

@@ -66,9 +67,10 @@ There you will also find a default syntax file.
6667

6768
### TOML Contents
6869

69-
- `background`: hexadecimal color code for the background
70-
- `tree-width`: decimal number of columns for the file tree
71-
- `syntax-file`: path to a syntax file for syntax highligting
70+
- `internal-clipboard`: set to `true` if you don't want to use the system-wide clipboard
7271
- `hide-folders`: list of folders to hide in the file tree
73-
- `hover`: hexadecimal color code for hovering color (tree & tabs)
72+
- `syntax-file`: path to a syntax file for syntax highligting
73+
- `tree-width`: decimal number of columns for the file tree
74+
- `background`: hexadecimal color code for the background
7475
- `syntax`: map of syntax token types to hexadecimal color codes
76+
- `hover`: hexadecimal color code for hovering color (tree & tabs)

assets/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[general]
22
# syntax-file = '/opt/syntax.toml'
33
# background = '#111'
4+
5+
internal-clipboard = true
46
hide-folders = ['.git', 'target']
57
tree-width = 30
68
hover = '#444'

src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct General {
2424
background: Option<HexColor>,
2525
syntax_file: Option<String>,
2626
hide_folders: Vec<String>,
27+
internal_clipboard: bool,
2728
hover: HexColor,
2829
tree_width: u16,
2930
}
@@ -119,6 +120,10 @@ pub fn hide_folder(folder: &String) -> bool {
119120
config().general.hide_folders.contains(folder)
120121
}
121122

123+
pub fn internal_clipboard() -> bool {
124+
config().general.internal_clipboard
125+
}
126+
122127
pub fn ansi_color(name: &str) -> Color {
123128
color(config().syntax.get(name))
124129
}

src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,23 +348,25 @@ fn main() -> Result<(), &'static str> {
348348

349349
for arg in args {
350350
let Ok(path) = fs::canonicalize(arg) else {
351+
restore_term();
351352
return Err("invalid path");
352353
};
353354

354355
let Some(path_str) = path.to_str().map(String::from) else {
356+
restore_term();
355357
return Err("invalid path");
356358
};
357359

358360
if path.is_dir() {
359361
globals.tree.add_folder(path_str);
360362
} else if let Err(err) = globals.tabs.open(&globals.syntaxes, path_str) {
363+
restore_term();
361364
println!("{err:?}");
362365
return Err("failed to open some files");
363366
}
364367
}
365368

366369
globals.run();
367-
368370
globals.interface.close();
369371

370372
Ok(())

src/tab/clipboard.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::process::Command;
2+
use crate::config::internal_clipboard;
23
use super::*;
34

45
const TMP_PATH: &str = "/tmp/hop-clipboard.txt";
@@ -17,6 +18,11 @@ impl Tab {
1718
}
1819
}
1920

21+
if internal_clipboard() {
22+
self.internal_clipboard = text;
23+
return;
24+
}
25+
2026
if let Err(error) = fs::write(TMP_PATH, text) {
2127
confirm!("failed to write clipboard-file ({TMP_PATH}):\n{error:?}");
2228
return;
@@ -31,14 +37,19 @@ impl Tab {
3137
}
3238

3339
pub fn paste(&mut self) {
34-
try_exec(false);
40+
let mut text = take(&mut self.internal_clipboard);
41+
let cursors = self.cursors.len();
3542

36-
let Ok(text) = fs::read_to_string(TMP_PATH) else {
37-
confirm!("failed to read clipboard-file ({TMP_PATH})");
38-
return;
39-
};
43+
if !internal_clipboard() {
44+
try_exec(false);
4045

41-
let cursors = self.cursors.len();
46+
let Ok(contents) = fs::read_to_string(TMP_PATH) else {
47+
confirm!("failed to read clipboard-file ({TMP_PATH})");
48+
return;
49+
};
50+
51+
text = contents;
52+
}
4253

4354
if cursors > 1 {
4455
let regions = text.split(DELIMITER).count();
@@ -106,6 +117,8 @@ fn try_exec(copy: bool) {
106117

107118
if !success {
108119
let ln1 = "failed to use wl-clipboard, xclip or macOS equivalents.";
109-
confirm!("{ln1}\nplease make sure at least one of these works.");
120+
let ln2 = "please make sure at least one of these works.";
121+
let ln3 = "alternatively, set `internal-clipboard` to `true` in config.";
122+
confirm!("{ln1}\n{ln2}\n{ln3}");
110123
}
111124
}

src/tab/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub struct DirtyLine<'a> {
3737
pub struct Tab {
3838
file_path: Option<String>,
3939
tmp_buf: String,
40+
internal_clipboard: String,
4041
name: Arc<str>,
4142
lines: Vec<Line>,
4243
scroll: isize,
@@ -148,6 +149,7 @@ impl Tab {
148149
name,
149150
lines: vec![line],
150151
scroll: 0,
152+
internal_clipboard: String::new(),
151153
cursors: vec![Cursor::new(0)],
152154
modified: false,
153155
tab_width_m1: 3,

0 commit comments

Comments
 (0)