Skip to content

Commit

Permalink
Colored Tags: Enable by default & README & Refactor
Browse files Browse the repository at this point in the history
* Enable Colored tags by default
* Include colored tags and their configuration within readme file
* Refactoring & Clippy Fixes
  • Loading branch information
AmmarAbouZor committed Sep 8, 2024
1 parent 0786fab commit 2dc9a51
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ TUI-Journal is a terminal-based application written in Rust that allows you to w
- Intuitive, responsive and user-friendly text-based user interface (TUI).
- Create, edit, and delete entries easily.
- Edit journal content with the built-in editor or use your favourite terminal text editor from within the app.
- Add custom tags to the journals and use them in the built-in filter.
- Add custom colored tags to the journals and use them in the built-in filter.
- Fuzzy Finder: Locate your desired journal with lightning-fast speed.
- Search functions for journals title and content in the built-in filter.
- Sort the journals based on their date, priority and title.
Expand Down Expand Up @@ -219,6 +219,8 @@ sync_os_clipboard = false # Syncs editor clipboard actions with operating syste
history_limit = 10 # Sets the maximum changes limit for the undo & redo stacks. Use 0 to disable it.
colored_tags = true # Sets if automatically coloring for tags is enabled.
[export]
default_path = "<Absolute_path_to_export_directory>" # Optional default path to export multiple journals or a single journal's content. Falls back to the current directory if not specified.
show_confirmation = true # Show confirmation after successful export.
Expand Down
31 changes: 16 additions & 15 deletions src/app/colored_tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ use ratatui::style::Color;
/// Hard coded colors for the tags.
/// Note: the order to pick the colors is from bottom to top because we are popping the colors from
/// the end of the stack.
const TAG_COLORS: &[TagColor] = &[
TagColor::new(Color::Black, Color::LightMagenta),
TagColor::new(Color::Red, Color::Cyan),
TagColor::new(Color::Yellow, Color::Blue),
TagColor::new(Color::Reset, Color::Red),
TagColor::new(Color::Black, Color::LightYellow),
TagColor::new(Color::Reset, Color::DarkGray),
TagColor::new(Color::Black, Color::LightGreen),
TagColor::new(Color::Black, Color::LightRed),
TagColor::new(Color::Black, Color::LightCyan),
const TAG_COLORS: &[TagColors] = &[
TagColors::new(Color::Black, Color::LightMagenta),
TagColors::new(Color::Red, Color::Cyan),
TagColors::new(Color::Yellow, Color::Blue),
TagColors::new(Color::Reset, Color::Red),
TagColors::new(Color::Black, Color::LightYellow),
TagColors::new(Color::Reset, Color::DarkGray),
TagColors::new(Color::Black, Color::LightGreen),
TagColors::new(Color::Black, Color::LightRed),
TagColors::new(Color::Black, Color::LightCyan),
];

#[derive(Debug, Clone)]
/// Manages assigning colors to the tags, keeping track on the assigned colors and providing
/// functions to updating them.
pub struct ColoredTagsManager {
tag_colors_map: HashMap<String, TagColor>,
available_colors: Vec<TagColor>,
tag_colors_map: HashMap<String, TagColors>,
available_colors: Vec<TagColors>,
}

impl ColoredTagsManager {
Expand Down Expand Up @@ -64,18 +64,19 @@ impl ColoredTagsManager {
}

/// Gets the matching color for the giving tag if tag exists.
pub fn get_tag_color(&self, tag: &str) -> Option<TagColor> {
pub fn get_tag_color(&self, tag: &str) -> Option<TagColors> {
self.tag_colors_map.get(tag).copied()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct TagColor {
/// Represents the needed colors for a colored tag
pub struct TagColors {
pub foreground: Color,
pub background: Color,
}

impl TagColor {
impl TagColors {
pub const fn new(foreground: Color, background: Color) -> Self {
Self {
foreground,
Expand Down
6 changes: 3 additions & 3 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub use runner::run;
pub use runner::HandleInputReturnType;
pub use ui::UIComponents;

pub use colored_tags::TagColor;
pub use colored_tags::TagColors;

pub struct App<D>
where
Expand Down Expand Up @@ -63,7 +63,7 @@ where
let selected_entries = HashSet::new();
let filtered_out_entries = HashSet::new();
let history = HistoryManager::new(settings.history_limit);
let colored_tags = settings.colored_tags.then(|| ColoredTagsManager::new());
let colored_tags = settings.colored_tags.then(ColoredTagsManager::new);

Self {
data_provide,
Expand Down Expand Up @@ -412,7 +412,7 @@ where
}

/// Gets the matching color for the giving tag if colored tags are enabled and tag exists.
pub fn get_color_for_tag(&self, tag: &str) -> Option<TagColor> {
pub fn get_color_for_tag(&self, tag: &str) -> Option<TagColors> {
self.colored_tags
.as_ref()
.and_then(|c| c.get_tag_color(tag))
Expand Down
3 changes: 1 addition & 2 deletions src/app/ui/entries_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ impl<'a> EntriesList {
.add_modifier(Modifier::DIM);

let mut added_lines = 1;

spans.push(Line::default());

for tag in entry.tags.iter() {
Expand All @@ -158,7 +157,7 @@ impl<'a> EntriesList {
.unwrap_or(tags_default_style);
let span_to_add = Span::styled(tag.to_owned(), style);

if last_line.width() + tag.len() < area.width as usize - LIST_INNER_MARGIN {
if last_line.width() + tag.len() < allowd_width {
last_line.push_span(span_to_add);
} else {
added_lines += 1;
Expand Down
8 changes: 6 additions & 2 deletions src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct Settings {
#[serde(default = "default_history_limit")]
/// Set the maximum size of the history stacks (undo & redo) size.
pub history_limit: usize,
#[serde(default)]
#[serde(default = "default_colored_tags")]
pub colored_tags: bool,
}

Expand All @@ -65,7 +65,7 @@ impl Default for Settings {
scroll_per_page: Default::default(),
sync_os_clipboard: Default::default(),
history_limit: default_history_limit(),
colored_tags: Default::default(),
colored_tags: default_colored_tags(),
}
}
}
Expand All @@ -82,6 +82,10 @@ const fn default_history_limit() -> usize {
10
}

const fn default_colored_tags() -> bool {
true
}

impl Settings {
pub async fn new() -> anyhow::Result<Self> {
let settings_path = get_settings_path()?;
Expand Down

0 comments on commit 2dc9a51

Please sign in to comment.