feat(development): add --debug flag (#41)

* wip: #39 create a -d --debug flag

* fix: switch join with concat
This commit is contained in:
Denis Maximov 2020-11-13 17:44:24 +02:00 committed by GitHub
parent 5a88b1a6ee
commit 20617c0263
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 7 deletions

View file

@ -42,7 +42,10 @@ pub struct Opt {
open_file: Option<PathBuf>,
#[structopt(long)]
/// Maximum panes on screen, caution: opening more panes will close old ones
max_panes: Option<usize>
max_panes: Option<usize>,
#[structopt(short, long)]
debug: bool
}
fn _debug_log_to_file (message: String) {
@ -53,6 +56,12 @@ fn _debug_log_to_file (message: String) {
file.write_all("\n".as_bytes()).unwrap();
}
fn delete_log_files() -> std::io::Result<()> {
std::fs::remove_dir_all("/tmp/mosaic-logs").ok();
std::fs::create_dir_all("/tmp/mosaic-logs").ok();
Ok(())
}
pub fn main() {
let os_input = get_os_input();
let opts = Opt::from_args();
@ -91,13 +100,15 @@ pub enum AppInstruction {
pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
let mut active_threads = vec![];
delete_log_files().unwrap();
let full_screen_ws = os_input.get_terminal_size_using_fd(0);
os_input.into_raw_mode(0);
let (send_screen_instructions, receive_screen_instructions): (Sender<ScreenInstruction>, Receiver<ScreenInstruction>) = channel();
let (send_pty_instructions, receive_pty_instructions): (Sender<PtyInstruction>, Receiver<PtyInstruction>) = channel();
let (send_app_instructions, receive_app_instructions): (Sender<AppInstruction>, Receiver<AppInstruction>) = channel();
let mut screen = Screen::new(receive_screen_instructions, send_pty_instructions.clone(), send_app_instructions.clone(), &full_screen_ws, os_input.clone(), opts.max_panes);
let mut pty_bus = PtyBus::new(receive_pty_instructions, send_screen_instructions.clone(), os_input.clone());
let mut pty_bus = PtyBus::new(receive_pty_instructions, send_screen_instructions.clone(), os_input.clone(), opts.debug);
active_threads.push(
thread::Builder::new()

View file

@ -34,6 +34,26 @@ fn _debug_log_to_file (message: String) {
file.write_all("\n".as_bytes()).unwrap();
}
fn debug_to_file(message: u8, pid: RawFd) {
use std::fs::OpenOptions;
use std::io::prelude::*;
let mut path = PathBuf::new();
path.push(
[
String::from("/tmp/mosaic-logs/mosaic-"),
pid.to_string(),
String::from(".log"),
]
.concat(),
);
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open(path)
.unwrap();
file.write_all(&[message]).unwrap();
}
impl Stream for ReadFromPid {
type Item = Vec<u8>;
fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
@ -147,9 +167,10 @@ pub struct PtyBus {
pub receive_pty_instructions: Receiver<PtyInstruction>,
pub id_to_child_pid: HashMap<RawFd, RawFd>,
os_input: Box<dyn OsApi>,
debug_to_file: bool
}
fn stream_terminal_bytes(pid: RawFd, send_screen_instructions: Sender<ScreenInstruction>, os_input: Box<dyn OsApi>) {
fn stream_terminal_bytes(pid: RawFd, send_screen_instructions: Sender<ScreenInstruction>, os_input: Box<dyn OsApi>, debug: bool) {
task::spawn({
async move {
let mut vte_parser = vte::Parser::new();
@ -163,6 +184,9 @@ fn stream_terminal_bytes(pid: RawFd, send_screen_instructions: Sender<ScreenInst
while let Some(bytes) = terminal_bytes.next().await {
let bytes_is_empty = bytes.is_empty();
for byte in bytes {
if debug {
debug_to_file(byte, pid);
}
vte_parser.advance(&mut vte_event_sender, byte);
}
if !bytes_is_empty {
@ -208,29 +232,30 @@ fn stream_terminal_bytes(pid: RawFd, send_screen_instructions: Sender<ScreenInst
}
impl PtyBus {
pub fn new (receive_pty_instructions: Receiver<PtyInstruction>, send_screen_instructions: Sender<ScreenInstruction>, os_input: Box<dyn OsApi>) -> Self {
pub fn new (receive_pty_instructions: Receiver<PtyInstruction>, send_screen_instructions: Sender<ScreenInstruction>, os_input: Box<dyn OsApi>, debug_to_file: bool) -> Self {
PtyBus {
send_screen_instructions,
receive_pty_instructions,
os_input,
id_to_child_pid: HashMap::new(),
debug_to_file
}
}
pub fn spawn_terminal(&mut self, file_to_open: Option<PathBuf>) {
let (pid_primary, pid_secondary): (RawFd, RawFd) = self.os_input.spawn_terminal(file_to_open);
stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone());
stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone(), self.debug_to_file);
self.id_to_child_pid.insert(pid_primary, pid_secondary);
self.send_screen_instructions.send(ScreenInstruction::NewPane(pid_primary)).unwrap();
}
pub fn spawn_terminal_vertically(&mut self, file_to_open: Option<PathBuf>) {
let (pid_primary, pid_secondary): (RawFd, RawFd) = self.os_input.spawn_terminal(file_to_open);
stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone());
stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone(), self.debug_to_file);
self.id_to_child_pid.insert(pid_primary, pid_secondary);
self.send_screen_instructions.send(ScreenInstruction::VerticalSplit(pid_primary)).unwrap();
}
pub fn spawn_terminal_horizontally(&mut self, file_to_open: Option<PathBuf>) {
let (pid_primary, pid_secondary): (RawFd, RawFd) = self.os_input.spawn_terminal(file_to_open);
stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone());
stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone(), self.debug_to_file);
self.id_to_child_pid.insert(pid_primary, pid_secondary);
self.send_screen_instructions.send(ScreenInstruction::HorizontalSplit(pid_primary)).unwrap();
}