node_launchpad/components/
utils.rs

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
// Copyright 2024 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::system;
use color_eyre::eyre::{self};
use ratatui::prelude::*;
use sn_node_manager::config::get_service_log_dir_path;
use sn_releases::ReleaseType;

/// helper function to create a centered rect using up certain percentage of the available rect `r`
pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
    let popup_layout = Layout::vertical([
        Constraint::Percentage((100 - percent_y) / 2),
        Constraint::Percentage(percent_y),
        Constraint::Percentage((100 - percent_y) / 2),
    ])
    .split(r);

    Layout::horizontal([
        Constraint::Percentage((100 - percent_x) / 2),
        Constraint::Percentage(percent_x),
        Constraint::Percentage((100 - percent_x) / 2),
    ])
    .split(popup_layout[1])[1]
}

/// Helper function to create a centered rect using a fixed x,y constraint.
pub fn centered_rect_fixed(x: u16, y: u16, r: Rect) -> Rect {
    let popup_layout = Layout::vertical([
        Constraint::Fill(1),
        Constraint::Length(y),
        Constraint::Fill(1),
    ])
    .split(r);

    Layout::horizontal([
        Constraint::Fill(1),
        Constraint::Length(x),
        Constraint::Fill(1),
    ])
    .split(popup_layout[1])[1]
}

/// Opens the logs folder for a given node service name or the default service log directory.
///
/// # Parameters
///
/// * `node_name`: Optional node service name. If `None`, the default service log directory is used.
///
/// # Returns
///
/// A `Result` indicating the success or failure of the operation.
pub fn open_logs(node_name: Option<String>) -> Result<(), eyre::Report> {
    let service_path = get_service_log_dir_path(ReleaseType::NodeLaunchpad, None, None)?
        .to_string_lossy()
        .into_owned();

    let folder = if let Some(node_name) = node_name {
        format!("{}/{}", service_path, node_name)
    } else {
        service_path.to_string()
    };
    if let Err(e) = system::open_folder(&folder) {
        error!("Failed to open folder: {}", e);
    }
    Ok(())
}