summaryrefslogtreecommitdiffstats
path: root/src/shell/history/entry.rs
blob: 24b90cf239f9bc5c31cbbb1080eb4c3ca36743fe (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use crate::shell::prelude::*;

pub struct Entry {
    cmdline: String,
    env: Env,
    pty: super::pty::Pty,
    job: super::job::Job,
    fullscreen: Option<bool>,
}

impl Entry {
    pub fn new(
        cmdline: String,
        env: Env,
        size: (u16, u16),
        event_w: crate::shell::event::Writer,
    ) -> Self {
        let (pty, pts) = super::pty::Pty::new(size, event_w.clone()).unwrap();
        let job = super::job::Job::new(&cmdline, env.clone(), &pts, event_w)
            .unwrap();
        Self {
            cmdline,
            env,
            pty,
            job,
            fullscreen: None,
        }
    }

    pub fn render(
        &self,
        out: &mut impl textmode::Textmode,
        idx: usize,
        entry_count: usize,
        state: &super::job::State,
        vt: &mut super::pty::Vt,
        size: (u16, u16),
        focused: bool,
        scrolling: bool,
        offset: time::UtcOffset,
    ) {
        let time = state.exit_info().map_or_else(
            || {
                format!(
                    "[{}]",
                    crate::format::time(
                        self.job.start_time().to_offset(offset)
                    )
                )
            },
            |info| {
                format!(
                    "({}) [{}]",
                    crate::format::duration(
                        *info.instant() - *self.job.start_instant()
                    ),
                    crate::format::time(
                        self.job.start_time().to_offset(offset)
                    ),
                )
            },
        );

        vt.bell(out, focused);

        set_bgcolor(out, idx, focused);
        out.set_fgcolor(textmode::color::YELLOW);
        let entry_count_width = format!("{}", entry_count + 1).len();
        let idx_str = format!("{}", idx + 1);
        out.write_str(&" ".repeat(entry_count_width - idx_str.len()));
        out.write_str(&idx_str);
        out.write_str(" ");
        out.reset_attributes();

        set_bgcolor(out, idx, focused);
        if let Some(info) = state.exit_info() {
            let status = info.status();
            if status.signal().is_some() {
                out.set_fgcolor(textmode::color::MAGENTA);
            } else if status.success() {
                out.set_fgcolor(textmode::color::DARKGREY);
            } else {
                out.set_fgcolor(textmode::color::RED);
            }
            out.write_str(&crate::format::exit_status(status));
        } else {
            out.write_str("     ");
        }
        out.reset_attributes();

        if vt.is_bell() {
            out.set_bgcolor(textmode::Color::Rgb(64, 16, 16));
        } else {
            set_bgcolor(out, idx, focused);
        }
        out.write_str("$ ");
        set_bgcolor(out, idx, focused);
        let start = usize::from(out.screen().cursor_position().1);
        let end = usize::from(size.1) - time.len() - 2;
        let max_len = end - start;
        let cmd = if self.cmd().len() > max_len {
            &self.cmd()[..(max_len - 4)]
        } else {
            self.cmd()
        };
        if let super::job::State::Running(span) = state {
            let span = (span.0.min(cmd.len()), span.1.min(cmd.len()));
            if !cmd[..span.0].is_empty() {
                out.write_str(&cmd[..span.0]);
            }
            if !cmd[span.0..span.1].is_empty() {
                out.set_bgcolor(textmode::Color::Rgb(16, 64, 16));
                out.write_str(&cmd[span.0..span.1]);
                set_bgcolor(out, idx, focused);
            }
            if !cmd[span.1..].is_empty() {
                out.write_str(&cmd[span.1..]);
            }
        } else {
            out.write_str(cmd);
        }
        if self.cmd().len() > max_len {
            if let super::job::State::Running(span) = state {
                if span.0 < cmd.len() && span.1 > cmd.len() {
                    out.set_bgcolor(textmode::Color::Rgb(16, 64, 16));
                }
            }
            out.write_str(" ");
            if let super::job::State::Running(span) = state {
                if span.1 > cmd.len() {
                    out.set_bgcolor(textmode::Color::Rgb(16, 64, 16));
                }
            }
            out.set_fgcolor(textmode::color::BLUE);
            out.write_str("...");
        }
        out.reset_attributes();

        set_bgcolor(out, idx, focused);
        let cur_pos = out.screen().cursor_position();
        out.write_str(&" ".repeat(
            usize::from(size.1) - time.len() - 1 - usize::from(cur_pos.1),
        ));
        out.write_str(&time);
        out.write_str(" ");
        out.reset_attributes();

        if vt.binary() {
            let msg = "This appears to be binary data. Fullscreen this entry to view anyway.";
            let len: u16 = msg.len().try_into().unwrap();
            out.move_to(
                out.screen().cursor_position().0 + 1,
                (size.1 - len) / 2,
            );
            out.set_fgcolor(textmode::color::RED);
            out.write_str(msg);
            out.hide_cursor(true);
        } else {
            let last_row =
                vt.output_lines(focused && !scrolling, state.running());
            let mut max_lines = self.max_lines(entry_count);
            if last_row > max_lines {
                out.write(b"\r\n");
                out.set_fgcolor(textmode::color::BLUE);
                out.write_str("...");
                out.reset_attributes();
                max_lines -= 1;
            }
            let mut out_row = out.screen().cursor_position().0 + 1;
            let screen = vt.screen();
            let pos = screen.cursor_position();
            let mut wrapped = false;
            let mut cursor_found = None;
            for (idx, row) in screen
                .rows_formatted(0, size.1)
                .enumerate()
                .take(last_row)
                .skip(last_row.saturating_sub(max_lines))
            {
                let idx: u16 = idx.try_into().unwrap();
                out.reset_attributes();
                if !wrapped {
                    out.move_to(out_row, 0);
                }
                out.write(&row);
                wrapped = screen.row_wrapped(idx);
                if pos.0 == idx {
                    cursor_found = Some(out_row);
                }
                out_row += 1;
            }
            if focused && !scrolling {
                if let Some(row) = cursor_found {
                    out.hide_cursor(screen.hide_cursor());
                    out.move_to(row, pos.1);
                } else {
                    out.hide_cursor(true);
                }
            }
        }

        out.reset_attributes();
    }

    pub fn render_fullscreen(&self, out: &mut impl textmode::Textmode) {
        self.pty.with_vt_mut(|vt| {
            out.write(&vt.screen().state_formatted());
            vt.bell(out, true);
            out.reset_attributes();
        });
    }

    pub fn input(&self, bytes: Vec<u8>) {
        self.pty.input(bytes);
    }

    pub fn resize(&self, size: (u16, u16)) {
        self.pty.resize(size);
    }

    pub fn cmd(&self) -> &str {
        &self.cmdline
    }

    pub fn toggle_fullscreen(&mut self) {
        if let Some(fullscreen) = self.fullscreen {
            self.fullscreen = Some(!fullscreen);
        } else {
            self.fullscreen = Some(!self.pty.fullscreen());
        }
    }

    pub fn set_fullscreen(&mut self, fullscreen: bool) {
        self.fullscreen = Some(fullscreen);
    }

    pub fn running(&self) -> bool {
        self.job.running()
    }

    pub fn lines(&self, entry_count: usize, focused: bool) -> usize {
        let running = self.running();
        1 + std::cmp::min(
            self.pty.with_vt(|vt| vt.output_lines(focused, running)),
            self.max_lines(entry_count),
        )
    }

    pub fn should_fullscreen(&self) -> bool {
        self.fullscreen.unwrap_or_else(|| self.pty.fullscreen())
    }

    pub fn lock_vt(&self) -> std::sync::MutexGuard<super::pty::Vt> {
        self.pty.lock_vt()
    }

    pub fn lock_state(&self) -> std::sync::MutexGuard<super::job::State> {
        self.job.lock_state()
    }

    pub fn set_span(&self, span: (usize, usize)) {
        self.job.set_span(span);
    }

    fn max_lines(&self, entry_count: usize) -> usize {
        if self.env.idx() == entry_count - 1 {
            15
        } else {
            5
        }
    }
}

fn set_bgcolor(out: &mut impl textmode::Textmode, idx: usize, focus: bool) {
    if focus {
        out.set_bgcolor(textmode::Color::Rgb(0x56, 0x1b, 0x8b));
    } else if idx % 2 == 0 {
        out.set_bgcolor(textmode::Color::Rgb(0x24, 0x21, 0x00));
    } else {
        out.set_bgcolor(textmode::Color::Rgb(0x20, 0x20, 0x20));
    }
}