on master help sign in
Allow piping from stdin into a buffer on startup (#996)
cf831b1Jason Hansen
helix-term/src/application.rs+12−2
@@ -7,7 +7,7 @@ use crate::{args::Args, compositor::Compositor, config::Config, job::Jobs, ui};
7use log::{error, warn};
7use log::{error, warn};
8
8
9use std::{
9use std::{
10− io::{stdout, Write},
10+ io::{stdin, stdout, Write},
11 sync::Arc,
11 sync::Arc,
12 time::{Duration, Instant},
12 time::{Duration, Instant},
13};
13};
@@ -17,6 +17,7 @@ use anyhow::Error;
17use crossterm::{
17use crossterm::{
18 event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream},
18 event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream},
19 execute, terminal,
19 execute, terminal,
20+ tty::IsTty,
20};
21};
21#[cfg(not(windows))]
22#[cfg(not(windows))]
22use {
23use {
@@ -134,8 +135,17 @@ impl Application {
134 }
135 }
135 editor.set_status(format!("Loaded {} files.", nr_of_files));
136 editor.set_status(format!("Loaded {} files.", nr_of_files));
136 }
137 }
137− } else {
138+ } else if stdin().is_tty() {
138 editor.new_file(Action::VerticalSplit);
139 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));
139 }
149 }
140
150
141 editor.set_theme(theme);
151 editor.set_theme(theme);
helix-view/src/editor.rs+13−4
@@ -9,6 +9,7 @@ use crate::{
9use futures_util::future;
9use futures_util::future;
10use std::{
10use std::{
11 collections::BTreeMap,
11 collections::BTreeMap,
12+ io::stdin,
12 path::{Path, PathBuf},
13 path::{Path, PathBuf},
13 pin::Pin,
14 pin::Pin,
14 sync::Arc,
15 sync::Arc,
@@ -314,16 +315,24 @@ impl Editor {
314 self._refresh();
315 self._refresh();
315 }
316 }
316
317
317− pub fn new_file(&mut self, action: Action) -> DocumentId {
318+ fn new_file_from_document(&mut self, action: Action, mut document: Document) -> DocumentId {
318 let id = DocumentId(self.next_document_id);
319 let id = DocumentId(self.next_document_id);
319 self.next_document_id += 1;
320 self.next_document_id += 1;
320− let mut doc = Document::default();
321− doc.id = id;
321+ document.id = id;
322− self.documents.insert(id, doc);
322+ self.documents.insert(id, document);
323 self.switch(id, action);
323 self.switch(id, action);
324 id
324 id
325 }
325 }
326
326
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+
327 pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
336 pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
328 let path = helix_core::path::get_canonicalized_path(&path)?;
337 let path = helix_core::path::get_canonicalized_path(&path)?;
329
338