feat(development): add --debug flag (#41)
* wip: #39 create a -d --debug flag * fix: switch join with concat
This commit is contained in:
parent
5a88b1a6ee
commit
20617c0263
2 changed files with 43 additions and 7 deletions
15
src/main.rs
15
src/main.rs
|
|
@ -42,7 +42,10 @@ pub struct Opt {
|
||||||
open_file: Option<PathBuf>,
|
open_file: Option<PathBuf>,
|
||||||
#[structopt(long)]
|
#[structopt(long)]
|
||||||
/// Maximum panes on screen, caution: opening more panes will close old ones
|
/// 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) {
|
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();
|
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() {
|
pub fn main() {
|
||||||
let os_input = get_os_input();
|
let os_input = get_os_input();
|
||||||
let opts = Opt::from_args();
|
let opts = Opt::from_args();
|
||||||
|
|
@ -91,13 +100,15 @@ pub enum AppInstruction {
|
||||||
pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
|
pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
|
||||||
let mut active_threads = vec![];
|
let mut active_threads = vec![];
|
||||||
|
|
||||||
|
delete_log_files().unwrap();
|
||||||
|
|
||||||
let full_screen_ws = os_input.get_terminal_size_using_fd(0);
|
let full_screen_ws = os_input.get_terminal_size_using_fd(0);
|
||||||
os_input.into_raw_mode(0);
|
os_input.into_raw_mode(0);
|
||||||
let (send_screen_instructions, receive_screen_instructions): (Sender<ScreenInstruction>, Receiver<ScreenInstruction>) = channel();
|
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_pty_instructions, receive_pty_instructions): (Sender<PtyInstruction>, Receiver<PtyInstruction>) = channel();
|
||||||
let (send_app_instructions, receive_app_instructions): (Sender<AppInstruction>, Receiver<AppInstruction>) = 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 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(
|
active_threads.push(
|
||||||
thread::Builder::new()
|
thread::Builder::new()
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,26 @@ fn _debug_log_to_file (message: String) {
|
||||||
file.write_all("\n".as_bytes()).unwrap();
|
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 {
|
impl Stream for ReadFromPid {
|
||||||
type Item = Vec<u8>;
|
type Item = Vec<u8>;
|
||||||
fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
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 receive_pty_instructions: Receiver<PtyInstruction>,
|
||||||
pub id_to_child_pid: HashMap<RawFd, RawFd>,
|
pub id_to_child_pid: HashMap<RawFd, RawFd>,
|
||||||
os_input: Box<dyn OsApi>,
|
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({
|
task::spawn({
|
||||||
async move {
|
async move {
|
||||||
let mut vte_parser = vte::Parser::new();
|
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 {
|
while let Some(bytes) = terminal_bytes.next().await {
|
||||||
let bytes_is_empty = bytes.is_empty();
|
let bytes_is_empty = bytes.is_empty();
|
||||||
for byte in bytes {
|
for byte in bytes {
|
||||||
|
if debug {
|
||||||
|
debug_to_file(byte, pid);
|
||||||
|
}
|
||||||
vte_parser.advance(&mut vte_event_sender, byte);
|
vte_parser.advance(&mut vte_event_sender, byte);
|
||||||
}
|
}
|
||||||
if !bytes_is_empty {
|
if !bytes_is_empty {
|
||||||
|
|
@ -208,29 +232,30 @@ fn stream_terminal_bytes(pid: RawFd, send_screen_instructions: Sender<ScreenInst
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PtyBus {
|
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 {
|
PtyBus {
|
||||||
send_screen_instructions,
|
send_screen_instructions,
|
||||||
receive_pty_instructions,
|
receive_pty_instructions,
|
||||||
os_input,
|
os_input,
|
||||||
id_to_child_pid: HashMap::new(),
|
id_to_child_pid: HashMap::new(),
|
||||||
|
debug_to_file
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn spawn_terminal(&mut self, file_to_open: Option<PathBuf>) {
|
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);
|
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.id_to_child_pid.insert(pid_primary, pid_secondary);
|
||||||
self.send_screen_instructions.send(ScreenInstruction::NewPane(pid_primary)).unwrap();
|
self.send_screen_instructions.send(ScreenInstruction::NewPane(pid_primary)).unwrap();
|
||||||
}
|
}
|
||||||
pub fn spawn_terminal_vertically(&mut self, file_to_open: Option<PathBuf>) {
|
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);
|
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.id_to_child_pid.insert(pid_primary, pid_secondary);
|
||||||
self.send_screen_instructions.send(ScreenInstruction::VerticalSplit(pid_primary)).unwrap();
|
self.send_screen_instructions.send(ScreenInstruction::VerticalSplit(pid_primary)).unwrap();
|
||||||
}
|
}
|
||||||
pub fn spawn_terminal_horizontally(&mut self, file_to_open: Option<PathBuf>) {
|
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);
|
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.id_to_child_pid.insert(pid_primary, pid_secondary);
|
||||||
self.send_screen_instructions.send(ScreenInstruction::HorizontalSplit(pid_primary)).unwrap();
|
self.send_screen_instructions.send(ScreenInstruction::HorizontalSplit(pid_primary)).unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue