improve ui for opening files, read stdin if no files (unless on macos :/)
This commit is contained in:
+57
-10
@@ -1,10 +1,12 @@
|
|||||||
use std::{env::{self}, io::{self}, process::exit, time::Duration};
|
use std::{env, io, path::PathBuf, process::exit, time::Duration};
|
||||||
use crossterm::{ExecutableCommand, event::{self, DisableMouseCapture, Event, KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind}, terminal::window_size};
|
use crossterm::{ExecutableCommand, event::{self, DisableMouseCapture, Event, KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind}, terminal::window_size};
|
||||||
use ratatui::{DefaultTerminal, style::Stylize, text::Span};
|
use ratatui::{DefaultTerminal, style::Stylize, text::Span};
|
||||||
use crate::{BYTES_PER_LINE, action::AppAction, buffer::Buffer, config::{Config, ConfigInitError}, cursor::Cursor};
|
use crate::{BYTES_PER_LINE, action::AppAction, buffer::Buffer, config::{Config, ConfigInitError}, cursor::Cursor};
|
||||||
|
|
||||||
mod widget;
|
mod widget;
|
||||||
|
|
||||||
|
const MACOS_STDIN_BROKEN_MESSAGE: &str = "reading from stdin on macOS does not work due to a limitation in crossterm. see https://github.com/crossterm-rs/crossterm/issues/396";
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
|
|
||||||
@@ -52,15 +54,44 @@ impl App {
|
|||||||
config.unwrap_or_default()
|
config.unwrap_or_default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let buffers: Vec<Buffer> = env::args()
|
let mut error_alert: Option<Span> = None;
|
||||||
.skip(1)
|
|
||||||
.map(Into::into)
|
|
||||||
.map(Buffer::new)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
if buffers.is_empty() {
|
let mut buffers: Vec<Buffer> = env::args()
|
||||||
println!("please provide at least one file as input");
|
.skip(1)
|
||||||
exit(1);
|
.map(Into::into)
|
||||||
|
.filter_map(|path: PathBuf| {
|
||||||
|
Buffer::from_file_at(path.clone())
|
||||||
|
.inspect_err(|error| {
|
||||||
|
error_alert = Some(
|
||||||
|
Span::raw(format!("error reading '{}': {error}", path.display())).red()
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.ok()
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if env::args().len() <= 1 {
|
||||||
|
#[cfg(target_os = "macos")] {
|
||||||
|
eprintln!("{MACOS_STDIN_BROKEN_MESSAGE}");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "macos"))] {
|
||||||
|
use io::{Read, stdin};
|
||||||
|
|
||||||
|
let mut standard_input = Vec::new();
|
||||||
|
stdin().read_to_end(&mut standard_input).unwrap();
|
||||||
|
buffers.push(Buffer::new("-".into(), standard_input));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(error_alert) = error_alert {
|
||||||
|
if buffers.is_empty() {
|
||||||
|
eprintln!("{error_alert}");
|
||||||
|
exit(1);
|
||||||
|
} else {
|
||||||
|
buffers[0].alert_message = error_alert;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let window_size = WindowSize {
|
let window_size = WindowSize {
|
||||||
@@ -98,7 +129,23 @@ impl App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_event(&mut self, terminal: &mut DefaultTerminal) {
|
pub fn handle_event(&mut self, terminal: &mut DefaultTerminal) {
|
||||||
match event::read().unwrap() {
|
let event = event::read()
|
||||||
|
.inspect_err(|error| {
|
||||||
|
#[cfg(target_os = "macos")] {
|
||||||
|
use io::ErrorKind;
|
||||||
|
|
||||||
|
if error.kind() == ErrorKind::Other {
|
||||||
|
let error_message = error.to_string();
|
||||||
|
if error_message == "Failed to initialize input reader" {
|
||||||
|
eprintln!("{MACOS_STDIN_BROKEN_MESSAGE}");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
match event {
|
||||||
Event::Resize(_, height) => {
|
Event::Resize(_, height) => {
|
||||||
self.window_size.rows = height as usize;
|
self.window_size.rows = height as usize;
|
||||||
|
|
||||||
|
|||||||
+8
-4
@@ -1,5 +1,5 @@
|
|||||||
use core::slice::GetDisjointMutIndex;
|
use core::slice::GetDisjointMutIndex;
|
||||||
use std::{collections::HashSet, fs::File, io::Read, path::PathBuf};
|
use std::{collections::HashSet, fs::File, io::{self, Read}, path::PathBuf};
|
||||||
use crossterm::event::KeyEvent;
|
use crossterm::event::KeyEvent;
|
||||||
use ratatui::{layout::{Constraint, Rect}, style::{Color, Style, Stylize}, text::Span, widgets::{Block, Borders, Clear, Widget}};
|
use ratatui::{layout::{Constraint, Rect}, style::{Color, Style, Stylize}, text::Span, widgets::{Block, Borders, Clear, Widget}};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@@ -171,11 +171,15 @@ impl Widget for Popup {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Buffer {
|
impl Buffer {
|
||||||
pub fn new(file_path: PathBuf) -> Self {
|
pub fn from_file_at(file_path: PathBuf) -> io::Result<Self> {
|
||||||
let file = File::open(&file_path);
|
let mut file = File::open(&file_path)?;
|
||||||
let mut contents = Vec::new();
|
let mut contents = Vec::new();
|
||||||
file.unwrap().read_to_end(&mut contents).unwrap();
|
file.read_to_end(&mut contents)?;
|
||||||
|
|
||||||
|
Ok(Self::new(file_path, contents))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new(file_path: PathBuf, contents: Vec<u8>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
file_name: file_path.file_name().unwrap().to_str().unwrap().to_owned(),
|
file_name: file_path.file_name().unwrap().to_str().unwrap().to_owned(),
|
||||||
file_path,
|
file_path,
|
||||||
|
|||||||
Reference in New Issue
Block a user