2006-03-04 18:16:49 +00:00
|
|
|
/*
|
|
|
|
htop - ListItem.c
|
2011-05-26 16:35:07 +00:00
|
|
|
(C) 2004-2011 Hisham H. Muhammad
|
2020-10-05 07:51:32 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
2006-03-04 18:16:49 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
#include "ListItem.h"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
#include "CRT.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
#include "RichString.h"
|
2020-10-14 18:21:09 +00:00
|
|
|
#include "XUtils.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void ListItem_delete(Object* cast) {
|
|
|
|
ListItem* this = (ListItem*)cast;
|
|
|
|
free(this->value);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2020-10-06 10:28:11 +00:00
|
|
|
static void ListItem_display(const Object* cast, RichString* out) {
|
|
|
|
const ListItem* const this = (const ListItem*)cast;
|
2008-03-09 08:58:38 +00:00
|
|
|
assert (this != NULL);
|
2020-10-18 17:22:30 +00:00
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
if (this->moving) {
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_writeWide(out, CRT_colors[DEFAULT_COLOR],
|
2015-07-17 12:33:34 +00:00
|
|
|
#ifdef HAVE_LIBNCURSESW
|
2020-10-31 21:14:27 +00:00
|
|
|
CRT_utf8 ? "↕ " :
|
2015-07-17 12:33:34 +00:00
|
|
|
#endif
|
2020-10-31 21:14:27 +00:00
|
|
|
"+ ");
|
2015-01-22 01:27:31 +00:00
|
|
|
} else {
|
|
|
|
RichString_prune(out);
|
|
|
|
}
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendWide(out, CRT_colors[DEFAULT_COLOR], this->value);
|
2008-03-09 08:58:38 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
ListItem* ListItem_new(const char* value, int key) {
|
2012-12-05 15:12:20 +00:00
|
|
|
ListItem* this = AllocThis(ListItem);
|
2016-02-02 14:53:02 +00:00
|
|
|
this->value = xStrdup(value);
|
2006-03-04 18:16:49 +00:00
|
|
|
this->key = key;
|
2015-01-22 01:27:31 +00:00
|
|
|
this->moving = false;
|
2006-03-04 18:16:49 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-11-10 00:31:37 +00:00
|
|
|
void ListItem_append(ListItem* this, const char* text) {
|
2020-10-18 17:22:30 +00:00
|
|
|
size_t oldLen = strlen(this->value);
|
|
|
|
size_t textLen = strlen(text);
|
|
|
|
size_t newLen = oldLen + textLen;
|
2016-02-02 14:53:02 +00:00
|
|
|
this->value = xRealloc(this->value, newLen + 1);
|
2012-11-10 00:31:37 +00:00
|
|
|
memcpy(this->value + oldLen, text, textLen);
|
|
|
|
this->value[newLen] = '\0';
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 17:22:30 +00:00
|
|
|
static long ListItem_compare(const void* cast1, const void* cast2) {
|
2020-09-23 12:15:51 +00:00
|
|
|
const ListItem* obj1 = (const ListItem*) cast1;
|
|
|
|
const ListItem* obj2 = (const ListItem*) cast2;
|
2006-03-04 18:16:49 +00:00
|
|
|
return strcmp(obj1->value, obj2->value);
|
|
|
|
}
|
2020-10-18 17:22:30 +00:00
|
|
|
|
|
|
|
const ObjectClass ListItem_class = {
|
|
|
|
.display = ListItem_display,
|
|
|
|
.delete = ListItem_delete,
|
|
|
|
.compare = ListItem_compare
|
|
|
|
};
|