1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Define backends using common libraries.
//!
//! Cursive doesn't print anything by itself: it delegates this job to a
//! backend library, which handles all actual input and output.
//!
//! This module defines the [`Backend`] trait, as well as a few implementations
//! using some common libraries. Each of those included backends needs a
//! corresonding feature to be enabled.
//!
//! [`Backend`]: ../backend/trait.Backend.html
#[cfg(unix)]
mod resize;

pub mod blt;
pub mod crossterm;
pub mod curses;
pub mod puppet;
pub mod termion;

#[allow(dead_code)]
fn boxed(e: impl std::error::Error + 'static) -> Box<dyn std::error::Error> {
    Box::new(e)
}

/// Tries to initialize the default backend.
///
/// Will use the first backend enabled from the list:
/// * BearLibTerminal
/// * Termion
/// * Crossterm
/// * Pancurses
/// * Ncurses
/// * Dummy
pub fn try_default(
) -> Result<Box<dyn cursive_core::backend::Backend>, Box<dyn std::error::Error>>
{
    cfg_if::cfg_if! {
        if #[cfg(feature = "blt-backend")] {
            Ok(blt::Backend::init())
        } else if #[cfg(feature = "termion-backend")] {
            termion::Backend::init().map_err(boxed)
        } else if #[cfg(feature = "crossterm-backend")] {
            crossterm::Backend::init().map_err(boxed)
        } else if #[cfg(feature = "pancurses-backend")] {
            curses::pan::Backend::init().map_err(boxed)
        } else if #[cfg(feature = "ncurses-backend")] {
            curses::n::Backend::init().map_err(boxed)
        } else {
            log::warn!("No built-it backend, falling back to Dummy backend.");
            Ok(cursive_core::backend::Dummy::init())
        }
    }
}