below_view/viewrc.rs
1// Copyright (c) Facebook, Inc. and its affiliates.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use serde::Deserialize;
16
17use super::get_belowrc_filename;
18use super::get_belowrc_view_section_key;
19
20/// Enum of supported front view.
21// We didn't re-use the MainViewState because we don't want to
22// expose internal state like Process(ProcessZoomState::Cgroup)
23#[derive(Clone, Deserialize)]
24#[serde(rename_all = "lowercase")]
25pub enum DefaultFrontView {
26 Cgroup,
27 Process,
28 System,
29}
30
31#[derive(Default, Deserialize)]
32pub struct SummaryViewExtraRowItem {
33 pub alias: Option<String>,
34 pub field_id: String,
35}
36
37#[derive(Default, Deserialize)]
38pub struct SummaryViewExtraRow {
39 pub title: Option<String>,
40 pub items: Vec<SummaryViewExtraRowItem>,
41}
42
43/// Runtime configuration on the below view.
44#[derive(Default, Deserialize)]
45pub struct ViewRc {
46 // The default front view. If this field is not set, we will use cgroup
47 // view as front view
48 pub default_view: Option<DefaultFrontView>,
49 // If we want to collapse all top level cgroups. If this field is not set,
50 // it will be treated as false
51 pub collapse_cgroups: Option<bool>,
52 // Overrides cgroup name column width.
53 pub cgroup_name_width: Option<usize>,
54 // Extra rows to add in the summary view.
55 pub summary_view_extra_rows: Option<Vec<SummaryViewExtraRow>>,
56}
57
58impl ViewRc {
59 /// Create a new ViewRc object base on the content in
60 /// $HOME/.config/below/belowrc. Will return default ViewRc if the belowrc
61 /// file is missing or view section does not exists. Optionally return a
62 /// parse error string.
63 pub fn new() -> (ViewRc, Option<String>) {
64 match std::fs::read_to_string(get_belowrc_filename()) {
65 Ok(belowrc_str) => match belowrc_str.parse::<toml::value::Value>() {
66 // We get the belowrc file, parsing the [view] section
67 Ok(belowrc_val) => {
68 if let Some(viewrc_val) = belowrc_val.get(get_belowrc_view_section_key()) {
69 // Got the [view] section, let's see if we can deserialize it to ViewRc
70 match viewrc_val.to_owned().try_into::<ViewRc>() {
71 Ok(viewrc) => (viewrc, None),
72 Err(e) => (
73 Default::default(),
74 Some(format!(
75 "Failed to parse belowrc::{}: {}",
76 get_belowrc_view_section_key(),
77 e
78 )),
79 ),
80 }
81 } else {
82 Default::default()
83 }
84 }
85 Err(e) => (
86 Default::default(),
87 Some(format!("Failed to parse belowrc: {}", e)),
88 ),
89 },
90 _ => (Default::default(), None),
91 }
92 }
93}