0

I am trying to detect user input by using crossterm, when Ctrl R is pressed it should go back to the main function. If I press any key, (not just the Ctrl R , it simply exits.

The following error message appears:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Custom { kind: NotConnected, error: "Not a terminal" }', src\prompts\confirm.rs:8:10.

The error, though, somehow happens not in the return_to_main file, which has the following code:

use crossterm::event::{Event, KeyCode, KeyEvent};
use crossterm::terminal::disable_raw_mode;
use crossterm::{event, terminal};
use std::time::Duration;


pub fn return_to_main() -> crossterm::Result<()> {
    terminal::enable_raw_mode()?;

    loop {
        if event::poll(Duration::from_millis(1000))? {
            if let Event::Key(event) = event::read()? {
                match event {
                    KeyEvent {
                        code: KeyCode::Char('r'),
                        modifiers: event::KeyModifiers::CONTROL,
                        kind: _,
                        state: _,
                    } => return Ok(()),
                    _ => (),
                }
            }
        }

        disable_raw_mode()?;
    }
}

but in the confirm.rs file which has the following code. (module for confirming something)

use dialoguer::{theme::ColorfulTheme, Confirm};

pub fn confirm(prompt: &str) -> bool {
    Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt(prompt)
        .default(true)
        .interact()
        .unwrap()
}

Note: without enabling raw mode this code works completely fine.

Mart
  • 11
  • 3
  • In what context are you invoking this code? Are standard input and output connected to a terminal, and if so, what kind of terminal (`TERM` type and emulator)? – bk2204 Aug 31 '23 at 01:17
  • @bk2204 the context is that I'm trying to make a return key. The terminal is just normal Windows Powershell. – Mart Aug 31 '23 at 19:56

0 Answers0