summaryrefslogtreecommitdiff
path: root/src/widgets/log.rs
blob: 4966cc013c82b2f3e408a3f72a1a338d6b54d6fa (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! Logging events in a scrolling list.
//!
//! Events are logged in an upwards scrolled box that forgets about the events
//! that move off-screen. The logger may optionally add timestamps to events.
//!
//! To create a log widget, make a `LogBuilder`, optionally modify properties,
//! and then use its `create` method to create a `Log` object.

use std::ptr;

use crate::bindings;
use crate::core::Screen;

pub struct Log {
    ptr: *mut bindings::Logwidget,
}

pub struct LogBuilder<'a, 'b> {
    _screen: &'a Screen<'b>,
    lefttop: (u32, u32),
    size: (u32, u32),
    title: Option<&'a str>,
    timestamps: bool,
}

impl<'a, 'b> LogBuilder<'a, 'b> {
    pub fn new(screen: &'a Screen<'b>, lefttop: (u32, u32), size: (u32, u32)) -> Self {
        LogBuilder {
            _screen: screen,
            lefttop, size,
            title: None,
            timestamps: false,
        }
    }

    pub fn add_title<'s: 'a>(&mut self, title: &'s str) -> &mut Self {
        self.title = Some(title);
        self
    }

    pub fn set_timestamps(&mut self, timestamps: bool) -> &mut Self {
        self.timestamps = timestamps;
        self
    }

    pub fn create(&self) -> Log {
        Log {
            ptr: unsafe {
                bindings::lgw_make(
                    self.lefttop.0 as i32, self.lefttop.1 as i32,
                    self.size.0 as i32, self.size.1 as i32,
                    match self.title {
                        None => ptr::null(),
                        Some(s) => s.as_ptr() as *const i8,
                    },
                    self.timestamps)
            }
        }
    }
}

impl Log {
    /// Called automatically; should only be needed if something else overwrote
    /// the widget.
    pub fn redraw(&self) {
        unsafe { bindings::lgw_redraw(self.ptr); }
    }

    pub fn add(&mut self, line: &str) {
        unsafe { bindings::lgw_add(self.ptr, line.as_ptr() as *const i8); }
    }

    pub fn clear(&mut self) {
        unsafe { bindings::lgw_clear(self.ptr); }
    }

    pub fn change_title(&mut self, title: &str) {
        unsafe { bindings::lgw_changetitle(self.ptr, title.as_ptr() as *const i8); }
    }
}

impl Drop for Log {
    fn drop(&mut self) {
        unsafe { bindings::lgw_destroy(self.ptr); }
    }
}