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