Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow piping from stdin into a buffer on startup #996

Merged
merged 6 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -17,6 +17,7 @@ use anyhow::Error;
use crossterm::{
event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream},
execute, terminal,
tty::IsTty,
};
#[cfg(not(windows))]
use {
Expand Down Expand Up @@ -122,8 +123,19 @@ impl Application {
}
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
return Err(anyhow::anyhow!(
"Piping into helix-term is currently not supported on macOS"
));
jasonrhansen marked this conversation as resolved.
Show resolved Hide resolved
} else {
if let Err(_) = editor.new_file_from_stdin(Action::VerticalSplit) {
editor.new_file(Action::VerticalSplit);
}
jasonrhansen marked this conversation as resolved.
Show resolved Hide resolved
}

editor.set_theme(theme);
Expand Down
17 changes: 13 additions & 4 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
use futures_util::future;
use std::{
collections::BTreeMap,
io::stdin,
path::{Path, PathBuf},
pin::Pin,
sync::Arc,
Expand Down Expand Up @@ -308,16 +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 = 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)?;

Expand Down