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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

#![forbid(unsafe_code)]
use crate::{
    interfaces::{LeftScreen, RightScreen},
    tui::{
        text_builder::TextBuilder,
        tui_interface::{TUIInterface, TUIOutput},
    },
};
use tui::{
    style::{Color, Style},
    text::Spans,
};

#[derive(Debug, Clone)]
pub struct Viewer<BytecodeViewer: LeftScreen, SourceViewer: RightScreen<BytecodeViewer>> {
    bytecode_text: Vec<String>,
    source_viewer: SourceViewer,
    bytecode_viewer: BytecodeViewer,
}

impl<BytecodeViewer: LeftScreen, SourceViewer: RightScreen<BytecodeViewer>>
    Viewer<BytecodeViewer, SourceViewer>
{
    pub fn new(source_viewer: SourceViewer, bytecode_viewer: BytecodeViewer) -> Self {
        Self {
            bytecode_text: bytecode_viewer
                .backing_string()
                .split('\n')
                .map(|x| x.to_string())
                .collect(),
            source_viewer,
            bytecode_viewer,
        }
    }
}

impl<BytecodeViewer: LeftScreen, SourceViewer: RightScreen<BytecodeViewer>> TUIInterface
    for Viewer<BytecodeViewer, SourceViewer>
{
    const LEFT_TITLE: &'static str = "Bytecode";
    const RIGHT_TITLE: &'static str = "Source Code";

    fn on_redraw(&mut self, line_number: u16, column_number: u16) -> TUIOutput {
        // Highlight style
        let style: Style = Style::default().bg(Color::Red);
        let report = match self
            .bytecode_viewer
            .get_source_index_for_line(line_number as usize, column_number as usize)
        {
            None => {
                let mut builder = TextBuilder::new();
                builder.add(self.source_viewer.backing_string(), Style::default());
                builder.finish()
            }
            Some(info) => {
                let source_context = self.source_viewer.source_for_code_location(info).unwrap();

                let mut builder = TextBuilder::new();
                builder.add(source_context.left, Style::default());
                builder.add(source_context.highlight, style);
                builder.add(source_context.remainder, Style::default());
                builder.finish()
            }
        };

        TUIOutput {
            left_screen: self
                .bytecode_text
                .iter()
                .map(|x| Spans::from(x.clone()))
                .collect(),
            right_screen: report,
        }
    }

    fn bound_line(&self, line_number: u16) -> u16 {
        std::cmp::min(
            line_number,
            self.bytecode_text.len().checked_sub(1).unwrap() as u16,
        )
    }

    fn bound_column(&self, line_number: u16, column_number: u16) -> u16 {
        std::cmp::min(
            column_number,
            self.bytecode_text[line_number as usize].len() as u16,
        )
    }
}