Skip to content

Commit e1bf90f

Browse files
Add increment and decrement controls for numeric display options
Signed-off-by: Ahmed Abouzied <email@aabouzied.com>
1 parent e83c20a commit e1bf90f

2 files changed

Lines changed: 225 additions & 18 deletions

File tree

DisplayOptionsPanel.c

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ in the source distribution for its full text.
2020
#include "ProvideCurses.h"
2121

2222

23-
static const char* const DisplayOptionsFunctions[] = {"(De)Select ","Increment ", "Decrement ", "Done ", NULL};
24-
static const char* const DisplayOptionsKeys[] = {"Enter","+", "-", "F10", "Esc"};
25-
static const int DisplayOptionsEvents[] = {KEY_ENTER, '+', '-', KEY_F(10), 27};
23+
static const char* const BooleanDisplayOptionsFunctions[] = {"Select ", "Done ", NULL};
24+
static const char* const BooleanDisplayOptionsKeys[] = {"Enter", "F10", "Esc"};
25+
static const int BooleanDisplayOptionsEvents[] = {KEY_ENTER, KEY_F(10), 27};
26+
static const char* const NumericDisplayOptionsFunctions[] = {"Decrement ", "Increment ", "Done ", NULL};
27+
static const char* const NumericDisplayOptionsKeys[] = {"-", "+", "F10", "Esc"};
28+
static const int NumericDisplayOptionsEvents[] = {'-', '+', KEY_F(10), 27};
2629

2730
static void DisplayOptionsPanel_delete(Object* object) {
2831
Panel* super = (Panel*) object;
@@ -31,13 +34,40 @@ static void DisplayOptionsPanel_delete(Object* object) {
3134
free(this);
3235
}
3336

37+
static void DisplayOptionsPanel_setFunctionBar(Panel* super, OptionItem* item) {
38+
if (OptionItem_kind(item) == OPTION_ITEM_NUMBER) {
39+
FunctionBar* fuBar = FunctionBar_new(NumericDisplayOptionsFunctions, NumericDisplayOptionsKeys, NumericDisplayOptionsEvents);
40+
super->currentBar = fuBar;
41+
} else {
42+
FunctionBar* fuBar = FunctionBar_new(BooleanDisplayOptionsFunctions, BooleanDisplayOptionsKeys, BooleanDisplayOptionsEvents);
43+
super->currentBar = fuBar;
44+
}
45+
}
46+
3447
static HandlerResult DisplayOptionsPanel_eventHandler(Panel* super, int ch) {
3548
DisplayOptionsPanel* this = (DisplayOptionsPanel*) super;
3649

3750
HandlerResult result = IGNORED;
3851
OptionItem* selected = (OptionItem*) Panel_getSelected(super);
3952

4053
switch (ch) {
54+
55+
case KEY_UP: {
56+
int selected_index = Panel_getSelectedIndex(super);
57+
if (selected_index > 1) {
58+
OptionItem* next_to_select = (OptionItem*) Panel_get(super, selected_index - 1);
59+
DisplayOptionsPanel_setFunctionBar(super, next_to_select);
60+
}
61+
break;
62+
}
63+
case KEY_DOWN: {
64+
int selected_index = Panel_getSelectedIndex(super);
65+
if (selected_index < Panel_size(super) - 1) {
66+
OptionItem* next_to_select = (OptionItem*) Panel_get(super, selected_index + 1);
67+
DisplayOptionsPanel_setFunctionBar(super, next_to_select);
68+
}
69+
break;
70+
}
4171
case '\n':
4272
case '\r':
4373
case KEY_ENTER:
@@ -92,7 +122,7 @@ const PanelClass DisplayOptionsPanel_class = {
92122
DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager* scr) {
93123
DisplayOptionsPanel* this = AllocThis(DisplayOptionsPanel);
94124
Panel* super = (Panel*) this;
95-
FunctionBar* fuBar = FunctionBar_new(DisplayOptionsFunctions, DisplayOptionsKeys, DisplayOptionsEvents);
125+
FunctionBar* fuBar = FunctionBar_new(BooleanDisplayOptionsFunctions, BooleanDisplayOptionsKeys, BooleanDisplayOptionsEvents);
96126
Panel_init(super, 1, 1, 1, 1, Class(OptionItem), true, fuBar);
97127

98128
this->settings = settings;
@@ -121,27 +151,27 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager*
121151
Panel_add(super, (Object*) CheckItem_newByRef("Add guest time in CPU meter percentage", &(settings->accountGuestInCPUMeter)));
122152
Panel_add(super, (Object*) CheckItem_newByRef("Also show CPU percentage numerically", &(settings->showCPUUsage)));
123153
Panel_add(super, (Object*) CheckItem_newByRef("Also show CPU frequency", &(settings->showCPUFrequency)));
124-
#ifdef BUILD_WITH_CPU_TEMP
154+
#ifdef BUILD_WITH_CPU_TEMP
125155
Panel_add(super, (Object*) CheckItem_newByRef(
126-
#if defined(HTOP_LINUX)
127-
"Also show CPU temperature (requires libsensors)",
128-
#elif defined(HTOP_FREEBSD)
129-
"Also show CPU temperature",
130-
#else
131-
#error Unknown temperature implementation!
132-
#endif
133-
&(settings->showCPUTemperature)));
156+
#if defined(HTOP_LINUX)
157+
"Also show CPU temperature (requires libsensors)",
158+
#elif defined(HTOP_FREEBSD)
159+
"Also show CPU temperature",
160+
#else
161+
#error Unknown temperature implementation!
162+
#endif
163+
& (settings->showCPUTemperature)));
134164
Panel_add(super, (Object*) CheckItem_newByRef("- Show temperature in degree Fahrenheit instead of Celsius", &(settings->degreeFahrenheit)));
135-
#endif
136-
#ifdef HAVE_GETMOUSE
165+
#endif
166+
#ifdef HAVE_GETMOUSE
137167
Panel_add(super, (Object*) CheckItem_newByRef("Enable the mouse", &(settings->enableMouse)));
138-
#endif
168+
#endif
139169
Panel_add(super, (Object*) NumberItem_newByRef("Update interval (in seconds)", &(settings->delay), -1, 1, 255));
140170
Panel_add(super, (Object*) CheckItem_newByRef("Highlight new and old processes", &(settings->highlightChanges)));
141171
Panel_add(super, (Object*) NumberItem_newByRef("- Highlight time (in seconds)", &(settings->highlightDelaySecs), 0, 1, 24 * 60 * 60));
142172
Panel_add(super, (Object*) NumberItem_newByRef("Hide main function bar (0 - off, 1 - on ESC until next input, 2 - permanently)", &(settings->hideFunctionBar), 0, 0, 2));
143-
#ifdef HAVE_LIBHWLOC
173+
#ifdef HAVE_LIBHWLOC
144174
Panel_add(super, (Object*) CheckItem_newByRef("Show topology when selecting affinity by default", &(settings->topologyAffinity)));
145-
#endif
175+
#endif
146176
return this;
147177
}

DisplayOptionsPanel.c.orig

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
htop - DisplayOptionsPanel.c
3+
(C) 2004-2011 Hisham H. Muhammad
4+
Released under the GNU GPLv2, see the COPYING file
5+
in the source distribution for its full text.
6+
*/
7+
8+
#include "config.h" // IWYU pragma: keep
9+
10+
#include "DisplayOptionsPanel.h"
11+
12+
#include <stdbool.h>
13+
#include <stdlib.h>
14+
15+
#include "CRT.h"
16+
#include "FunctionBar.h"
17+
#include "Header.h"
18+
#include "Object.h"
19+
#include "OptionItem.h"
20+
#include "ProvideCurses.h"
21+
22+
23+
static const char* const BooleanDisplayOptionsFunctions[] = {"Select ", "Done ", NULL};
24+
static const char* const BooleanDisplayOptionsKeys[] = {"Enter", "F10", "Esc"};
25+
static const int BooleanDisplayOptionsEvents[] = {KEY_ENTER, KEY_F(10), 27};
26+
static const char* const NumericDisplayOptionsFunctions[] = {"Decrement ", "Increment ", "Done ", NULL};
27+
static const char* const NumericDisplayOptionsKeys[] = {"-", "+", "F10", "Esc"};
28+
static const int NumericDisplayOptionsEvents[] = {'-', '+', KEY_F(10), 27};
29+
30+
static void DisplayOptionsPanel_delete(Object* object) {
31+
Panel* super = (Panel*) object;
32+
DisplayOptionsPanel* this = (DisplayOptionsPanel*) object;
33+
Panel_done(super);
34+
free(this);
35+
}
36+
37+
static void DisplayOptionsPanel_setFunctionBar(Panel *super, OptionItem* item){
38+
if (OptionItem_kind(item) == OPTION_ITEM_NUMBER) {
39+
FunctionBar* fuBar = FunctionBar_new(NumericDisplayOptionsFunctions, NumericDisplayOptionsKeys, NumericDisplayOptionsEvents);
40+
super->currentBar = fuBar;
41+
} else {
42+
FunctionBar* fuBar = FunctionBar_new(BooleanDisplayOptionsFunctions, BooleanDisplayOptionsKeys, BooleanDisplayOptionsEvents);
43+
super->currentBar = fuBar;
44+
}
45+
}
46+
47+
static HandlerResult DisplayOptionsPanel_eventHandler(Panel* super, int ch) {
48+
DisplayOptionsPanel* this = (DisplayOptionsPanel*) super;
49+
50+
HandlerResult result = IGNORED;
51+
OptionItem* selected = (OptionItem*) Panel_getSelected(super);
52+
53+
switch (ch) {
54+
55+
case KEY_UP: {
56+
int selected_index = Panel_getSelectedIndex(super);
57+
if (selected_index > 1) {
58+
OptionItem* next_to_select = (OptionItem*) Panel_get(super, selected_index -1);
59+
DisplayOptionsPanel_setFunctionBar(super, next_to_select);
60+
}
61+
break;
62+
}
63+
case KEY_DOWN: {
64+
int selected_index = Panel_getSelectedIndex(super);
65+
if (selected_index < Panel_size(super) - 1) {
66+
OptionItem* next_to_select = (OptionItem*) Panel_get(super, selected_index +1);
67+
DisplayOptionsPanel_setFunctionBar(super, next_to_select);
68+
}
69+
break;
70+
}
71+
case '\n':
72+
case '\r':
73+
case KEY_ENTER:
74+
case KEY_MOUSE:
75+
case KEY_RECLICK:
76+
case ' ':
77+
switch (OptionItem_kind(selected)) {
78+
case OPTION_ITEM_CHECK:
79+
CheckItem_toggle((CheckItem*)selected);
80+
result = HANDLED;
81+
break;
82+
case OPTION_ITEM_NUMBER:
83+
NumberItem_toggle((NumberItem*)selected);
84+
result = HANDLED;
85+
break;
86+
}
87+
break;
88+
case '-':
89+
if (OptionItem_kind(selected) == OPTION_ITEM_NUMBER) {
90+
NumberItem_decrease((NumberItem*)selected);
91+
result = HANDLED;
92+
}
93+
break;
94+
case '+':
95+
if (OptionItem_kind(selected) == OPTION_ITEM_NUMBER) {
96+
NumberItem_increase((NumberItem*)selected);
97+
result = HANDLED;
98+
}
99+
break;
100+
}
101+
102+
if (result == HANDLED) {
103+
this->settings->changed = true;
104+
Header* header = this->scr->header;
105+
Header_calculateHeight(header);
106+
Header_reinit(header);
107+
Header_updateData(header);
108+
Header_draw(header);
109+
ScreenManager_resize(this->scr, this->scr->x1, header->height, this->scr->x2, this->scr->y2);
110+
}
111+
return result;
112+
}
113+
114+
const PanelClass DisplayOptionsPanel_class = {
115+
.super = {
116+
.extends = Class(Panel),
117+
.delete = DisplayOptionsPanel_delete
118+
},
119+
.eventHandler = DisplayOptionsPanel_eventHandler
120+
};
121+
122+
DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager* scr) {
123+
DisplayOptionsPanel* this = AllocThis(DisplayOptionsPanel);
124+
Panel* super = (Panel*) this;
125+
FunctionBar* fuBar = FunctionBar_new(BooleanDisplayOptionsFunctions, BooleanDisplayOptionsKeys, BooleanDisplayOptionsEvents);
126+
Panel_init(super, 1, 1, 1, 1, Class(OptionItem), true, fuBar);
127+
128+
this->settings = settings;
129+
this->scr = scr;
130+
131+
Panel_setHeader(super, "Display options");
132+
Panel_add(super, (Object*) CheckItem_newByRef("Tree view", &(settings->treeView)));
133+
Panel_add(super, (Object*) CheckItem_newByRef("- Tree view is always sorted by PID (htop 2 behavior)", &(settings->treeViewAlwaysByPID)));
134+
Panel_add(super, (Object*) CheckItem_newByRef("- Tree view is collapsed by default", &(settings->allBranchesCollapsed)));
135+
Panel_add(super, (Object*) CheckItem_newByRef("Shadow other users' processes", &(settings->shadowOtherUsers)));
136+
Panel_add(super, (Object*) CheckItem_newByRef("Hide kernel threads", &(settings->hideKernelThreads)));
137+
Panel_add(super, (Object*) CheckItem_newByRef("Hide userland process threads", &(settings->hideUserlandThreads)));
138+
Panel_add(super, (Object*) CheckItem_newByRef("Display threads in a different color", &(settings->highlightThreads)));
139+
Panel_add(super, (Object*) CheckItem_newByRef("Show custom thread names", &(settings->showThreadNames)));
140+
Panel_add(super, (Object*) CheckItem_newByRef("Show program path", &(settings->showProgramPath)));
141+
Panel_add(super, (Object*) CheckItem_newByRef("Highlight program \"basename\"", &(settings->highlightBaseName)));
142+
Panel_add(super, (Object*) CheckItem_newByRef("Highlight out-dated/removed programs", &(settings->highlightDeletedExe)));
143+
Panel_add(super, (Object*) CheckItem_newByRef("Merge exe, comm and cmdline in Command", &(settings->showMergedCommand)));
144+
Panel_add(super, (Object*) CheckItem_newByRef("- Try to find comm in cmdline (when Command is merged)", &(settings->findCommInCmdline)));
145+
Panel_add(super, (Object*) CheckItem_newByRef("- Try to strip exe from cmdline (when Command is merged)", &(settings->stripExeFromCmdline)));
146+
Panel_add(super, (Object*) CheckItem_newByRef("Highlight large numbers in memory counters", &(settings->highlightMegabytes)));
147+
Panel_add(super, (Object*) CheckItem_newByRef("Leave a margin around header", &(settings->headerMargin)));
148+
Panel_add(super, (Object*) CheckItem_newByRef("Detailed CPU time (System/IO-Wait/Hard-IRQ/Soft-IRQ/Steal/Guest)", &(settings->detailedCPUTime)));
149+
Panel_add(super, (Object*) CheckItem_newByRef("Count CPUs from 1 instead of 0", &(settings->countCPUsFromOne)));
150+
Panel_add(super, (Object*) CheckItem_newByRef("Update process names on every refresh", &(settings->updateProcessNames)));
151+
Panel_add(super, (Object*) CheckItem_newByRef("Add guest time in CPU meter percentage", &(settings->accountGuestInCPUMeter)));
152+
Panel_add(super, (Object*) CheckItem_newByRef("Also show CPU percentage numerically", &(settings->showCPUUsage)));
153+
Panel_add(super, (Object*) CheckItem_newByRef("Also show CPU frequency", &(settings->showCPUFrequency)));
154+
#ifdef BUILD_WITH_CPU_TEMP
155+
Panel_add(super, (Object*) CheckItem_newByRef(
156+
#if defined(HTOP_LINUX)
157+
"Also show CPU temperature (requires libsensors)",
158+
#elif defined(HTOP_FREEBSD)
159+
"Also show CPU temperature",
160+
#else
161+
#error Unknown temperature implementation!
162+
#endif
163+
&(settings->showCPUTemperature)));
164+
Panel_add(super, (Object*) CheckItem_newByRef("- Show temperature in degree Fahrenheit instead of Celsius", &(settings->degreeFahrenheit)));
165+
#endif
166+
#ifdef HAVE_GETMOUSE
167+
Panel_add(super, (Object*) CheckItem_newByRef("Enable the mouse", &(settings->enableMouse)));
168+
#endif
169+
Panel_add(super, (Object*) NumberItem_newByRef("Update interval (in seconds)", &(settings->delay), -1, 1, 255));
170+
Panel_add(super, (Object*) CheckItem_newByRef("Highlight new and old processes", &(settings->highlightChanges)));
171+
Panel_add(super, (Object*) NumberItem_newByRef("- Highlight time (in seconds)", &(settings->highlightDelaySecs), 0, 1, 24 * 60 * 60));
172+
Panel_add(super, (Object*) NumberItem_newByRef("Hide main function bar (0 - off, 1 - on ESC until next input, 2 - permanently)", &(settings->hideFunctionBar), 0, 0, 2));
173+
#ifdef HAVE_LIBHWLOC
174+
Panel_add(super, (Object*) CheckItem_newByRef("Show topology when selecting affinity by default", &(settings->topologyAffinity)));
175+
#endif
176+
return this;
177+
}

0 commit comments

Comments
 (0)