Skip to content

Commit 87ce64c

Browse files
Gigoo25mdaser
authored andcommitted
Add the ability to display notes with comments
1 parent 74050e9 commit 87ce64c

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

src/ssh_config_store.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,45 @@ use crate::database::FileDatabase;
22
use anyhow::{format_err, Result};
33
use ssh_cfg::{SshConfig, SshConfigParser, SshHostConfig};
44
use std::fmt::Debug;
5+
use std::collections::HashMap;
6+
use std::fs::read_to_string;
7+
use std::path::PathBuf;
8+
9+
trait ConfigComments {
10+
fn get_comments(&self) -> HashMap<String, String>;
11+
}
12+
13+
impl ConfigComments for SshConfig {
14+
fn get_comments(&self) -> HashMap<String, String> {
15+
let mut comments = HashMap::new();
16+
17+
let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
18+
let config_path = PathBuf::from(home).join(".ssh").join("config");
19+
20+
if let Ok(contents) = read_to_string(config_path) {
21+
let mut current_comment = Vec::new();
22+
23+
for line in contents.lines() {
24+
let trimmed = line.trim();
25+
26+
if trimmed.starts_with('#') {
27+
let comment_text = trimmed[1..].trim().to_string();
28+
current_comment.push(comment_text);
29+
} else if trimmed.starts_with("Host ") {
30+
let host = trimmed["Host ".len()..].trim().to_string();
31+
if !current_comment.is_empty() {
32+
comments.insert(host, current_comment.join("\n"));
33+
current_comment.clear();
34+
}
35+
} else if trimmed.is_empty() {
36+
current_comment.clear();
37+
}
38+
}
39+
}
40+
41+
comments
42+
}
43+
}
544

645
#[derive(Debug)]
746
pub struct SshGroupItem {
@@ -10,6 +49,7 @@ pub struct SshGroupItem {
1049
pub connection_count: i64,
1150
pub last_used: i64,
1251
pub host_config: SshHostConfig,
52+
pub comment: Option<String>,
1353
}
1454

1555
#[derive(Debug)]
@@ -28,12 +68,14 @@ impl SshConfigStore {
2868
pub async fn new(db: &FileDatabase) -> Result<SshConfigStore> {
2969
let ssh_config = SshConfigParser::parse_home().await?;
3070

71+
let comments = ssh_config.get_comments();
72+
3173
let mut scs = SshConfigStore {
3274
config: ssh_config,
3375
groups: Vec::new(),
3476
};
3577

36-
scs.create_ssh_groups(db);
78+
scs.create_ssh_groups(db, &comments);
3779

3880
if scs.groups.is_empty() {
3981
return Err(format_err!("Your configuration file contains no entries (or only wildcards) ! Please add at least one."));
@@ -42,7 +84,7 @@ impl SshConfigStore {
4284
Ok(scs)
4385
}
4486

45-
fn create_ssh_groups(&mut self, db: &FileDatabase) {
87+
fn create_ssh_groups(&mut self, db: &FileDatabase, comments: &std::collections::HashMap<String, String>) {
4688
let mut groups: Vec<SshGroup> = vec![SshGroup {
4789
name: "Others".to_string(),
4890
items: Vec::new(),
@@ -67,6 +109,7 @@ impl SshConfigStore {
67109
last_used: host_entry.last_used_date,
68110
full_name: key.to_string(),
69111
host_config: value.clone(),
112+
comment: comments.get(key).cloned(),
70113
};
71114

72115
if group.is_none() {
@@ -90,6 +133,7 @@ impl SshConfigStore {
90133
last_used: host_entry.last_used_date,
91134
name: key.to_string(),
92135
host_config: value.clone(),
136+
comment: comments.get(key).cloned(),
93137
});
94138
});
95139

src/widgets/config_widget.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ impl ConfigWidget {
6161
}
6262

6363
fn ssh_group_item_to_spans(config: &SshGroupItem) -> Vec<Spans<'_>> {
64-
let mut spans = vec![Spans::from(vec![
64+
let mut spans = Vec::new();
65+
66+
spans.push(Spans::from(vec![
6567
Span::styled("Host ", Style::default().fg(THEME.text_primary())),
6668
Span::styled(
6769
&config.full_name,
6870
Style::default().fg(THEME.text_secondary()),
6971
),
70-
])];
72+
]));
7173

7274
config.host_config.iter().for_each(|(key, value)| {
7375
spans.push(Spans::from(vec![
@@ -78,6 +80,20 @@ impl ConfigWidget {
7880
]));
7981
});
8082

83+
if let Some(comment) = &config.comment {
84+
spans.push(Spans::from(vec![Span::styled(
85+
" Notes",
86+
Style::default().fg(THEME.text_primary()),
87+
)]));
88+
89+
for line in comment.lines() {
90+
spans.push(Spans::from(vec![
91+
Span::styled(" ", Style::default().fg(THEME.text_primary())),
92+
Span::styled(line, Style::default().fg(THEME.text_secondary())),
93+
]));
94+
}
95+
}
96+
8197
spans.push(Spans::from(vec![Span::styled(
8298
"\n",
8399
Style::default().fg(THEME.text_secondary()),

0 commit comments

Comments
 (0)