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
54
55
56
57
58
59
//!
//! Terminal creation options
//!

use web_sys::Element;

/// Indicates the target element to which the Terminal instance should be
/// bound to in DOM (WASM-browser only)
pub enum TargetElement {
    /// Bind to the document body
    Body,
    /// Bind to a specific supplied [`web_sys::Element`]
    Element(Element),
    /// Bind to the element with a specific tag name
    TagName(String),
    /// Bind to the element with a specific id
    Id(String),
}

/// Terminal options
pub struct Options {
    /// Default prompt (string such as `"$ "`)
    pub prompt: Option<String>,
    /// Target DOM element (when running under WASM)
    pub element: TargetElement,
}

impl Default for Options {
    fn default() -> Self {
        Options {
            prompt: None,
            element: TargetElement::Body,
        }
    }
}

impl Options {
    /// Create new default options
    pub fn new() -> Options {
        Options::default()
    }

    /// Set prompt string
    pub fn with_prompt(mut self, prompt: &str) -> Self {
        self.prompt = Some(prompt.into());
        self
    }

    /// Set target element
    pub fn with_element(mut self, element: TargetElement) -> Self {
        self.element = element;
        self
    }

    /// Get prompt string
    pub fn prompt(&self) -> String {
        self.prompt.as_ref().unwrap_or(&"$ ".to_string()).clone()
    }
}