diff options
author | Daniel Lange <DLange@git.local> | 2016-04-11 13:00:15 +0200 |
---|---|---|
committer | Daniel Lange <DLange@git.local> | 2016-04-11 13:00:15 +0200 |
commit | d3c9975943df58e293359b87905d19ff1fd52061 (patch) | |
tree | e416378879f60e8d538b1b25963904f767d30ff4 /RichString.c | |
download | debian_htop-d3c9975943df58e293359b87905d19ff1fd52061.tar.gz debian_htop-d3c9975943df58e293359b87905d19ff1fd52061.tar.bz2 debian_htop-d3c9975943df58e293359b87905d19ff1fd52061.zip |
Imported Upstream version 0.5upstream/0.5
Diffstat (limited to 'RichString.c')
-rw-r--r-- | RichString.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/RichString.c b/RichString.c new file mode 100644 index 0000000..63db850 --- /dev/null +++ b/RichString.c @@ -0,0 +1,80 @@ + +#include "RichString.h" + +#include <stdlib.h> +#include <string.h> +#include <curses.h> +#include <sys/param.h> + +#include "debug.h" +#include <assert.h> + +#define RICHSTRING_MAXLEN 300 + +/*{ + +typedef struct RichString_ { + int len; + chtype chstr[RICHSTRING_MAXLEN+1]; +} RichString; + +}*/ + +/* private property */ +WINDOW* workArea = NULL; + +RichString RichString_new() { + RichString this; + this.len = 0; + return this; +} + +void RichString_delete(RichString this) { +} + +void RichString_prune(RichString* this) { + this->len = 0; +} + +void RichString_write(RichString* this, int attrs, char* data) { + this->len = 0; + RichString_append(this, attrs, data); +} + +inline void RichString_append(RichString* this, int attrs, char* data) { + RichString_appendn(this, attrs, data, strlen(data)); +} + +inline void RichString_appendn(RichString* this, int attrs, char* data, int len) { + if (!workArea) { + workArea = newpad(1, RICHSTRING_MAXLEN); + } + assert(workArea); + wattrset(workArea, attrs); + int maxToWrite = (RICHSTRING_MAXLEN - 1) - this->len; + int wrote = MIN(maxToWrite, len); + mvwaddnstr(workArea, 0, 0, data, maxToWrite); + int oldstrlen = this->len; + this->len += wrote; + mvwinchnstr(workArea, 0, 0, this->chstr + oldstrlen, wrote); + wattroff(workArea, attrs); +} + +void RichString_setAttr(RichString *this, int attrs) { + for (int i = 0; i < this->len; i++) { + char c = this->chstr[i]; + this->chstr[i] = c | attrs; + } +} + +void RichString_applyAttr(RichString *this, int attrs) { + for (int i = 0; i < this->len - 1; i++) { + this->chstr[i] |= attrs; + } +} + +RichString RichString_quickString(int attrs, char* data) { + RichString str = RichString_new(); + RichString_write(&str, attrs, data); + return str; +} |