Skip to main content

workflow_terminal/terminal/
bindings.rs

1//!
2//! [xterm.js](http://xtermjs.org) [`mod@wasm_bindgen`] interface and plugin bindings
3//!
4
5use std::fmt::Debug;
6use std::fmt::Formatter;
7use wasm_bindgen::JsValue;
8use wasm_bindgen::prelude::wasm_bindgen;
9use web_sys::Element;
10
11#[wasm_bindgen]
12extern "C" {
13
14    /// Binding to the xterm.js `FitAddon` plugin that resizes the terminal to
15    /// fit its container element.
16    #[wasm_bindgen(js_namespace=["window", "FitAddon"], js_name="FitAddon")]
17    pub type FitAddon;
18
19    /// Constructs a new `FitAddon` instance.
20    #[wasm_bindgen(
21        constructor,
22        js_class = "window.FitAddon.FitAddon",
23        js_name = "FitAddon"
24    )]
25    pub fn new() -> FitAddon;
26
27    /// Computes the terminal dimensions that fit the current container.
28    #[wasm_bindgen(method, js_name = "proposeDimensions")]
29    pub fn propose_dimensions(this: &FitAddon);
30
31    /// Resizes the terminal to fit its container element.
32    #[wasm_bindgen(method, js_name = "fit")]
33    pub fn fit(this: &FitAddon);
34}
35
36#[wasm_bindgen]
37extern "C" {
38
39    /// Binding to the xterm.js `WebLinksAddon` plugin that turns URLs into
40    /// clickable links.
41    #[wasm_bindgen(js_namespace=["window","WebLinksAddon"], js_name="WebLinksAddon")]
42    pub type WebLinksAddon;
43
44    /// Constructs a new `WebLinksAddon` with the given link-click callback.
45    #[wasm_bindgen(
46        constructor,
47        js_class = "window.WebLinksAddon.WebLinksAddon",
48        js_name = "WebLinksAddon"
49    )]
50    pub fn new(callback: JsValue) -> WebLinksAddon;
51}
52
53#[wasm_bindgen]
54extern "C" {
55    /// Binding to the internal `_core` object of an xterm.js terminal.
56    #[wasm_bindgen(extends = js_sys::Object)]
57    pub type XtermCoreImpl;
58    /// Applies the given theme object directly to the terminal core.
59    #[wasm_bindgen(method, js_name = "_setTheme")]
60    pub fn set_theme(this: &XtermCoreImpl, them: js_sys::Object);
61
62    /// Binding to the payload of an xterm.js key event.
63    #[wasm_bindgen(extends = js_sys::Object)]
64    pub type XtermEvent;
65
66    /// Returns the underlying DOM `KeyboardEvent` for this event.
67    #[wasm_bindgen(method, getter, js_name = "domEvent")]
68    pub fn get_dom_event(this: &XtermEvent) -> web_sys::KeyboardEvent;
69    /// Returns the key string associated with this event.
70    #[wasm_bindgen(method, getter, js_name = "key")]
71    pub fn get_key(this: &XtermEvent) -> String;
72
73    /// Binding to the xterm.js `Terminal` instance.
74    #[wasm_bindgen(js_namespace=window, js_name="Terminal")]
75    pub type XtermImpl;
76
77    /// Constructs a new xterm.js `Terminal` with the given options object.
78    #[wasm_bindgen(constructor, js_class = "Terminal")]
79    pub fn new(opt: js_sys::Object) -> XtermImpl;
80
81    /// Moves keyboard focus to the terminal.
82    #[wasm_bindgen(method)]
83    pub fn focus(this: &XtermImpl);
84
85    /// Returns the terminal instance's number.
86    #[wasm_bindgen(method, getter)]
87    pub fn number(this: &XtermImpl) -> u32;
88
89    /// Returns the terminal's internal `_core` object.
90    #[wasm_bindgen(method, getter, js_name = "_core")]
91    pub fn core(this: &XtermImpl) -> XtermCoreImpl;
92
93    /// Opens (renders) the terminal within the given DOM element.
94    #[wasm_bindgen(method)]
95    pub fn open(this: &XtermImpl, el: &Element);
96
97    /// Sets the named terminal option to the given value.
98    #[wasm_bindgen(method, js_name = "setOption")]
99    pub fn set_option(this: &XtermImpl, name: &str, option: JsValue);
100
101    /// Returns the value of the named terminal option.
102    #[wasm_bindgen(method, js_name = "getOption")]
103    pub fn get_option(this: &XtermImpl, name: &str) -> JsValue;
104
105    /// Redraws the terminal rows in the inclusive range `start..=stop`.
106    #[wasm_bindgen(method)]
107    pub fn refresh(this: &XtermImpl, start: u32, stop: u32);
108
109    /// Returns the number of rows currently displayed.
110    #[wasm_bindgen(method, getter, js_name = "rows")]
111    pub fn rows(this: &XtermImpl) -> u32;
112
113    /// Returns the number of columns currently displayed.
114    #[wasm_bindgen(method, getter, js_name = "cols")]
115    pub fn cols(this: &XtermImpl) -> u32;
116
117    /// Registers a callback invoked on each key event.
118    #[wasm_bindgen(method, js_name = "onKey")]
119    pub fn on_key(this: &XtermImpl, f: &js_sys::Function);
120
121    #[wasm_bindgen(method, js_name = "write")]
122    fn _write(this: &XtermImpl, text: String);
123
124    // #[wasm_bindgen(method, js_name="paste")]
125    // fn _paste(this: &XtermImpl, text:String);
126
127    /// Loads the given addon into the terminal.
128    #[wasm_bindgen(method, js_name = "loadAddon")]
129    pub fn load_addon(this: &XtermImpl, addon: JsValue);
130
131    /// Returns the DOM element hosting the terminal.
132    #[wasm_bindgen(method, getter, js_name = "element")]
133    pub fn get_element(this: &XtermImpl) -> Element;
134
135    /// Returns the currently selected text.
136    #[wasm_bindgen(method, js_name = "getSelection")]
137    pub fn get_selection(this: &XtermImpl) -> String;
138
139    /// Registers a regular-expression link matcher with the given click callback.
140    #[wasm_bindgen(method, js_name = "registerLinkMatcher")]
141    pub fn register_link_matcher(
142        this: &XtermImpl,
143        regexp: &js_sys::RegExp,
144        callback: &js_sys::Function,
145    );
146
147    // future versions of xterm.js
148    // #[wasm_bindgen(method, js_name = "getSelectionService")]
149    // pub fn get_selection_service(this: &XtermImpl) -> SelectionService;
150    // #[wasm_bindgen(extends = js_sys::Object)]
151    // pub type SelectionService;
152    // #[wasm_bindgen(method, js_name = "getSelection")]
153    // pub fn get_selection(this: &SelectionService) -> String;
154
155}
156
157impl Debug for XtermImpl {
158    fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
159        write!(f, "Workflow Xterm")?;
160        Ok(())
161    }
162}
163
164impl XtermImpl {
165    /// Writes the given text to the terminal display.
166    pub fn write<T: ToString>(&self, text: T) {
167        self._write(text.to_string());
168    }
169
170    /// Applies the given object as the terminal's `theme` option.
171    pub fn set_theme(&self, theme: js_sys::Object) {
172        self.set_option("theme", theme.into());
173        //self.core().set_theme(theme);
174    }
175}
176
177#[wasm_bindgen]
178extern "C" {
179    #[wasm_bindgen (extends = :: js_sys :: Object , js_name = ResizeObserver , typescript_type = "ResizeObserver")]
180    #[derive(Debug, Clone, PartialEq, Eq)]
181    /// Binding to the DOM [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver) API.
182    pub type ResizeObserver;
183    /// Constructs a new `ResizeObserver` that invokes `callback` on size changes.
184    #[wasm_bindgen(catch, constructor, js_class = "ResizeObserver")]
185    pub fn new(callback: &::js_sys::Function) -> std::result::Result<ResizeObserver, JsValue>;
186    /// Stops observing all target elements.
187    #[wasm_bindgen (method , structural , js_class = "ResizeObserver" , js_name = disconnect)]
188    pub fn disconnect(this: &ResizeObserver);
189    /// Begins observing size changes of the given element.
190    #[wasm_bindgen (method , structural , js_class = "ResizeObserver" , js_name = observe)]
191    pub fn observe(this: &ResizeObserver, target: &Element);
192    // # [wasm_bindgen (method , structural , js_class = "ResizeObserver" , js_name = observe)]
193    // pub fn observe_with_options(
194    //     this: &ResizeObserver,
195    //     target: &Element,
196    //     options: &ResizeObserverOptions,
197    // );
198    /// Stops observing size changes of the given element.
199    // # [wasm_bindgen (method , structural , js_class = "ResizeObserver" , js_name = unobserve)]
200    pub fn unobserve(this: &ResizeObserver, target: &Element);
201}
202
203// #[wasm_bindgen]
204// extern "C" {
205//     #[wasm_bindgen (extends = web_sys::Event , extends = :: js_sys :: Object , js_name = ClipboardEvent , typescript_type = "ClipboardEvent")]
206//     #[derive(Debug, Clone, PartialEq, Eq)]
207//     pub type ClipboardEvent;
208//     #[wasm_bindgen (structural , method , getter , js_class = "ClipboardEvent" , js_name = clipboardData)]
209//     pub fn clipboard_data(this: &ClipboardEvent) -> Option<web_sys::DataTransfer>;
210//     #[wasm_bindgen(catch, constructor, js_class = "ClipboardEvent")]
211//     pub fn new(type_: &str) -> Result<ClipboardEvent, JsValue>;
212//     // #[wasm_bindgen(catch, constructor, js_class = "ClipboardEvent")]
213//     // pub fn new_with_event_init_dict(
214//     //     type_: &str,
215//     //     event_init_dict: &ClipboardEventInit,
216//     // ) -> Result<ClipboardEvent, JsValue>;
217// }