-
-
Notifications
You must be signed in to change notification settings - Fork 603
Expand file tree
/
Copy pathCommandScreen.c
More file actions
139 lines (112 loc) · 3.92 KB
/
Copy pathCommandScreen.c
File metadata and controls
139 lines (112 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
htop - CommandScreen.c
(C) 2017,2020 ryenus
(C) 2020,2021 htop dev team
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/
#include "config.h" // IWYU pragma: keep
#include "CommandScreen.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "CRT.h"
#include "FunctionBar.h"
#include "Macros.h"
#include "Panel.h"
#include "ProvideCurses.h"
static void CommandScreen_scan(InfoScreen* this) {
Panel* panel = this->display;
int idx = MAXIMUM(Panel_getSelectedIndex(panel), 0);
Panel_prune(panel);
const char* p = Process_getCommand(this->process);
size_t line_maxlen = COLS < 40 ? 40 : COLS;
size_t line_offset = 0;
size_t last_space = 0;
char* line = xCalloc(line_maxlen + 1, sizeof(char));
for (; *p != '\0'; p++) {
if (line_offset >= line_maxlen) {
assert(line_offset <= line_maxlen);
assert(last_space <= line_maxlen);
size_t line_len = last_space <= 0 ? line_offset : last_space;
char tmp = line[line_len];
line[line_len] = '\0';
InfoScreen_addLine(this, line);
line[line_len] = tmp;
assert(line_len <= line_offset);
line_offset -= line_len;
memmove(line, line + line_len, line_offset);
last_space = 0;
}
line[line_offset++] = *p;
if (*p == ' ') {
last_space = line_offset;
}
}
if (line_offset > 0) {
line[line_offset] = '\0';
InfoScreen_addLine(this, line);
}
free(line);
Panel_setSelected(panel, idx);
}
static void CommandScreen_draw(InfoScreen* this) {
InfoScreen_drawTitled(this, "Command of process %d - %s", Process_getPid(this->process), Process_getCommand(this->process));
}
static void CommandScreen_copyCommand(const Process* process) {
const char* command = Process_getCommand(process);
if (!command) return;
char copyCmd[1024];
bool success = false;
#ifdef __APPLE__
snprintf(copyCmd, sizeof(copyCmd), "printf '%%s' '%s' | pbcopy", command);
success = (system(copyCmd) == 0);
#elif defined(__linux__) || defined(__unix__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
snprintf(copyCmd, sizeof(copyCmd), "printf '%%s' '%s' | xclip -selection clipboard 2>/dev/null", command);
if (system(copyCmd) != 0) {
snprintf(copyCmd, sizeof(copyCmd), "printf '%%s' '%s' | xsel --clipboard 2>/dev/null", command);
success = (system(copyCmd) == 0);
} else {
success = true;
}
#endif
// Show feedback message
attrset(CRT_colors[FUNCTION_BAR]);
mvhline(LINES - 1, 0, ' ', COLS);
if (success) {
mvaddstr(LINES - 1, 0, "Command copied to clipboard");
} else {
mvaddstr(LINES - 1, 0, "Copy failed - clipboard not available");
}
attrset(CRT_colors[DEFAULT_COLOR]);
refresh();
}
static bool CommandScreen_onKey(InfoScreen* super, int ch) {
switch (ch) {
case KEY_F(7):
CommandScreen_copyCommand(super->process);
return true;
default:
return false;
}
}
const InfoScreenClass CommandScreen_class = {
.super = {
.extends = Class(Object),
.delete = CommandScreen_delete
},
.scan = CommandScreen_scan,
.draw = CommandScreen_draw,
.onKey = CommandScreen_onKey
};
CommandScreen* CommandScreen_new(Process* process) {
CommandScreen* this = AllocThis(CommandScreen);
static const char* const CommandScreenFunctions[] = {"Search ", "Filter ", "Refresh", "Copy ", "Done ", NULL};
static const char* const CommandScreenKeys[] = {"F3", "F4", "F5", "F7", "Esc"};
static const int CommandScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(7), 27};
FunctionBar* bar = FunctionBar_new(CommandScreenFunctions, CommandScreenKeys, CommandScreenEvents);
return (CommandScreen*) InfoScreen_init(&this->super, process, bar, LINES - 2, " ");
}
void CommandScreen_delete(Object* this) {
free(InfoScreen_done((InfoScreen*)this));
}