Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions DisplayOptionsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static HandlerResult DisplayOptionsPanel_eventHandler(Panel* super, int ch) {
}

NumberItem* numItem = (OptionItem_kind(selected) == OPTION_ITEM_NUMBER) ? (NumberItem*)selected : NULL;
StringItem* strItem = (OptionItem_kind(selected) == OPTION_ITEM_STRING) ? (StringItem*)selected : NULL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RTTI (run-time type information). This is a smell of bad object/class modeling. Unfortunately I don't have any idea to fix it for now.


/* Helper: position the hardware cursor right after the edit buffer.
* +1 on Y for the panel header row; +1 on X for the leading '[' bracket. */
Expand All @@ -54,8 +55,19 @@ static HandlerResult DisplayOptionsPanel_eventHandler(Panel* super, int ch) {
super->cursorOn = true; \
} while (0)

#define SET_STR_CURSOR() do { \
super->cursorY = super->y + 1 + (super->selected - super->scrollV); \
super->cursorX = super->x + 1 + (int)LineEditor_getCursor(&strItem->editor); \
super->cursorOn = true; \
} while (0)

switch (ch) {
case 27: /* Escape: cancel editing */
if (numItem && numItem->editing) {
NumberItem_cancelEditing(numItem);
super->cursorOn = false;
return HANDLED;
}
if (numItem && numItem->editing) {
NumberItem_cancelEditing(numItem);
super->cursorOn = false;
Expand All @@ -64,6 +76,11 @@ static HandlerResult DisplayOptionsPanel_eventHandler(Panel* super, int ch) {
break;
case KEY_BACKSPACE:
case KEY_DEL_MAC:
if (strItem && strItem->editing) {
LineEditor_handleKey(&strItem->editor, KEY_BACKSPACE);
SET_STR_CURSOR();
return HANDLED;
}
if (numItem) {
if (!numItem->editing) {
NumberItem_startEditingFromValue(numItem);
Expand All @@ -76,6 +93,19 @@ static HandlerResult DisplayOptionsPanel_eventHandler(Panel* super, int ch) {
case '\n':
case '\r':
case KEY_ENTER:
if (strItem && strItem->editing) {
StringItem_applyEditing(strItem);
super->cursorOn = false;
settingsChanged = true;
result = HANDLED;
break;
}
if (strItem && !strItem->editing) {
StringItem_startEditing(strItem);
SET_STR_CURSOR();
result = HANDLED;
break;
}
if (numItem && numItem->editing) {
if (NumberItem_applyEditing(numItem)) {
settingsChanged = true;
Expand Down Expand Up @@ -219,6 +249,12 @@ static HandlerResult DisplayOptionsPanel_eventHandler(Panel* super, int ch) {
SET_EDIT_CURSOR();
return HANDLED;
}
} else if (strItem) {
if (strItem->editing) {
LineEditor_handleKey(&strItem->editor, ch);
SET_STR_CURSOR();
return HANDLED;
}
}
break;
}
Expand Down Expand Up @@ -282,6 +318,7 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager*
Panel_add(super, (Object*) CheckItem_newByRef("Highlight program \"basename\"", &(settings->highlightBaseName)));
Panel_add(super, (Object*) CheckItem_newByRef("Highlight out-dated/removed programs (red) / libraries (yellow)", &(settings->highlightDeletedExe)));
Panel_add(super, (Object*) CheckItem_newByRef("Shadow distribution path prefixes", &(settings->shadowDistPathPrefix)));
Panel_add(super, (Object*) StringItem_newByRef("- Custom path prefixes (colon-separated)", &(settings->distPathPrefixes), NULL));
Panel_add(super, (Object*) CheckItem_newByRef("Merge exe, comm and cmdline in Command", &(settings->showMergedCommand)));
Panel_add(super, (Object*) CheckItem_newByRef("- Try to find comm in cmdline (when Command is merged)", &(settings->findCommInCmdline)));
Panel_add(super, (Object*) CheckItem_newByRef("- Try to strip exe from cmdline (when Command is merged)", &(settings->stripExeFromCmdline)));
Expand Down
67 changes: 67 additions & 0 deletions OptionItem.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ static void NumberItem_display(const Object* cast, RichString* out) {
RichString_appendWide(out, CRT_colors[CHECK_TEXT], this->super.text);
}

static void StringItem_display(const Object* cast, RichString* out) {
const StringItem* this = (const StringItem*)cast;
int labelAttr = CRT_colors[CHECK_TEXT];
int boxAttr = CRT_colors[CHECK_BOX];
int valAttr = this->valid ? CRT_colors[CHECK_MARK] : CRT_colors[FAILED_READ];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the validate function inside a StringItem do? And what would happen (or should happen) if the validation fails?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently its a placeholder. There was the idea of validating if the input path is a valid path in #822 but I did not implement any validation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what should be done if the "validation" fails?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input should be displayed in red as visual feedback.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "red" visual feedback would be not good enough if you have a list of path prefixes to edit. Also, I saw no code the "discard" directories that don't exist or are exact duplicates of previously specified paths.


RichString_writeAscii(out, boxAttr, "[");
if (this->editing) {
RichString_appendAscii(out, valAttr, this->editor.buffer);
} else {
const char* val = (this->ref && *this->ref) ? *this->ref : NULL;
if (val) {
RichString_appendAscii(out, valAttr, val);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use RichString_appendAscii for strings read from external sources. Use RichString_appendWide instead.

} else {
RichString_appendAscii(out, CRT_colors[PROCESS_SHADOW], "(empty)");
}
}
RichString_appendAscii(out, boxAttr, "] ");
RichString_appendWide(out, labelAttr, this->super.text);
}

const OptionItemClass OptionItem_class = {
.super = {
.extends = Class(Object),
Expand Down Expand Up @@ -112,6 +133,15 @@ const OptionItemClass NumberItem_class = {
.kind = OPTION_ITEM_NUMBER
};

const OptionItemClass StringItem_class = {
.super = {
.extends = Class(OptionItem),
.delete = OptionItem_delete,
.display = StringItem_display
},
.kind = OPTION_ITEM_STRING
};

TextItem* TextItem_new(const char* text) {
TextItem* this = AllocThis(TextItem);
this->super.text = xStrdup(text);
Expand Down Expand Up @@ -323,3 +353,40 @@ void NumberItem_deleteChar(NumberItem* this) {
this->editBuffer[this->editLen] = '\0';
}
}
StringItem* StringItem_newByRef(const char* text, char** ref, bool (*validate)(const char* text)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typedef bool (*ValidateStringFn)(const char* str, size_t len, void* context);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will consolidate into a typedef. Will use your suggested signature with len and context if the feature moves forward.

StringItem* this = AllocThis(StringItem);
this->super.text = xStrdup(text);
this->ref = ref;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does ref do here and how is this argument different from text?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

text is the label displayed next to the input field ( "- Custom path prefixes"). ref is a pointer to the settings field being edited (&settings->distPathPrefixes), so changes are written directly to the settings struct.

this->editing = false;
this->valid = true;
this->validate = validate;
LineEditor_init(&this->editor);
if (ref && *ref)
LineEditor_setText(&this->editor, *ref);
return this;
}

void StringItem_startEditing(StringItem* this) {
this->editing = true;
LineEditor_setText(&this->editor, (this->ref && *this->ref) ? *this->ref : "");
}

void StringItem_cancelEditing(StringItem* this) {
this->editing = false;
LineEditor_setText(&this->editor, (this->ref && *this->ref) ? *this->ref : "");
}

bool StringItem_applyEditing(StringItem* this) {
this->editing = false;
const char* text = this->editor.buffer;
if (this->validate && !this->validate(text)) {
this->valid = false;
return false;
}
this->valid = true;
if (this->ref) {
free(*this->ref);
*this->ref = (*text != '\0') ? xStrdup(text) : NULL;
}
return true;
}
17 changes: 17 additions & 0 deletions OptionItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ in the source distribution for its full text.

#include <stdbool.h>

#include "LineEditor.h"
#include "Object.h"

#define NUMBERITEM_EDIT_MAX 10
Expand All @@ -17,6 +18,7 @@ enum OptionItemType {
OPTION_ITEM_TEXT,
OPTION_ITEM_CHECK,
OPTION_ITEM_NUMBER,
OPTION_ITEM_STRING,
};

typedef struct OptionItemClass_ {
Expand Down Expand Up @@ -62,10 +64,20 @@ typedef struct NumberItem_ {
int savedValue;
} NumberItem;

typedef struct StringItem_ {
OptionItem super;
char** ref;
bool editing;
bool valid;
LineEditor editor;
bool (*validate)(const char* text);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. (Split the bool (*validate)(const char* text) function definition to its own typedef.)

} StringItem;
Comment on lines +67 to +74

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm skeptical of this item. It looks like a duplicate functionality when we have "Screens" whose names are editable.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify. Do you mean that StringItem is unnecessary as a new abstraction, and we should instead embed LineEditor directly in DisplayOptionsPanel like ScreensPanel does?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. I mean there's a significant overlap between the two string editing functionality. It's better to consolidate the two functionality into one code. How to do that is left for you to figure out.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhh. Forgot to mention this: LINEEDITOR_MAX is currently 128 (bytes). This is like not enough for editing a list of strings.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'll hold off on refactoring until there's consensus on whether the feature is wanted.


extern const OptionItemClass OptionItem_class;
extern const OptionItemClass TextItem_class;
extern const OptionItemClass CheckItem_class;
extern const OptionItemClass NumberItem_class;
extern const OptionItemClass StringItem_class;

TextItem* TextItem_new(const char* text);

Expand All @@ -88,4 +100,9 @@ bool NumberItem_applyEditing(NumberItem* this);
bool NumberItem_addChar(NumberItem* this, char c);
void NumberItem_deleteChar(NumberItem* this);

StringItem* StringItem_newByRef(const char* text, char** ref, bool (*validate)(const char* text));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split the bool (*validate)(const char* text) function definition to its own typedef, please?

void StringItem_startEditing(StringItem* this);
void StringItem_cancelEditing(StringItem* this);
bool StringItem_applyEditing(StringItem* this);

#endif
71 changes: 26 additions & 45 deletions Process.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,28 @@ static inline char* stpcpyWithNewlineConversion(char* dstStr, const char* srcStr
return dstStr;
}

static size_t findDistPrefixLength(const char* str, const Settings* settings) {
static const char* const builtinPrefixes =
"/bin/:/sbin/:/lib/:/lib32/:/lib64/:/libx32/:"
"/usr/bin/:/usr/sbin/:/usr/lib/:/usr/lib32/:/usr/lib64/:/usr/libx32/:"
"/usr/libexec/:/usr/local/bin/:/usr/local/lib/:/usr/local/sbin/:"
"/nix/store/:/run/current-system/";

const char* list = (settings->distPathPrefixes && settings->distPathPrefixes[0] != '\0')
? settings->distPathPrefixes
: builtinPrefixes;

const char* token = list;
while (*token) {
const char* end = String_strchrnul(token, ':');
size_t len = end - token;
if (strncmp(str, token, len) == 0)
return len;
token = *end ? end + 1 : end;
}
return 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's still a performance concern with this new function. The old code had a switch-case that could make the lookup slightly faster, but now we're forced to perform a linear search...

I wish a slightly faster data structure such as a trie.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this specific case implementing a trie might be overkill given the small number of prefixes. A full implementation would add significant complexity for what I'd expect to be a minimal performance gain.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont know. If it's me I'd implement it. Otherwise I don't like to trade a performance loss for a feature that few people use.

}

/*
* This function makes the merged Command string. It also stores the offsets of the
* basename, comm w.r.t the merged Command string - these offsets will be used by
Expand Down Expand Up @@ -256,51 +278,10 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {

#define CHECK_AND_MARK_DIST_PATH_PREFIXES(str_) \
do { \
if ((str_)[0] != '/') { \
break; \
} \
switch ((str_)[1]) { \
case 'b': \
CHECK_AND_MARK(str_, "/bin/"); \
break; \
case 'l': \
CHECK_AND_MARK(str_, "/lib/"); \
CHECK_AND_MARK(str_, "/lib32/"); \
CHECK_AND_MARK(str_, "/lib64/"); \
CHECK_AND_MARK(str_, "/libx32/"); \
break; \
case 's': \
CHECK_AND_MARK(str_, "/sbin/"); \
break; \
case 'u': \
if (String_startsWith(str_, "/usr/")) { \
switch ((str_)[5]) { \
case 'b': \
CHECK_AND_MARK(str_, "/usr/bin/"); \
break; \
case 'l': \
CHECK_AND_MARK(str_, "/usr/libexec/"); \
CHECK_AND_MARK(str_, "/usr/lib/"); \
CHECK_AND_MARK(str_, "/usr/lib32/"); \
CHECK_AND_MARK(str_, "/usr/lib64/"); \
CHECK_AND_MARK(str_, "/usr/libx32/"); \
\
CHECK_AND_MARK(str_, "/usr/local/bin/"); \
CHECK_AND_MARK(str_, "/usr/local/lib/"); \
CHECK_AND_MARK(str_, "/usr/local/sbin/"); \
break; \
case 's': \
CHECK_AND_MARK(str_, "/usr/sbin/"); \
break; \
} \
} \
break; \
case 'n': \
CHECK_AND_MARK(str_, "/nix/store/"); \
break; \
case 'r': \
CHECK_AND_MARK(str_, "/run/current-system/"); \
break; \
size_t plen = findDistPrefixLength(str_, settings); \
if (plen > 0) { \
WRITE_HIGHLIGHT(0, plen, CRT_colors[PROCESS_SHADOW], \
CMDLINE_HIGHLIGHT_FLAG_PREFIXDIR); \
} \
} while (0)

Expand Down
5 changes: 5 additions & 0 deletions Settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static void Settings_deleteScreens(Settings* this) {
void Settings_delete(Settings* this) {
free(this->filename);
free(this->initialFilename);
free(this->distPathPrefixes);
Settings_deleteColumns(this);
Settings_deleteScreens(this);
free(this);
Expand Down Expand Up @@ -440,6 +441,8 @@ static bool Settings_read(Settings* this, const char* fileName, const Machine* h
this->highlightDeletedExe = atoi(option[1]);
} else if (String_eq(option[0], "shadow_distribution_path_prefix")) {
this->shadowDistPathPrefix = atoi(option[1]);
} else if (String_eq(option[0], "dist_path_prefixes")) {
free_and_xStrdup(&this->distPathPrefixes, option[1]);
} else if (String_eq(option[0], "highlight_megabytes")) {
this->highlightMegabytes = atoi(option[1]);
} else if (String_eq(option[0], "highlight_threads")) {
Expand Down Expand Up @@ -696,6 +699,8 @@ int Settings_write(const Settings* this, bool onCrash) {
printSettingInteger("highlight_base_name", this->highlightBaseName);
printSettingInteger("highlight_deleted_exe", this->highlightDeletedExe);
printSettingInteger("shadow_distribution_path_prefix", this->shadowDistPathPrefix);
if (this->distPathPrefixes && this->distPathPrefixes[0] != '\0')
printSettingString("dist_path_prefixes", this->distPathPrefixes);
printSettingInteger("highlight_megabytes", this->highlightMegabytes);
printSettingInteger("highlight_threads", this->highlightThreads);
printSettingInteger("highlight_changes", this->highlightChanges);
Expand Down
1 change: 1 addition & 0 deletions Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ typedef struct ScreenSettings_ {
typedef struct Settings_ {
char* filename;
char* initialFilename;
char* distPathPrefixes;
bool writeConfig; /* whether to write the current settings on exit */
int config_version;
HeaderLayout hLayout;
Expand Down