aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 2ad38135476b7f40649d458937683f0cc5f0b7ca (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
mod cli;
mod client;
mod common;
mod server;

use client::{boundaries, layout, panes, tab};
use common::{
    command_is_executing, errors, ipc, os_input_output, pty_bus, screen, start, utils, wasm_vm,
    IpcSenderWithContext, ServerInstruction,
};
use directories_next::ProjectDirs;

use structopt::StructOpt;

use crate::cli::CliArgs;
use crate::command_is_executing::CommandIsExecuting;
use crate::os_input_output::get_os_input;
use crate::utils::{
    consts::{ZELLIJ_TMP_DIR, ZELLIJ_TMP_LOG_DIR},
    logging::*,
};
use client::{boundaries, layout, panes, tab};
use common::{
    command_is_executing, errors, os_input_output, pty_bus, screen, setup, start, utils, wasm_vm,
    ApiCommand,
};
use std::convert::TryFrom;
use std::io::Write;
use std::os::unix::net::UnixStream;
use structopt::StructOpt;

pub fn main() {
    // First run installation of default plugins & layouts
    let project_dirs = ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap();
    let data_dir = project_dirs.data_dir();
    let mut assets = asset_map! {
        "assets/layouts/default.yaml" => "layouts/default.yaml",
        "assets/layouts/strider.yaml" => "layouts/strider.yaml",
    };
    assets.extend(asset_map! {
        "assets/plugins/status-bar.wasm" => "plugins/status-bar.wasm",
        "assets/plugins/tab-bar.wasm" => "plugins/tab-bar.wasm",
        "assets/plugins/strider.wasm" => "plugins/strider.wasm",
    });

    for (path, bytes) in assets {
        let path = data_dir.join(path);
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        if !path.exists() {
            std::fs::write(path, bytes).expect("Failed to install default assets!");
        }
    }

    let opts = CliArgs::from_args();
    let config = match Config::try_from(&opts) {
        Ok(config) => config,
        Err(e) => {
            eprintln!("There was an error in the config file:\n{}", e);
            std::process::exit(1);
        }
    };
    if let Some(split_dir) = opts.split {
        match split_dir {
            'h' => {
                let mut send_server_instructions = IpcSenderWithContext::to_server();
                send_server_instructions
                    .send(ServerInstruction::SplitHorizontally)
                    .unwrap();
            }
            'v' => {
                let mut send_server_instructions = IpcSenderWithContext::to_server();
                send_server_instructions
                    .send(ServerInstruction::SplitVertically)
                    .unwrap();
            }
            _ => {}
        };
    } else if opts.move_focus {
        let mut send_server_instructions = IpcSenderWithContext::to_server();
        send_server_instructions
            .send(ServerInstruction::MoveFocus)
            .unwrap();
    } else if let Some(file_to_open) = opts.open_file {
        let mut send_server_instructions = IpcSenderWithContext::to_server();
        send_server_instructions
            .send(ServerInstruction::OpenFile(file_to_open))
            .unwrap();
    } else {
        let os_input = get_os_input();
        atomic_create_dir(ZELLIJ_TMP_DIR).unwrap();
        atomic_create_dir(ZELLIJ_TMP_LOG_DIR).unwrap();
        start(Box::new(os_input), opts, config);
    }
}