Skip to content

Commit e703b2c

Browse files
committed
Batching for open and read
1 parent 6ef05d7 commit e703b2c

6 files changed

Lines changed: 54 additions & 34 deletions

File tree

src/cmd/open.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::process::{Command, Stdio};
33

44
use crate::cmd::prelude::*;
55
use crate::paper::PaperList;
6-
use crate::utils::{confirm, expand_tilde_string};
6+
use crate::utils::confirm;
77

88
pub static MAN: &'static str = "Usage:
99
1) alone: open [filter]
@@ -61,9 +61,10 @@ pub fn execute(
6161
// Some reports.
6262
let num_open = files.len();
6363
println!(
64-
"Skipping papers without filepaths ({} out of {}).",
64+
"{} {} selected. Skipping {} without file paths.",
65+
num_papers,
66+
if num_papers > 1 { "papers" } else { "paper" },
6567
num_papers - num_open,
66-
num_papers
6768
);
6869

6970
// Ask for confirmation.
@@ -72,37 +73,54 @@ pub fn execute(
7273
}
7374

7475
// Open papers.
75-
let mut selected = Vec::new();
7676
if config.output.viewer_batch {
77-
// let command = build_viewer_command(files, config);
78-
// spawn(command, // TODO: handle batched.
77+
let (selected, files): (Vec<usize>, Vec<PathBuf>) = files.into_iter().unzip();
78+
if spawn(build_viewer_command(files.as_ref(), config)) {
79+
Ok(CommandOutput::Papers(PaperList(selected)))
80+
} else {
81+
Ok(CommandOutput::Papers(PaperList(Vec::new())))
82+
}
7983
} else {
84+
let mut selected = Vec::new();
8085
for (i, file) in files.into_iter() {
81-
let command = build_viewer_command(&[file], config);
82-
spawn(command, i, &mut selected);
86+
if spawn(build_viewer_command(&[file], config)) {
87+
selected.push(i);
88+
}
8389
}
90+
Ok(CommandOutput::Papers(PaperList(selected)))
8491
}
85-
86-
Ok(CommandOutput::Papers(PaperList(selected)))
8792
}
8893

89-
fn spawn(mut command: Command, i: usize, selected: &mut Vec<usize>) {
94+
fn spawn(mut command: Command) -> bool {
9095
match command.spawn() {
91-
Ok(_) => selected.push(i),
96+
Ok(_) => true,
9297
Err(e) => {
9398
if matches!(e.kind(), std::io::ErrorKind::NotFound) {
9499
println!("Invalid editor command: '{:?}'", e);
95100
} else {
96101
println!("Failed to spawn subprocess: '{:?}'", e);
97102
}
103+
false
98104
}
99105
}
100106
}
101107

102108
fn build_viewer_command(files: &[PathBuf], config: &Config) -> Command {
103-
let command = &config.output.viewer_command;
104-
let mut ret = Command::new(&command[0]);
105-
ret.args(&command[1..]).args(files);
106-
ret.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null());
109+
let mut ret = Command::new(&config.output.viewer_command[0]);
110+
let mut curly = false;
111+
for command in &config.output.viewer_command[1..] {
112+
if command == "{}" {
113+
ret.args(files);
114+
curly = true;
115+
} else {
116+
ret.arg(command);
117+
}
118+
}
119+
if !curly {
120+
ret.args(files);
121+
}
122+
ret.stdin(Stdio::null())
123+
.stdout(Stdio::null())
124+
.stderr(Stdio::null());
107125
ret
108126
}

src/cmd/read.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ pub fn execute(
7070
fn spawn(mut command: Command, block: bool) {
7171
match command.spawn() {
7272
Ok(mut handle) => {
73-
if !block { return }
73+
if !block {
74+
return;
75+
}
7476
if let Err(e) = handle.wait() {
7577
println!("Failed to wait subprocess: {}", e);
7678
}

src/cmd/touch.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
use std::path::PathBuf;
2-
31
use crate::cmd::prelude::*;
42
use crate::paper::{Paper, PaperList};
5-
use crate::utils::expand_tilde_string;
63

74
pub static MAN: &'static str = "Usage: touch [paper]
85
@@ -34,12 +31,8 @@ pub fn execute(
3431
let paper = Paper::from_args(input.args)?;
3532

3633
// Verify file path.
37-
if let Some(filepath) = &paper.filepath {
38-
if let Some(base) = &config.storage.file_base_dir {
39-
40-
}
41-
let path = expand_tilde_string(&filepath)?;
42-
if !PathBuf::from(path).exists() {
34+
if let Some(filepath) = paper.abs_filepath(config)? {
35+
if !filepath.exists() {
4336
return Err(Fallacy::PathDoesNotExist(filepath.to_owned()));
4437
}
4538
}

src/config.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ with default settings.
4040
(default: ['title', 'first author', 'venue', 'year'])
4141
- viewer_command: Command to use for the viewer to open
4242
papers. It is assumed that the viewer program is a
43-
non-command line program.
43+
non-command line program. If you place a set of curly
44+
braces ('{}') in the list, the path to the file(s) will
45+
be substituted in that location. Otherwise, the path(s)
46+
will be placed at the end.
4447
(default: ['zathura'])
4548
- viewer_batch: Whether to open multiple papers with a
4649
single invocation of the viewer command. If true, the
@@ -50,7 +53,10 @@ with default settings.
5053
(default: false)
5154
- editor_command: Command to use for the editor to edit
5255
notes. It is assumed that the editor is a command line
53-
program.
56+
program. If you place a set of curly braces ('{}') in
57+
the list, the path to the file(s) will be substituted
58+
in that location. Otherwise, the path(s) will be placed
59+
at the end.
5460
(default: ['vim', '-p'])
5561
- editor_batch: Whether to open multiple notes with a
5662
single invocation of the editor command. If true, the
@@ -137,15 +143,19 @@ impl OutputConfig {
137143

138144
// Check viewer command and expand tilde.
139145
if self.viewer_command.len() == 0 {
140-
return Err(Fallacy::ConfigAuditError("Viewer command cannot be empty.".to_owned()));
146+
return Err(Fallacy::ConfigAuditError(
147+
"Viewer command cannot be empty.".to_owned(),
148+
));
141149
}
142150
for path in self.viewer_command.iter_mut() {
143151
*path = expand_tilde_string(path)?;
144152
}
145153

146154
// Check editor command and expand tilde.
147155
if self.editor_command.len() == 0 {
148-
return Err(Fallacy::ConfigAuditError("Editor command cannot be empty.".to_owned()));
156+
return Err(Fallacy::ConfigAuditError(
157+
"Editor command cannot be empty.".to_owned(),
158+
));
149159
}
150160
for path in self.editor_command.iter_mut() {
151161
*path = expand_tilde_string(path)?;

src/error.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,9 @@ pub enum Fallacy {
4545
PaperMissingFields(String),
4646
// path
4747
#[error("Specified file path does not exist: '{0}'")]
48-
PathDoesNotExist(String),
48+
PathDoesNotExist(PathBuf),
4949
#[error("Invalid UTF-8 character in path: '{0}'")]
5050
PathInvalidUTF8(PathBuf),
51-
#[error("'{0}' is not executable.")]
52-
PathNotExecutable(PathBuf),
5351
#[error("Cannot resolve relative path '{0}' because the base directory was not given. See 'base_dir' in `man config`.")]
5452
PathRelativeWithoutBase(PathBuf),
5553
// exit

src/utils.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::io::Write;
22
use std::path::PathBuf;
3-
use std::process::Command;
43

54
use crate::error::Fallacy;
65

0 commit comments

Comments
 (0)