node_launchpad/components/
utils.rs

1// Copyright 2024 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use crate::system;
10use ant_node_manager::config::get_service_log_dir_path;
11use ant_releases::ReleaseType;
12use color_eyre::eyre::{self};
13use ratatui::prelude::*;
14
15/// helper function to create a centered rect using up certain percentage of the available rect `r`
16pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
17    let popup_layout = Layout::vertical([
18        Constraint::Percentage((100 - percent_y) / 2),
19        Constraint::Percentage(percent_y),
20        Constraint::Percentage((100 - percent_y) / 2),
21    ])
22    .split(r);
23
24    Layout::horizontal([
25        Constraint::Percentage((100 - percent_x) / 2),
26        Constraint::Percentage(percent_x),
27        Constraint::Percentage((100 - percent_x) / 2),
28    ])
29    .split(popup_layout[1])[1]
30}
31
32/// Helper function to create a centered rect using a fixed x,y constraint.
33pub fn centered_rect_fixed(x: u16, y: u16, r: Rect) -> Rect {
34    let popup_layout = Layout::vertical([
35        Constraint::Fill(1),
36        Constraint::Length(y),
37        Constraint::Fill(1),
38    ])
39    .split(r);
40
41    Layout::horizontal([
42        Constraint::Fill(1),
43        Constraint::Length(x),
44        Constraint::Fill(1),
45    ])
46    .split(popup_layout[1])[1]
47}
48
49/// Opens the logs folder for a given node service name or the default service log directory.
50///
51/// # Parameters
52///
53/// * `node_name`: Optional node service name. If `None`, the default service log directory is used.
54///
55/// # Returns
56///
57/// A `Result` indicating the success or failure of the operation.
58pub fn open_logs(node_name: Option<String>) -> Result<(), eyre::Report> {
59    let service_path = get_service_log_dir_path(ReleaseType::NodeLaunchpad, None, None)?
60        .to_string_lossy()
61        .into_owned();
62
63    let folder = if let Some(node_name) = node_name {
64        format!("{}/{}", service_path, node_name)
65    } else {
66        service_path.to_string()
67    };
68    if let Err(e) = system::open_folder(&folder) {
69        error!("Failed to open folder: {}", e);
70    }
71    Ok(())
72}