main
rs 65 lines 1.93 KB
Raw
1 use byte_unit::Byte;
2 use serde::{Deserialize, Serialize};
3
4 /// Scrollback is the set of session contents that weren't shared live
5 /// but are still part of the shared session.
6 #[derive(Clone, Deserialize, Serialize)]
7 pub struct Scrollback {
8 /// The blocks that make up the scrollback. Clients are expected
9 /// to be able to serialize and deserialize accordingly.
10 pub blocks: Vec<ScrollbackBlock>,
11
12 /// True iff the session is in alt-screen mode
13 /// at time of share.
14 pub is_alt_screen_active: bool,
15 }
16
17 impl Scrollback {
18 pub fn num_bytes(&self) -> Byte {
19 self.blocks
20 .iter()
21 .map(|b| b.num_bytes().as_u64())
22 .fold(0, u64::saturating_add)
23 .into()
24 }
25
26 /// Returns true if the scrollback size exceeds |size_bytes|.
27 pub fn exceeds_size_bytes(&self, size_bytes: Byte) -> bool {
28 self.num_bytes() > size_bytes
29 }
30 }
31
32 /// Override the Debug impl to avoid accidentally leaking sensitive
33 /// data in logs.
34 impl std::fmt::Debug for Scrollback {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 write!(
37 f,
38 "Scrollback {{ num_blocks: {}, is_alt_screen_active: {} }}",
39 self.blocks.len(),
40 self.is_alt_screen_active
41 )
42 }
43 }
44
45 /// An individual scrollback block.
46 #[derive(Clone, Deserialize, Serialize)]
47 pub struct ScrollbackBlock {
48 /// The raw contents of the block. Clients are expected to be able to
49 /// serialize and deserialize from [`SerializedBlock`] in the Warp client.
50 pub raw: Vec<u8>,
51 }
52
53 impl ScrollbackBlock {
54 pub fn num_bytes(&self) -> Byte {
55 self.raw.len().into()
56 }
57 }
58
59 /// Override the Debug impl to avoid accidentally leaking sensitive
60 /// data in logs.
61 impl std::fmt::Debug for ScrollbackBlock {
62 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63 write!(f, "ScrollbackBlock {{ num_bytes: {} }}", self.num_bytes())
64 }
65 }