1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-05 22:00:54 +00:00

[status] Boolean option ui.show_mem

Display available memory from /proc/meminfo.
This commit is contained in:
Witold Filipczyk 2024-04-25 16:44:39 +02:00
parent 0f1e056875
commit 23c5c8b866
2 changed files with 68 additions and 0 deletions

View File

@ -64,6 +64,7 @@ enum led_option {
LEDS_CLOCK_ALIAS,
LEDS_SHOW_IP_ENABLE,
LEDS_SHOW_MEM_ENABLE,
LEDS_TEMPERATURE_TREE,
LEDS_TEMPERATURE_ENABLE,
@ -96,6 +97,10 @@ static union option_info led_options[] = {
"show_ip", OPT_ZERO, 0,
N_("Whether to display IP of the document in the status bar.")),
INIT_OPT_BOOL("ui", N_("Show available memory"),
"show_mem", OPT_ZERO, 0,
N_("Whether to display available memory. From /proc/meminfo.")),
INIT_OPT_TREE("ui", N_("Temperature"),
"temperature", OPT_ZERO, N_("Temperature of CPU.")),
@ -125,6 +130,7 @@ static union option_info led_options[] = {
#define get_leds_clock_format() get_opt_leds(LEDS_CLOCK_FORMAT).string
#define get_leds_panel_enable() get_opt_leds(LEDS_PANEL_ENABLE).number
#define get_leds_show_ip_enable() get_opt_leds(LEDS_SHOW_IP_ENABLE).number
#define get_leds_show_mem_enable() get_opt_leds(LEDS_SHOW_MEM_ENABLE).number
#define get_leds_temperature_enable() get_opt_leds(LEDS_TEMPERATURE_ENABLE).number
#define get_leds_temperature_filename() get_opt_leds(LEDS_TEMPERATURE_FILENAME).string
@ -209,6 +215,58 @@ draw_show_ip(struct session *ses, int xpos, int ypos, struct color_pair *color)
return 0;
}
static int
draw_show_mem(struct session *ses, int xpos, int ypos, struct color_pair *color)
{
struct terminal *term = ses->tab->term;
FILE *f;
struct string text;
int i;
int length;
char *pos;
size_t ret = 0;
f = fopen("/proc/meminfo", "r");
if (!f) {
return 0;
}
while (!feof(f)) {
char buffer[128];
if (!fgets(buffer, 127, f)) {
break;
}
if (strncmp(buffer, "MemAvailable:", sizeof("MemAvailable:")-1)) {
continue;
}
if (sscanf(buffer, "MemAvailable:%ld", &ret) < 1) {
ret = 0;
break;
} else {
break;
}
}
fclose(f);
if (ret < 1) {
return 0;
}
if (!init_string(&text)) {
return 0;
}
add_format_to_string(&text, "[%ld MiB]", ret / 1024);
length = text.length;
for (i = 0, pos = text.source; i < length; i++) {
draw_char(term, xpos - length + i, ypos, pos[i], 0, color);
}
done_string(&text);
return length;
}
static int
draw_temperature(struct session *ses, int xpos, int ypos, struct color_pair *color)
{
@ -332,6 +390,12 @@ draw_leds(struct session *ses)
if (color) term->leds_length += draw_temperature(ses, xpos - term->leds_length, ypos, color);
}
if (get_leds_show_mem_enable()) {
struct color_pair *color = get_bfu_color(term, "status.showmem-text");
if (color) term->leds_length += draw_show_mem(ses, xpos - term->leds_length, ypos, color);
}
if (get_leds_show_ip_enable()) {
struct color_pair *color = get_bfu_color(term, "status.showip-text");

View File

@ -1356,6 +1356,10 @@ static union option_info config_options_info[] = {
"showip-text", "black", "white", "black", "white",
N_("Status bar show ip text colors.")),
INIT_OPT_COLORS(".status", N_("Status bar show available memory"),
"showmem-text", "black", "white", "black", "white",
N_("Status bar show available memory text colors.")),
INIT_OPT_COLORS(".status", N_("Generic status bar"),
"status-bar", "black", "white", "black", "white",
N_("Generic status bar colors.")),