Skip to content

Commit

Permalink
feat: support for colored messages (#311)
Browse files Browse the repository at this point in the history
This option is disabled by default, but can be enabled by setting the
`colored_messages` config option to `true`. The coloring is the same as
the one used for the author name.

Closes #285
  • Loading branch information
boxdot authored Sep 16, 2024
1 parent 20a948e commit 45f32f6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# Changelog

## 0.5.2-dev

### Added

- Add `colored_messages` config option

## 0.5.1

## Added
### Added

- Edit messages ([#301])

## Fixes
### Fixes

- Fix unexpected response HTTP 409 during linking ([#299])

Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub struct Config {
#[serde(default)]
/// If set, enables encryption of the key store and messages database
pub passphrase: Option<String>,
/// If set, the full message text will be colored, not only the author name
#[serde(default)]
pub colored_messages: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -73,6 +76,7 @@ impl Config {
developer: Default::default(),
sqlite: Default::default(),
passphrase: None,
colored_messages: false,
}
}

Expand Down
56 changes: 43 additions & 13 deletions src/ui/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,15 @@ fn draw_messages(f: &mut Frame, app: &mut App, area: Rect) {
};
let date_division = display_date_line(msg.arrived_at, &mut previous_msg_day, width);
let show_receipt = ShowReceipt::from_msg(&msg, app.user_id, app.config.show_receipts);
let msg = display_message(&names, &msg, &prefix, width, height, show_receipt);
let msg = display_message(
&names,
&msg,
&prefix,
width,
height,
show_receipt,
app.config.colored_messages,
);
[date_division, msg]
})
.flatten();
Expand Down Expand Up @@ -451,6 +459,7 @@ fn display_message(
width: usize,
height: usize,
show_receipt: ShowReceipt,
colored_messages: bool,
) -> Option<ListItem<'static>> {
let receipt = Span::styled(
display_receipt(msg.receipt, show_receipt),
Expand Down Expand Up @@ -515,17 +524,22 @@ fn display_message(
time.clone(),
from.clone(),
delimiter.clone(),
Span::styled(line.strip_prefix(prefix).unwrap().to_string(), quote_style),
Span::styled(line.strip_prefix(prefix).unwrap().to_owned(), quote_style),
]
} else {
vec![Span::styled(line.to_string(), quote_style)]
vec![Span::styled(line.into_owned(), quote_style)]
};
Line::from(res)
})
.collect();
}

let add_time = spans.is_empty();
let message_style = if colored_messages {
Style::default().fg(from_color)
} else {
Style::default()
};
spans.extend(
textwrap::wrap(&text, &wrap_opts)
.into_iter()
Expand All @@ -537,10 +551,10 @@ fn display_message(
time.clone(),
from.clone(),
delimiter.clone(),
Span::from(line.strip_prefix(prefix).unwrap().to_string()),
Span::styled(line.strip_prefix(prefix).unwrap().to_owned(), message_style),
]
} else {
vec![Span::from(line.into_owned())]
vec![Span::styled(line.into_owned(), message_style)]
};
Line::from(res)
}),
Expand Down Expand Up @@ -802,7 +816,15 @@ mod tests {
attachments: vec![test_attachment()],
..test_message()
};
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, ShowReceipt::Never);
let rendered = display_message(
&names,
&msg,
PREFIX,
WIDTH,
HEIGHT,
ShowReceipt::Never,
false,
);

let expected = ListItem::new(Text::from(vec![
Line::from(vec![
Expand Down Expand Up @@ -830,7 +852,15 @@ mod tests {
attachments: vec![test_attachment()],
..test_message()
};
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, ShowReceipt::Never);
let rendered = display_message(
&names,
&msg,
PREFIX,
WIDTH,
HEIGHT,
ShowReceipt::Never,
false,
);

let expected = ListItem::new(Text::from(vec![
Line::from(vec![
Expand Down Expand Up @@ -862,7 +892,7 @@ mod tests {
..test_message()
};
let show_receipt = ShowReceipt::from_msg(&msg, USER_ID, true);
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, show_receipt);
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, show_receipt, false);

let expected = ListItem::new(Text::from(vec![Line::from(vec![
Span::styled("○ ", Style::default().fg(Color::Yellow)),
Expand All @@ -886,7 +916,7 @@ mod tests {
..test_message()
};
let show_receipt = ShowReceipt::from_msg(&msg, USER_ID, true);
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, show_receipt);
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, show_receipt, false);

let expected = ListItem::new(Text::from(vec![Line::from(vec![
Span::styled("◉ ", Style::default().fg(Color::Yellow)),
Expand All @@ -910,7 +940,7 @@ mod tests {
..test_message()
};
let show_receipt = ShowReceipt::from_msg(&msg, USER_ID, true);
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, show_receipt);
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, show_receipt, false);

let expected = ListItem::new(Text::from(vec![Line::from(vec![
Span::styled("● ", Style::default().fg(Color::Yellow)),
Expand All @@ -934,7 +964,7 @@ mod tests {
..test_message()
};
let show_receipt = ShowReceipt::from_msg(&msg, USER_ID, false);
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, show_receipt);
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, show_receipt, false);

let expected = ListItem::new(Text::from(vec![Line::from(vec![
Span::styled("", Style::default().fg(Color::Yellow)),
Expand All @@ -960,7 +990,7 @@ mod tests {
..test_message()
};
let show_receipt = ShowReceipt::from_msg(&msg, USER_ID, true);
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, show_receipt);
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, show_receipt, false);

let expected = ListItem::new(Text::from(vec![Line::from(vec![
Span::styled(" ", Style::default().fg(Color::Yellow)),
Expand Down Expand Up @@ -998,7 +1028,7 @@ mod tests {
..test_message()
};
let show_receipt = ShowReceipt::from_msg(&msg, USER_ID, true);
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, show_receipt);
let rendered = display_message(&names, &msg, PREFIX, WIDTH, HEIGHT, show_receipt, false);

let expected = ListItem::new(Text::from(vec![
Line::from(vec![
Expand Down

0 comments on commit 45f32f6

Please sign in to comment.