Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement dynamic indentation #20

Merged
merged 1 commit into from
Nov 3, 2024
Merged
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
39 changes: 33 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ impl HyprlandConfig {
}
}

fn detect_indentation(&self, content: &[String]) -> usize {
for line in content {
if let Some(indent) = line
.chars()
.take_while(|c| c.is_whitespace())
.count()
.checked_sub(0)
{
if indent > 0 {
return indent;
}
}
}
4
}

pub fn add_entry(&mut self, category: &str, entry: &str) {
let parts: Vec<&str> = category.split('.').collect();
let parent_category = if parts.len() > 1 {
Expand All @@ -126,6 +142,12 @@ impl HyprlandConfig {
let depth = parent_category.matches('.').count();
let key = entry.split('=').next().unwrap().trim();

let indent_size = if let Some(content) = self.sourced_content.get(source_index) {
self.detect_indentation(content)
} else {
4
};

let mut should_update_sections = false;
let mut content_updated = String::new();

Expand All @@ -134,17 +156,20 @@ impl HyprlandConfig {

if parts.len() > 1 && !self.sourced_sections.contains_key(&subcategory_key) {
let last_part = parts.last().unwrap();
let section_start = format!("{}{} {{", " ".repeat(depth + 1), last_part);
let section_end = format!("{}}}", " ".repeat(depth + 1));
let section_start =
format!("{}{} {{", " ".repeat(indent_size * (depth + 1)), last_part);
let section_end = format!("{}}}", " ".repeat(indent_size * (depth + 1)));

if end > 0 && !sourced_content[end - 1].trim().is_empty() {
sourced_content.insert(end, String::new());
end += 1;
}

sourced_content.insert(end, section_start);
sourced_content
.insert(end + 1, format!("{}{}", " ".repeat(depth + 2), entry));
sourced_content.insert(
end + 1,
format!("{}{}", " ".repeat(indent_size * (depth + 2)), entry),
);
sourced_content.insert(end + 2, section_end);

self.sourced_sections
Expand All @@ -159,8 +184,9 @@ impl HyprlandConfig {
category.to_string()
};
let depth = parent_category.matches('.').count();
let formatted_entry =
format!("{}{}", " ".repeat(indent_size * (depth + 1)), entry);

let formatted_entry = format!("{}{}", " ".repeat(depth + 1), entry);
let existing_line = sourced_content[sub_start..=sub_end]
.iter()
.position(|line| line.trim().starts_with(key));
Expand All @@ -175,7 +201,8 @@ impl HyprlandConfig {
}
}
} else {
let formatted_entry = format!("{}{}", " ".repeat(depth + 1), entry);
let formatted_entry =
format!("{}{}", " ".repeat(indent_size * (depth + 1)), entry);
let existing_line = sourced_content[start..=end]
.iter()
.position(|line| line.trim().starts_with(key));
Expand Down