summaryrefslogtreecommitdiffstats
path: root/src/nbsh.rs
blob: 84d35e33cf0587c38b8c3bf63bcaebc479f8868d (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
use textmode::Textmode as _;

pub async fn run() -> anyhow::Result<()> {
    let mut input = textmode::Input::new().await?;
    let mut output = textmode::Output::new().await?;

    // avoid the guards getting stuck in a task that doesn't run to
    // completion
    let _input_guard = input.take_raw_guard();
    let _output_guard = output.take_screen_guard();

    let (action_w, action_r) = async_std::channel::unbounded();

    let repl = async_std::sync::Arc::new(async_std::sync::Mutex::new(
        crate::repl::Repl::new(action_w.clone()),
    ));
    let history = async_std::sync::Arc::new(async_std::sync::Mutex::new(
        crate::history::History::new(action_w),
    ));
    let input_source = async_std::sync::Arc::new(
        async_std::sync::Mutex::new(InputSource::Repl),
    );

    render(
        &mut output,
        &*repl.lock_arc().await,
        &*history.lock_arc().await,
        &*input_source.lock_arc().await,
    )
    .await
    .unwrap();

    {
        let repl = async_std::sync::Arc::clone(&repl);
        let history = async_std::sync::Arc::clone(&history);
        let input_source = async_std::sync::Arc::clone(&input_source);
        async_std::task::spawn(async move {
            while let Ok(action) = action_r.recv().await {
                handle_action(
                    action,
                    &mut output,
                    async_std::sync::Arc::clone(&repl),
                    async_std::sync::Arc::clone(&history),
                    async_std::sync::Arc::clone(&input_source),
                )
                .await;
            }
        });
    }

    loop {
        let quit = handle_input(
            &mut input,
            async_std::sync::Arc::clone(&repl),
            async_std::sync::Arc::clone(&history),
            async_std::sync::Arc::clone(&input_source),
        )
        .await;
        if quit {
            break;
        }
    }

    Ok(())
}

async fn handle_action(
    action: Action,
    output: &mut textmode::Output,
    repl: async_std::sync::Arc<async_std::sync::Mutex<crate::repl::Repl>>,
    history: async_std::sync::Arc<
        async_std::sync::Mutex<crate::history::History>,
    >,
    input_source: async_std::sync::Arc<async_std::sync::Mutex<InputSource>>,
) {
    match action {
        Action::Render => {
            render(
                output,
                &*repl.lock_arc().await,
                &*history.lock_arc().await,
                &*input_source.lock_arc().await,
            )
            .await
            .unwrap();
        }
        Action::Run(ref cmd) => {
            history.lock_arc().await.run(cmd).await.unwrap();
        }
        Action::UpdateFocus(new_input_source) => {
            *input_source.lock_arc().await = new_input_source;
        }
    }
}

async fn handle_input(
    input: &mut textmode::Input,
    repl: async_std::sync::Arc<async_std::sync::Mutex<crate::repl::Repl>>,
    history: async_std::sync::Arc<
        async_std::sync::Mutex<crate::history::History>,
    >,
    input_source: async_std::sync::Arc<async_std::sync::Mutex<InputSource>>,
) -> bool {
    let key = input.read_key().await.unwrap();
    if let Some(key) = key {
        let input_source = *input_source.lock_arc().await;
        let quit = match input_source {
            InputSource::Repl => repl.lock_arc().await.handle_key(key).await,
            InputSource::History(idx) => {
                history.lock_arc().await.handle_key(key, idx).await
            }
        };
        if quit {
            return true;
        }
    } else {
        return true;
    }
    false
}

async fn render(
    out: &mut textmode::Output,
    repl: &crate::repl::Repl,
    history: &crate::history::History,
    input_source: &InputSource,
) -> anyhow::Result<()> {
    out.clear();
    if let InputSource::Repl = input_source {
        history.render(out, repl.lines()).await?;
        repl.render(out).await?;
    } else {
        history.render(out, 0).await?;
    }
    out.refresh().await?;
    Ok(())
}

#[derive(Copy, Clone, Debug)]
pub enum InputSource {
    Repl,
    History(usize),
}

pub enum Action {
    Render,
    Run(String),
    UpdateFocus(InputSource),
}