Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct App {
pub config_paragraph_offset: u16,
pub db: FileDatabase,
pub show_help: bool,
pub config: Config,
pub _config: Config,
}

impl App {
Expand All @@ -52,7 +52,7 @@ impl App {
db,
searcher: Searcher::new(),
show_help: false,
config,
_config: config,
})
}

Expand Down
48 changes: 46 additions & 2 deletions src/ssh_config_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,45 @@ use crate::database::FileDatabase;
use anyhow::{format_err, Result};
use ssh_cfg::{SshConfig, SshConfigParser, SshHostConfig};
use std::fmt::Debug;
use std::collections::HashMap;
use std::fs::read_to_string;
use std::path::PathBuf;

trait ConfigComments {
fn get_comments(&self) -> HashMap<String, String>;
}

impl ConfigComments for SshConfig {
fn get_comments(&self) -> HashMap<String, String> {
let mut comments = HashMap::new();

let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
let config_path = PathBuf::from(home).join(".ssh").join("config");

if let Ok(contents) = read_to_string(config_path) {
let mut current_comment = Vec::new();

for line in contents.lines() {
let trimmed = line.trim();

if trimmed.starts_with('#') {
let comment_text = trimmed[1..].trim().to_string();
current_comment.push(comment_text);
} else if trimmed.starts_with("Host ") {
let host = trimmed["Host ".len()..].trim().to_string();
if !current_comment.is_empty() {
comments.insert(host, current_comment.join("\n"));
current_comment.clear();
}
} else if trimmed.is_empty() {
current_comment.clear();
}
}
}

comments
}
}

#[derive(Debug)]
pub struct SshGroupItem {
Expand All @@ -10,6 +49,7 @@ pub struct SshGroupItem {
pub connection_count: i64,
pub last_used: i64,
pub host_config: SshHostConfig,
pub comment: Option<String>,
}

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

let comments = ssh_config.get_comments();

let mut scs = SshConfigStore {
config: ssh_config,
groups: Vec::new(),
};

scs.create_ssh_groups(db);
scs.create_ssh_groups(db, &comments);

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

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

if group.is_none() {
Expand All @@ -90,6 +133,7 @@ impl SshConfigStore {
last_used: host_entry.last_used_date,
name: key.to_string(),
host_config: value.clone(),
comment: comments.get(key).cloned(),
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/widgets/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tui::{
widgets::{Block, Borders},
};

pub fn new(title: &str) -> Block {
pub fn new(title: &str) -> Block<'_> {
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(THEME.border_color()))
Expand Down
22 changes: 19 additions & 3 deletions src/widgets/config_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ impl ConfigWidget {
.wrap(Wrap { trim: false })
}

fn ssh_group_item_to_spans(config: &SshGroupItem) -> Vec<Spans> {
let mut spans = vec![Spans::from(vec![
fn ssh_group_item_to_spans(config: &SshGroupItem) -> Vec<Spans<'_>> {
let mut spans = Vec::new();

spans.push(Spans::from(vec![
Span::styled("Host ", Style::default().fg(THEME.text_primary())),
Span::styled(
&config.full_name,
Style::default().fg(THEME.text_secondary()),
),
])];
]));

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

if let Some(comment) = &config.comment {
spans.push(Spans::from(vec![Span::styled(
" Notes",
Style::default().fg(THEME.text_primary()),
)]));

for line in comment.lines() {
spans.push(Spans::from(vec![
Span::styled(" ", Style::default().fg(THEME.text_primary())),
Span::styled(line, Style::default().fg(THEME.text_secondary())),
]));
}
}

spans.push(Spans::from(vec![Span::styled(
"\n",
Style::default().fg(THEME.text_secondary()),
Expand Down