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

Somehow my console gets randomly cleared a right after the programm startup #863

Open
Outfluencer opened this issue Dec 24, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Outfluencer
Copy link

Outfluencer commented Dec 24, 2024

Platform Windows
Terminal software idk?

reedline = { version = "0.38.0", features = ["external_printer"] }

I am on windows cmd.
Right after i start my test programm my console is cleared, in rust rover its not cleared, i think it doesnt support to be cleared

Steps to reproduce

i am not really sure how its caused but i will provide my code

Screenshots/Screencaptures

Desktop.2024.12.24.-.23.44.52.01.mp4

my code


use crate::plugin::api::API;
use crate::server::command::CommandSender;
use crate::server::ProxyServer;
use core::str;
use env_logger::{Builder, Target, WriteStyle};
use log::error;
use reedline::{
    EditMode, Emacs, ExternalPrinter, Prompt, PromptEditMode, PromptHistorySearch,
    PromptHistorySearchStatus, Reedline, ReedlineEvent, ReedlineRawEvent, Signal,
};
use std::borrow::Cow;
use std::fmt::Arguments;
use std::io;
use std::io::Write;
use std::sync::{Mutex, RwLock};

pub mod auth;
pub mod chat;
pub mod haproxy;
pub mod plugin;
pub mod server;
pub mod util;
pub mod version;

/********** pipe all the writes ***********/
struct SharedWriter {
    printer: Box<ExternalPrinter<String>>,
}
impl Write for SharedWriter {
    fn write(&mut self, _: &[u8]) -> io::Result<usize> {
        unreachable!("write");
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }

    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
        let str = String::from_utf8_lossy(buf);
        self.printer
            .print(str.to_string())
            .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
        Ok(())
    }

    fn write_fmt(&mut self, _: Arguments<'_>) -> io::Result<()> {
        unreachable!("write_fmt");
    }
}
/********** pipe all the writes ***********/

/**********    custom prompt    ***********/
struct CrustPrompt;

impl Prompt for CrustPrompt {
    fn render_prompt_left(&self) -> Cow<str> {
        Cow::Borrowed("")
    }

    fn render_prompt_right(&self) -> Cow<str> {
        Cow::Borrowed("")
    }

    fn render_prompt_indicator(&self, _: PromptEditMode) -> Cow<str> {
        "> ".into()
    }

    fn render_prompt_multiline_indicator(&self) -> Cow<str> {
        Cow::Borrowed(":::")
    }

    fn render_prompt_history_search_indicator(
        &self,
        history_search: PromptHistorySearch,
    ) -> Cow<str> {
        let prefix = match history_search.status {
            PromptHistorySearchStatus::Passing => "",
            PromptHistorySearchStatus::Failing => "failing ",
        };
        Cow::Owned(format!(
            "({}reverse-search: {}) ",
            prefix, history_search.term
        ))
    }
}

struct EmacsWrapper {
    emacs: Emacs,
}
impl EditMode for EmacsWrapper {
    fn parse_event(&mut self, event: ReedlineRawEvent) -> ReedlineEvent {
        let event = self.emacs.parse_event(event);
        match event {
            ReedlineEvent::ClearScreen => {
                unreachable!("CLEAR SCREEN");
                panic!("CLEAR SCREEN");
            }
            _ => event,
        }
    }

    fn edit_mode(&self) -> PromptEditMode {
        PromptEditMode::Custom(">".to_owned())
    }
}

fn main() {
    println!("Starting...");

    if std::env::var("RUST_LOG").is_err() {
        #[cfg(debug_assertions)]
        std::env::set_var("RUST_LOG", "info");
        #[cfg(not(debug_assertions))]
        std::env::set_var("RUST_LOG", "info");
    }
    let emacs = Emacs::default();

    let printer = ExternalPrinter::default();
    let mut line_editor = Reedline::create()
        .with_external_printer(printer.clone())
        .with_edit_mode(Box::new(EmacsWrapper { emacs }));

    Builder::from_default_env()
        .write_style(WriteStyle::Always)
        .target(Target::Pipe(Box::new(SharedWriter {
            printer: Box::new(printer),
        })))
        .try_init()
        .unwrap();

    server::run_server();

    loop {
        if let Ok(sig) = line_editor.read_line(&CrustPrompt) {
            match sig {
                Signal::Success(input) => {
                    let executed = ProxyServer::instance()
                        .command_registry()
                        .execute(&CommandSender::Console, &input);
                    if !executed {
                        error!("Unknown command.");
                    }
                }
                Signal::CtrlD | Signal::CtrlC => {
                    API.shutdown_proxy(None);
                }
            }
            continue;
        }
        break;
    }
}
@Outfluencer Outfluencer added the bug Something isn't working label Dec 24, 2024
@Outfluencer
Copy link
Author

Outfluencer commented Dec 24, 2024

it was an issue because \n was appended twice because of my wrapper impl. (it was not)

Edit: nvm it just randomly didnt happend 4 times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant