Skip to content

Commit

Permalink
Support configuring user gutter width (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
mordquist committed Mar 24, 2024
1 parent 23a729e commit 46e081b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
4 changes: 4 additions & 0 deletions docs/iamb.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ overridden as described in *PROFILES*.
> *protocol.font_size* is an optional list of two numbers representing font
> width and height in pixels.
**user_gutter_width** (type: usize)
> Specify the width of the column where usernames are displayed in a room.
> Usernames that are too long are truncated.
## USER OVERRIDES

Overrides are mapped onto matrix User IDs such as _@user:matrix.org_ and are
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ pub struct TunableValues {
pub open_command: Option<Vec<String>>,
pub notifications: Notifications,
pub image_preview: Option<ImagePreviewValues>,
pub user_gutter_width: usize,
}

#[derive(Clone, Default, Deserialize)]
Expand All @@ -507,6 +508,7 @@ pub struct Tunables {
pub open_command: Option<Vec<String>>,
pub notifications: Option<Notifications>,
pub image_preview: Option<ImagePreview>,
pub user_gutter_width: Option<usize>,
}

impl Tunables {
Expand All @@ -533,6 +535,7 @@ impl Tunables {
open_command: self.open_command.or(other.open_command),
notifications: self.notifications.or(other.notifications),
image_preview: self.image_preview.or(other.image_preview),
user_gutter_width: self.user_gutter_width.or(other.user_gutter_width),
}
}

Expand All @@ -555,6 +558,7 @@ impl Tunables {
open_command: self.open_command,
notifications: self.notifications.unwrap_or_default(),
image_preview: self.image_preview.map(ImagePreview::values),
user_gutter_width: self.user_gutter_width.unwrap_or(30),
}
}
}
Expand Down
37 changes: 20 additions & 17 deletions src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,10 @@ const BOLD_STYLE: Style = Style {
underline_color: None,
};

const USER_GUTTER: usize = 30;
const TIME_GUTTER: usize = 12;
const READ_GUTTER: usize = 5;
const MIN_MSG_LEN: usize = 30;

const USER_GUTTER_EMPTY: &str = " ";
const USER_GUTTER_EMPTY_SPAN: Span<'static> = span_static(USER_GUTTER_EMPTY);

const TIME_GUTTER_EMPTY: &str = " ";
const TIME_GUTTER_EMPTY_SPAN: Span<'static> = span_static(TIME_GUTTER_EMPTY);

Expand Down Expand Up @@ -576,10 +572,13 @@ impl<'a> MessageFormatter<'a> {
text.lines.push(Line::from(vec![leading, date, trailing]));
}

let user_gutter_empty_span =
space_span(self.settings.tunables.user_gutter_width, Style::default());

match self.cols {
MessageColumns::Four => {
let settings = self.settings;
let user = self.user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN);
let user = self.user.take().unwrap_or(user_gutter_empty_span);
let time = self.time.take().unwrap_or(TIME_GUTTER_EMPTY_SPAN);

let mut line = vec![user];
Expand All @@ -604,7 +603,7 @@ impl<'a> MessageFormatter<'a> {
text.lines.push(Line::from(line))
},
MessageColumns::Three => {
let user = self.user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN);
let user = self.user.take().unwrap_or(user_gutter_empty_span);
let time = self.time.take().unwrap_or_else(|| Span::from(""));

let mut line = vec![user];
Expand All @@ -614,7 +613,7 @@ impl<'a> MessageFormatter<'a> {
text.lines.push(Line::from(line))
},
MessageColumns::Two => {
let user = self.user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN);
let user = self.user.take().unwrap_or(user_gutter_empty_span);
let mut line = vec![user];
line.extend(prev_line.spans);

Expand Down Expand Up @@ -743,28 +742,29 @@ impl Message {
Some(prev) if prev.timestamp.same_day(&self.timestamp) => None,
_ => self.timestamp.show_date(),
};
let user_gutter = settings.tunables.user_gutter_width;

if USER_GUTTER + TIME_GUTTER + READ_GUTTER + MIN_MSG_LEN <= width &&
if user_gutter + TIME_GUTTER + READ_GUTTER + MIN_MSG_LEN <= width &&
settings.tunables.read_receipt_display
{
let cols = MessageColumns::Four;
let fill = width - USER_GUTTER - TIME_GUTTER - READ_GUTTER;
let fill = width - user_gutter - TIME_GUTTER - READ_GUTTER;
let user = self.show_sender(prev, true, info, settings);
let time = self.timestamp.show_time();
let read = info.event_receipts.get(self.event.event_id()).map(|read| read.iter());

MessageFormatter { settings, cols, orig, fill, user, date, time, read }
} else if USER_GUTTER + TIME_GUTTER + MIN_MSG_LEN <= width {
} else if user_gutter + TIME_GUTTER + MIN_MSG_LEN <= width {
let cols = MessageColumns::Three;
let fill = width - USER_GUTTER - TIME_GUTTER;
let fill = width - user_gutter - TIME_GUTTER;
let user = self.show_sender(prev, true, info, settings);
let time = self.timestamp.show_time();
let read = None;

MessageFormatter { settings, cols, orig, fill, user, date, time, read }
} else if USER_GUTTER + MIN_MSG_LEN <= width {
} else if user_gutter + MIN_MSG_LEN <= width {
let cols = MessageColumns::Two;
let fill = width - USER_GUTTER;
let fill = width - user_gutter;
let user = self.show_sender(prev, true, info, settings);
let time = None;
let read = None;
Expand All @@ -787,11 +787,13 @@ impl Message {
prev: Option<&Message>,
vwctx: &ViewportContext<MessageCursor>,
info: &'a RoomInfo,
settings: &'a ApplicationSettings,
) -> Option<(&dyn Protocol, u16, u16)> {
let width = vwctx.get_width();
let user_gutter = settings.tunables.user_gutter_width;
// The x position where get_render_format would render the text.
let x = (if USER_GUTTER + MIN_MSG_LEN <= width {
USER_GUTTER
let x = (if user_gutter + MIN_MSG_LEN <= width {
user_gutter
} else {
0
}) as u16;
Expand Down Expand Up @@ -1029,8 +1031,9 @@ impl Message {
}

let Span { content, style } = self.sender_span(info, settings);
let ((truncated, width), _) = take_width(content, 28);
let padding = 28 - width;
let user_gutter = settings.tunables.user_gutter_width;
let ((truncated, width), _) = take_width(content, user_gutter - 2);
let padding = user_gutter - 2 - width;

let sender = if align_right {
space(padding) + &truncated + " "
Expand Down
1 change: 1 addition & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ pub fn mock_tunables() -> TunableValues {
message_user_color: false,
notifications: Notifications { enabled: false, show_message: None },
image_preview: None,
user_gutter_width: 30,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/windows/room/scrollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ impl<'a> StatefulWidget for Scrollback<'a> {
let txt = item.show(prev, foc && sel, &state.viewctx, info, settings);

let mut msg_preview = if picker.is_some() {
item.line_preview(prev, &state.viewctx, info)
item.line_preview(prev, &state.viewctx, info, settings)
} else {
None
};
Expand Down

0 comments on commit 46e081b

Please sign in to comment.