mirror of
https://github.com/helix-editor/helix.git
synced 2025-01-19 13:37:06 +04:00
Allow piping from stdin into a buffer on startup (#996)
* Allow piping from stdin into a buffer on startup * Refactor * Don't allow piping into new buffer on macOS * Update helix-term/src/application.rs Co-authored-by: Blaž Hrastnik <blaz@mxxn.io> * Update helix-term/src/application.rs Co-authored-by: Blaž Hrastnik <blaz@mxxn.io> * Fix Co-authored-by: Blaž Hrastnik <blaz@mxxn.io>
This commit is contained in:
parent
68224232af
commit
cf831b1a65
@ -7,7 +7,7 @@
|
||||
use log::{error, warn};
|
||||
|
||||
use std::{
|
||||
io::{stdout, Write},
|
||||
io::{stdin, stdout, Write},
|
||||
sync::Arc,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
@ -17,6 +17,7 @@
|
||||
use crossterm::{
|
||||
event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream},
|
||||
execute, terminal,
|
||||
tty::IsTty,
|
||||
};
|
||||
#[cfg(not(windows))]
|
||||
use {
|
||||
@ -134,8 +135,17 @@ pub fn new(args: Args, mut config: Config) -> Result<Self, Error> {
|
||||
}
|
||||
editor.set_status(format!("Loaded {} files.", nr_of_files));
|
||||
}
|
||||
} else {
|
||||
} else if stdin().is_tty() {
|
||||
editor.new_file(Action::VerticalSplit);
|
||||
} else if cfg!(target_os = "macos") {
|
||||
// On Linux and Windows, we allow the output of a command to be piped into the new buffer.
|
||||
// This doesn't currently work on macOS because of the following issue:
|
||||
// https://github.com/crossterm-rs/crossterm/issues/500
|
||||
anyhow::bail!("Piping into helix-term is currently not supported on macOS");
|
||||
} else {
|
||||
editor
|
||||
.new_file_from_stdin(Action::VerticalSplit)
|
||||
.unwrap_or_else(|_| editor.new_file(Action::VerticalSplit));
|
||||
}
|
||||
|
||||
editor.set_theme(theme);
|
||||
|
@ -9,6 +9,7 @@
|
||||
use futures_util::future;
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
io::stdin,
|
||||
path::{Path, PathBuf},
|
||||
pin::Pin,
|
||||
sync::Arc,
|
||||
@ -314,16 +315,24 @@ pub fn switch(&mut self, id: DocumentId, action: Action) {
|
||||
self._refresh();
|
||||
}
|
||||
|
||||
pub fn new_file(&mut self, action: Action) -> DocumentId {
|
||||
fn new_file_from_document(&mut self, action: Action, mut document: Document) -> DocumentId {
|
||||
let id = DocumentId(self.next_document_id);
|
||||
self.next_document_id += 1;
|
||||
let mut doc = Document::default();
|
||||
doc.id = id;
|
||||
self.documents.insert(id, doc);
|
||||
document.id = id;
|
||||
self.documents.insert(id, document);
|
||||
self.switch(id, action);
|
||||
id
|
||||
}
|
||||
|
||||
pub fn new_file(&mut self, action: Action) -> DocumentId {
|
||||
self.new_file_from_document(action, Document::default())
|
||||
}
|
||||
|
||||
pub fn new_file_from_stdin(&mut self, action: Action) -> Result<DocumentId, Error> {
|
||||
let (rope, encoding) = crate::document::from_reader(&mut stdin(), None)?;
|
||||
Ok(self.new_file_from_document(action, Document::from(rope, Some(encoding))))
|
||||
}
|
||||
|
||||
pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
|
||||
let path = helix_core::path::get_canonicalized_path(&path)?;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user