Skip to content

Commit 6a7cae0

Browse files
committed
ui/themes: add NO_COLOR support
https://no-color.org/
1 parent ee65f35 commit 6a7cae0

11 files changed

Lines changed: 494 additions & 242 deletions

File tree

meli.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,10 @@ catchall for general errors
402402
Specifies the editor to use
403403
.It Ev MELI_CONFIG
404404
Override the configuration file
405+
.It Ev NO_COLOR
406+
When present (regardless of its value), prevents the addition of ANSI color. The configuration value
407+
.Ic use_color
408+
overrides this.
405409
.El
406410
.Sh FILES
407411
.Nm

ui/src/components/mail/listing.rs

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,49 +1050,38 @@ impl Listing {
10501050
if idx == lines_len {
10511051
break;
10521052
}
1053-
let (
1054-
fg_color,
1055-
bg_color,
1056-
index_fg_color,
1057-
index_bg_color,
1058-
unread_count_fg,
1059-
unread_count_bg,
1060-
) = if must_highlight_account {
1053+
let (att, index_att, unread_count_att) = if must_highlight_account {
10611054
if self.cursor_pos.1 == idx {
1062-
(
1063-
crate::conf::value(context, "mail.sidebar_highlighted").fg,
1064-
crate::conf::value(context, "mail.sidebar_highlighted").bg,
1065-
crate::conf::value(context, "mail.sidebar_highlighted_index").fg,
1066-
crate::conf::value(context, "mail.sidebar_highlighted_index").bg,
1067-
crate::conf::value(context, "mail.sidebar_highlighted_unread_count").fg,
1068-
crate::conf::value(context, "mail.sidebar_highlighted_unread_count").bg,
1069-
)
1055+
let mut ret = (
1056+
crate::conf::value(context, "mail.sidebar_highlighted"),
1057+
crate::conf::value(context, "mail.sidebar_highlighted_index"),
1058+
crate::conf::value(context, "mail.sidebar_highlighted_unread_count"),
1059+
);
1060+
1061+
if std::env::var("NO_COLOR").is_ok()
1062+
&& (context.settings.terminal.use_color.is_false()
1063+
|| context.settings.terminal.use_color.is_internal())
1064+
{
1065+
ret.0.attrs |= Attr::Reverse;
1066+
ret.1.attrs |= Attr::Reverse;
1067+
ret.2.attrs |= Attr::Reverse;
1068+
}
1069+
ret
10701070
} else {
10711071
(
1072-
crate::conf::value(context, "mail.sidebar_highlighted_account").fg,
1073-
crate::conf::value(context, "mail.sidebar_highlighted_account").bg,
1074-
crate::conf::value(context, "mail.sidebar_highlighted_account_index").fg,
1075-
crate::conf::value(context, "mail.sidebar_highlighted_account_index").bg,
1072+
crate::conf::value(context, "mail.sidebar_highlighted_account"),
1073+
crate::conf::value(context, "mail.sidebar_highlighted_account_index"),
10761074
crate::conf::value(
10771075
context,
10781076
"mail.sidebar_highlighted_account_unread_count",
1079-
)
1080-
.fg,
1081-
crate::conf::value(
1082-
context,
1083-
"mail.sidebar_highlighted_account_unread_count",
1084-
)
1085-
.bg,
1077+
),
10861078
)
10871079
}
10881080
} else {
10891081
(
1090-
crate::conf::value(context, "mail.sidebar").fg,
1091-
crate::conf::value(context, "mail.sidebar").bg,
1092-
crate::conf::value(context, "mail.sidebar_index").fg,
1093-
crate::conf::value(context, "mail.sidebar_index").bg,
1094-
crate::conf::value(context, "mail.sidebar_unread_count").fg,
1095-
crate::conf::value(context, "mail.sidebar_unread_count").bg,
1082+
crate::conf::value(context, "mail.sidebar"),
1083+
crate::conf::value(context, "mail.sidebar_index"),
1084+
crate::conf::value(context, "mail.sidebar_unread_count"),
10961085
)
10971086
};
10981087

@@ -1117,27 +1106,27 @@ impl Listing {
11171106
let (x, _) = write_string_to_grid(
11181107
&format!("{:>width$}", inc, width = total_folder_no_digits),
11191108
grid,
1120-
index_fg_color,
1121-
index_bg_color,
1122-
Attr::Default,
1109+
index_att.fg,
1110+
index_att.bg,
1111+
index_att.attrs,
11231112
(set_y(upper_left, y), bottom_right),
11241113
None,
11251114
);
11261115
let (x, _) = write_string_to_grid(
11271116
&" ".repeat(depth + 1),
11281117
grid,
1129-
fg_color,
1130-
bg_color,
1131-
Attr::Default,
1118+
att.fg,
1119+
att.bg,
1120+
att.attrs,
11321121
((x, y), bottom_right),
11331122
None,
11341123
);
11351124
let (x, _) = write_string_to_grid(
11361125
entries[&folder_idx].name(),
11371126
grid,
1138-
fg_color,
1139-
bg_color,
1140-
Attr::Default,
1127+
att.fg,
1128+
att.bg,
1129+
att.attrs,
11411130
((x, y), bottom_right),
11421131
None,
11431132
);
@@ -1156,13 +1145,14 @@ impl Listing {
11561145
let (x, _) = write_string_to_grid(
11571146
&count_string,
11581147
grid,
1159-
unread_count_fg,
1160-
unread_count_bg,
1161-
if count.unwrap_or(0) > 0 {
1162-
Attr::Bold
1163-
} else {
1164-
Attr::Default
1165-
},
1148+
unread_count_att.fg,
1149+
unread_count_att.bg,
1150+
unread_count_att.attrs
1151+
| if count.unwrap_or(0) > 0 {
1152+
Attr::Bold
1153+
} else {
1154+
Attr::Default
1155+
},
11661156
(
11671157
(
11681158
/* Hide part of folder name if need be to fit the message count */
@@ -1173,7 +1163,9 @@ impl Listing {
11731163
),
11741164
None,
11751165
);
1176-
change_colors(grid, ((x, y), set_y(bottom_right, y)), fg_color, bg_color);
1166+
for c in grid.row_iter(x..(get_x(bottom_right) + 1), y) {
1167+
grid[c].set_fg(att.fg).set_bg(att.bg).set_attrs(att.attrs);
1168+
}
11771169
idx += 1;
11781170
}
11791171
if idx == 0 {

ui/src/components/mail/listing/compact.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,32 @@ impl ListingTrait for CompactListing {
146146
} else {
147147
self.color_cache.odd.bg
148148
};
149+
let attrs = if self.cursor_pos.2 == idx {
150+
self.color_cache.highlighted.attrs
151+
} else if self.selection[&thread_hash] {
152+
self.color_cache.selected.attrs
153+
} else if thread.unseen() > 0 {
154+
self.color_cache.unseen.attrs
155+
} else if idx % 2 == 0 {
156+
self.color_cache.even.attrs
157+
} else {
158+
self.color_cache.odd.attrs
159+
};
149160

150161
let (upper_left, bottom_right) = area;
151-
change_colors(grid, area, fg_color, bg_color);
152162
let x = get_x(upper_left)
153163
+ self.data_columns.widths[0]
154164
+ self.data_columns.widths[1]
155165
+ self.data_columns.widths[2]
156166
+ 3 * 2;
157167

168+
for c in grid.row_iter(
169+
get_x(upper_left)..(get_x(bottom_right) + 1),
170+
get_y(upper_left),
171+
) {
172+
grid[c].set_fg(fg_color).set_bg(bg_color).set_attrs(attrs);
173+
}
174+
158175
copy_area(
159176
grid,
160177
&self.data_columns.columns[3],
@@ -165,7 +182,7 @@ impl ListingTrait for CompactListing {
165182
),
166183
);
167184
for c in grid.row_iter(x..(self.data_columns.widths[3] + x), get_y(upper_left)) {
168-
grid[c].set_bg(bg_color);
185+
grid[c].set_bg(bg_color).set_attrs(attrs);
169186
}
170187
return;
171188
}
@@ -641,6 +658,12 @@ impl CompactListing {
641658
thread_snooze_flag: crate::conf::value(context, "mail.listing.thread_snooze_flag"),
642659
..self.color_cache
643660
};
661+
if std::env::var("NO_COLOR").is_ok()
662+
&& (context.settings.terminal.use_color.is_false()
663+
|| context.settings.terminal.use_color.is_internal())
664+
{
665+
self.color_cache.highlighted.attrs |= Attr::Reverse;
666+
}
644667

645668
// Get mailbox as a reference.
646669
//

ui/src/components/mail/listing/conversations.rs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ impl ListingTrait for ConversationsListing {
119119
} else {
120120
self.color_cache.theme_default.bg
121121
};
122+
let attrs = if self.cursor_pos.2 == idx {
123+
self.color_cache.highlighted.attrs
124+
} else if self.selection[&thread_hash] {
125+
self.color_cache.selected.attrs
126+
} else if thread.unseen() > 0 {
127+
self.color_cache.unseen.attrs
128+
} else {
129+
self.color_cache.theme_default.attrs
130+
};
122131

123132
copy_area(
124133
grid,
@@ -134,26 +143,38 @@ impl ListingTrait for ConversationsListing {
134143
let (x, y) = upper_left;
135144
if self.cursor_pos.2 == idx || self.selection[&thread_hash] {
136145
for x in x..=get_x(bottom_right) {
137-
grid[(x, y)].set_fg(fg_color);
138-
grid[(x, y)].set_bg(bg_color);
146+
grid[(x, y)]
147+
.set_fg(fg_color)
148+
.set_bg(bg_color)
149+
.set_attrs(attrs);
139150

140-
grid[(x, y + 1)].set_fg(fg_color);
141-
grid[(x, y + 1)].set_bg(bg_color);
151+
grid[(x, y + 1)]
152+
.set_fg(fg_color)
153+
.set_bg(bg_color)
154+
.set_attrs(attrs);
142155

143-
grid[(x, y + 2)].set_fg(padding_fg);
144-
grid[(x, y + 2)].set_bg(bg_color);
156+
grid[(x, y + 2)]
157+
.set_fg(padding_fg)
158+
.set_bg(bg_color)
159+
.set_attrs(attrs);
145160
}
146161
} else if width < width!(area) {
147162
/* fill any remaining columns, if our view is wider than self.content */
148163
for x in (x + width)..=get_x(bottom_right) {
149-
grid[(x, y)].set_fg(fg_color);
150-
grid[(x, y)].set_bg(bg_color);
164+
grid[(x, y)]
165+
.set_fg(fg_color)
166+
.set_bg(bg_color)
167+
.set_attrs(attrs);
151168

152-
grid[(x, y + 1)].set_fg(fg_color);
153-
grid[(x, y + 1)].set_bg(bg_color);
169+
grid[(x, y + 1)]
170+
.set_fg(fg_color)
171+
.set_bg(bg_color)
172+
.set_attrs(attrs);
154173

155-
grid[(x, y + 2)].set_fg(padding_fg);
156-
grid[(x, y + 2)].set_bg(bg_color);
174+
grid[(x, y + 2)]
175+
.set_fg(padding_fg)
176+
.set_bg(bg_color)
177+
.set_attrs(attrs);
157178
}
158179
}
159180
return;
@@ -570,6 +591,12 @@ impl ConversationsListing {
570591
..self.color_cache
571592
};
572593

594+
if std::env::var("NO_COLOR").is_ok()
595+
&& (context.settings.terminal.use_color.is_false()
596+
|| context.settings.terminal.use_color.is_internal())
597+
{
598+
self.color_cache.highlighted.attrs |= Attr::Reverse;
599+
}
573600
// Get mailbox as a reference.
574601
//
575602
match context.accounts[self.cursor_pos.0].status(self.folder_hash) {

0 commit comments

Comments
 (0)