on master help sign in
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>

commitcf831b1a65625f29d6e1bc12483a45c1adc8dff4 authorJason Hansen <jasonrodneyhansen@gmail.com> dateTue 9 Nov 2021, 18:53 (5 years ago) parent6822423
2 files changed+25−6
helix-term/src/application.rs+12−2 helix-view/src/editor.rs+13−4
helix-term/src/application.rs+12−2
@@ -7,7 +7,7 @@ use crate::{args::Args, compositor::Compositor, config::Config, job::Jobs, ui};
77use log::{error, warn};
88
99use std::{
10− io::{stdout, Write},
10+ io::{stdin, stdout, Write},
1111 sync::Arc,
1212 time::{Duration, Instant},
1313};
@@ -17,6 +17,7 @@ use anyhow::Error;
1717use crossterm::{
1818 event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream},
1919 execute, terminal,
20+ tty::IsTty,
2021};
2122#[cfg(not(windows))]
2223use {
@@ -134,8 +135,17 @@ impl Application {
134135 }
135136 editor.set_status(format!("Loaded {} files.", nr_of_files));
136137 }
137− } else {
138+ } else if stdin().is_tty() {
138139 editor.new_file(Action::VerticalSplit);
140+ } else if cfg!(target_os = "macos") {
141+ // On Linux and Windows, we allow the output of a command to be piped into the new buffer.
142+ // This doesn't currently work on macOS because of the following issue:
143+ // https://github.com/crossterm-rs/crossterm/issues/500
144+ anyhow::bail!("Piping into helix-term is currently not supported on macOS");
145+ } else {
146+ editor
147+ .new_file_from_stdin(Action::VerticalSplit)
148+ .unwrap_or_else(|_| editor.new_file(Action::VerticalSplit));
139149 }
140150
141151 editor.set_theme(theme);
helix-view/src/editor.rs+13−4
@@ -9,6 +9,7 @@ use crate::{
99use futures_util::future;
1010use std::{
1111 collections::BTreeMap,
12+ io::stdin,
1213 path::{Path, PathBuf},
1314 pin::Pin,
1415 sync::Arc,
@@ -314,16 +315,24 @@ impl Editor {
314315 self._refresh();
315316 }
316317
317− pub fn new_file(&mut self, action: Action) -> DocumentId {
318+ fn new_file_from_document(&mut self, action: Action, mut document: Document) -> DocumentId {
318319 let id = DocumentId(self.next_document_id);
319320 self.next_document_id += 1;
320− let mut doc = Document::default();
321− doc.id = id;
322− self.documents.insert(id, doc);
321+ document.id = id;
322+ self.documents.insert(id, document);
323323 self.switch(id, action);
324324 id
325325 }
326326
327+ pub fn new_file(&mut self, action: Action) -> DocumentId {
328+ self.new_file_from_document(action, Document::default())
329+ }
330+
331+ pub fn new_file_from_stdin(&mut self, action: Action) -> Result<DocumentId, Error> {
332+ let (rope, encoding) = crate::document::from_reader(&mut stdin(), None)?;
333+ Ok(self.new_file_from_document(action, Document::from(rope, Some(encoding))))
334+ }
335+
327336 pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
328337 let path = helix_core::path::get_canonicalized_path(&path)?;
329338