From 13c8d5cc9ec69dfaad2c77f92c265524e8a1cc4a Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Sat, 6 Nov 2021 13:04:32 -0600 Subject: [PATCH 1/6] Allow piping from stdin into a buffer on startup --- helix-view/src/editor.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 17cd3d7b58e6..2bde3ef47527 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -6,9 +6,11 @@ use crate::{ Document, DocumentId, View, ViewId, }; +use crossterm::tty::IsTty; use futures_util::future; use std::{ collections::BTreeMap, + io::stdin, path::{Path, PathBuf}, pin::Pin, sync::Arc, @@ -311,7 +313,13 @@ impl Editor { pub fn new_file(&mut self, action: Action) -> DocumentId { let id = DocumentId(self.next_document_id); self.next_document_id += 1; - let mut doc = Document::default(); + let mut doc = if stdin().is_tty() { + Document::default() + } else if let Ok((rope, encoding)) = crate::document::from_reader(&mut stdin(), None) { + Document::from(rope, Some(encoding)) + } else { + Document::default() + }; doc.id = id; self.documents.insert(id, doc); self.switch(id, action); From e8db390506964898c34793e320e924c70d2e2694 Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Sat, 6 Nov 2021 16:10:20 -0600 Subject: [PATCH 2/6] Refactor --- helix-term/src/application.rs | 9 +++++++-- helix-view/src/editor.rs | 23 ++++++++++++----------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 6037148f6b70..6a0f73fc1a08 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -7,7 +7,7 @@ use crate::{args::Args, compositor::Compositor, config::Config, job::Jobs, ui}; use log::{error, warn}; use std::{ - io::{stdout, Write}, + io::{stdin, stdout, Write}, sync::Arc, time::{Duration, Instant}, }; @@ -17,6 +17,7 @@ use anyhow::Error; use crossterm::{ event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream}, execute, terminal, + tty::IsTty, }; #[cfg(not(windows))] use { @@ -122,8 +123,12 @@ impl Application { } editor.set_status(format!("Loaded {} files.", nr_of_files)); } - } else { + } else if stdin().is_tty() { editor.new_file(Action::VerticalSplit); + } else { + if let Err(_) = editor.new_file_from_stdin(Action::VerticalSplit) { + editor.new_file(Action::VerticalSplit); + } } editor.set_theme(theme); diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 2bde3ef47527..f27c03e9e197 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -6,7 +6,6 @@ use crate::{ Document, DocumentId, View, ViewId, }; -use crossterm::tty::IsTty; use futures_util::future; use std::{ collections::BTreeMap, @@ -310,22 +309,24 @@ impl Editor { 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 = if stdin().is_tty() { - Document::default() - } else if let Ok((rope, encoding)) = crate::document::from_reader(&mut stdin(), None) { - Document::from(rope, Some(encoding)) - } else { - 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 { + 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 { let path = helix_core::path::get_canonicalized_path(&path)?; From 52f611af84db5fd40f658a2796631ea9db5734fe Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Sat, 6 Nov 2021 18:45:31 -0600 Subject: [PATCH 3/6] Don't allow piping into new buffer on macOS --- helix-term/src/application.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 6a0f73fc1a08..ab919fec4699 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -125,6 +125,13 @@ impl Application { } } 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 + return Err(anyhow::anyhow!( + "Piping into helix-term is currently not supported on macOS" + )); } else { if let Err(_) = editor.new_file_from_stdin(Action::VerticalSplit) { editor.new_file(Action::VerticalSplit); From a18d5490cad99f58a772bd3c14bc8e81f7809334 Mon Sep 17 00:00:00 2001 From: Jason Hansen Date: Sat, 6 Nov 2021 20:05:58 -0600 Subject: [PATCH 4/6] Update helix-term/src/application.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Blaž Hrastnik --- helix-term/src/application.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index ab919fec4699..d830e3dbbba5 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -129,9 +129,7 @@ impl Application { // 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 - return Err(anyhow::anyhow!( - "Piping into helix-term is currently not supported on macOS" - )); + anyhow::bail!("Piping into helix-term is currently not supported on macOS"); } else { if let Err(_) = editor.new_file_from_stdin(Action::VerticalSplit) { editor.new_file(Action::VerticalSplit); From 694131198706079130169454ac87d1369ad7bf44 Mon Sep 17 00:00:00 2001 From: Jason Hansen Date: Sat, 6 Nov 2021 20:06:03 -0600 Subject: [PATCH 5/6] Update helix-term/src/application.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Blaž Hrastnik --- helix-term/src/application.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index d830e3dbbba5..82976f65c02a 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -131,9 +131,7 @@ impl Application { // https://github.com/crossterm-rs/crossterm/issues/500 anyhow::bail!("Piping into helix-term is currently not supported on macOS"); } else { - if let Err(_) = editor.new_file_from_stdin(Action::VerticalSplit) { - editor.new_file(Action::VerticalSplit); - } + editor.new_file_from_stdin(Action::VerticalSplit).unwrap_or_else(|| editor.new_file(Action::VerticalSplit)) } editor.set_theme(theme); From 81e8040cd7f855b216b043cf64592928c02a45fc Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Sat, 6 Nov 2021 20:11:25 -0600 Subject: [PATCH 6/6] Fix --- helix-term/src/application.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 82976f65c02a..7428d5a73707 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -131,7 +131,9 @@ impl Application { // 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 + .new_file_from_stdin(Action::VerticalSplit) + .unwrap_or_else(|_| editor.new_file(Action::VerticalSplit)); } editor.set_theme(theme);