Skip to content

Commit

Permalink
refactor: remove Default impls and add config parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
RoloEdits committed Dec 30, 2024
1 parent 20e6d29 commit def0d38
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 46 deletions.
2 changes: 1 addition & 1 deletion helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ impl EditorView {

let lang = doc.language_name().unwrap_or(DEFAULT_LANGUAGE_NAME);
let config = editor.config();
let icon = config.icons.mime.lang(lang);
let icon = config.icons.mime.get(lang);

let text = format!(
" {icon} {}{} ",
Expand Down
4 changes: 2 additions & 2 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ where
{
let mime = &context.editor.config().icons.mime;

let icon = mime.lang(context.doc.language_name().unwrap_or(DEFAULT_LANGUAGE_NAME));
let icon = mime.get(context.doc.language_name().unwrap_or(DEFAULT_LANGUAGE_NAME));

write(context, format!(" {} ", icon), None);
}
Expand Down Expand Up @@ -536,7 +536,7 @@ where
let vcs = if head.is_empty() {
format!("{head}")
} else {
format!("{icon}{head}")
format!("{icon} {head}")
};

write(context, vcs, None);
Expand Down
52 changes: 9 additions & 43 deletions helix-view/src/editor/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq, Clone)]
#[serde(default)]
pub struct Icons {
pub mime: Mime,
pub lsp: Lsp,
Expand Down Expand Up @@ -153,25 +154,14 @@ impl Lsp {
}
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Default)]
pub struct Diagnostic {
hint: Option<String>,
info: Option<String>,
warning: Option<String>,
error: Option<String>,
}

impl Default for Diagnostic {
fn default() -> Self {
Self {
hint: Some(String::from("○")),
info: Some(String::from("●")),
warning: Some(String::from("▲")),
error: Some(String::from("■")),
}
}
}

impl Diagnostic {
const DEFAULT: &'static str = "●";

Expand All @@ -191,19 +181,11 @@ impl Diagnostic {
}
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Default)]
pub struct Vcs {
icon: Option<String>,
}

impl Default for Vcs {
fn default() -> Self {
Self {
icon: Some(String::from(" ")),
}
}
}

impl Vcs {
const DEFAULT: &'static str = "";

Expand All @@ -214,37 +196,21 @@ impl Vcs {
}
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Default)]
pub struct Mime {
directory: Option<String>,
#[serde(flatten)]
mime: HashMap<String, String>,
}

impl Default for Mime {
fn default() -> Self {
Self {
directory: None,
mime: {
let mut mime = HashMap::new();
mime.insert(String::from("rust"), String::from("󱘗 "));
mime.insert(String::from("markdown"), String::from(" "));
mime.insert(String::from("css"), String::from("󰌜 "));
mime.insert(String::from("toml"), String::from(" "));
mime.insert(String::from("lock"), String::from("󱌼 "));
mime.insert(String::from("text"), String::from(" "));
mime
},
}
}
}

impl Mime {
pub fn directory(&self) -> &str {
self.directory.as_ref().map_or("🖿 ", |directory| directory)
self.directory.as_ref().map_or("", |directory| directory)
}

pub fn lang(&self, mime: &str) -> &str {
self.mime.get(mime).map_or("*", |mime| mime)
// Returns the symbol that matches the name, if any, otherwise returns the name back.
pub fn get<'name, 'mime: 'name>(&'mime self, r#type: &'name str) -> &'name str {
self.mime.get(r#type).map_or(r#type, |mime| mime)
}
}

Expand Down

0 comments on commit def0d38

Please sign in to comment.