Skip to main content

tui/rendering/
render_context.rs

1use std::sync::Arc;
2
3use crate::theme::Theme;
4
5#[cfg(feature = "syntax")]
6use crate::syntax_highlighting::SyntaxHighlighter;
7
8/// Environment passed to render methods: terminal size, theme.
9#[derive(Clone)]
10pub struct ViewContext {
11    pub size: Size,
12    pub theme: Arc<Theme>,
13    #[cfg(feature = "syntax")]
14    pub(crate) highlighter: Arc<SyntaxHighlighter>,
15}
16
17/// Terminal dimensions in columns and rows.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub struct Size {
20    pub width: u16,
21    pub height: u16,
22}
23
24impl ViewContext {
25    pub fn new(size: impl Into<Size>) -> Self {
26        Self::new_with_theme(size, Theme::default())
27    }
28
29    pub fn new_with_theme(size: impl Into<Size>, theme: Theme) -> Self {
30        Self {
31            size: size.into(),
32            theme: Arc::new(theme),
33            #[cfg(feature = "syntax")]
34            highlighter: Arc::new(SyntaxHighlighter::new()),
35        }
36    }
37
38    #[cfg(feature = "syntax")]
39    pub fn highlighter(&self) -> &SyntaxHighlighter {
40        &self.highlighter
41    }
42
43    pub fn with_size(&self, size: impl Into<Size>) -> Self {
44        Self {
45            size: size.into(),
46            theme: self.theme.clone(),
47            #[cfg(feature = "syntax")]
48            highlighter: self.highlighter.clone(),
49        }
50    }
51}
52
53impl From<(u16, u16)> for Size {
54    fn from((width, height): (u16, u16)) -> Self {
55        Self { width, height }
56    }
57}