summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: b3852cd97ed4eb3deaa622ddccdfcdf630fda912 (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
#![warn(clippy::pedantic)]
#![warn(clippy::nursery)]
#![allow(clippy::missing_const_for_fn)]
#![allow(clippy::too_many_lines)]
#![allow(clippy::unused_self)]

mod action;
mod format;
mod history;
mod parse;
mod readline;
mod state;
mod util;

use async_std::stream::StreamExt as _;

async fn resize(
    action_w: &async_std::channel::Sender<crate::action::Action>,
) {
    let size = terminal_size::terminal_size().map_or(
        (24, 80),
        |(terminal_size::Width(w), terminal_size::Height(h))| (h, w),
    );
    action_w
        .send(crate::action::Action::Resize(size))
        .await
        .unwrap();
}

async fn async_main() -> 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 state = state::State::new();
    state.render(&mut output, true).await.unwrap();

    let state = util::mutex(state);

    {
        let mut signals = signal_hook_async_std::Signals::new(&[
            signal_hook::consts::signal::SIGWINCH,
        ])?;
        let action_w = action_w.clone();
        async_std::task::spawn(async move {
            while signals.next().await.is_some() {
                resize(&action_w).await;
            }
        });
    }

    resize(&action_w).await;

    {
        let state = async_std::sync::Arc::clone(&state);
        let action_w = action_w.clone();
        async_std::task::spawn(async move {
            while let Some(key) = input.read_key().await.unwrap() {
                if let Some(action) =
                    state.lock_arc().await.handle_key(key).await
                {
                    action_w.send(action).await.unwrap();
                }
            }
        });
    }

    // redraw the clock every second
    {
        let action_w = action_w.clone();
        async_std::task::spawn(async move {
            let first_sleep = 1_000_000_000_u64.saturating_sub(
                chrono::Local::now().timestamp_subsec_nanos().into(),
            );
            async_std::task::sleep(std::time::Duration::from_nanos(
                first_sleep,
            ))
            .await;
            let mut interval = async_std::stream::interval(
                std::time::Duration::from_secs(1),
            );
            action_w.send(crate::action::Action::Render).await.unwrap();
            while interval.next().await.is_some() {
                action_w.send(crate::action::Action::Render).await.unwrap();
            }
        });
    }

    let debouncer = crate::action::debounce(action_r);
    while let Some(action) = debouncer.recv().await {
        state
            .lock_arc()
            .await
            .handle_action(action, &mut output, &action_w)
            .await;
    }

    Ok(())
}

fn main() {
    match async_std::task::block_on(async_main()) {
        Ok(_) => (),
        Err(e) => {
            eprintln!("nbsh: {}", e);
            std::process::exit(1);
        }
    };
}