Skip to content

Commit

Permalink
feat: copy selected process command to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenxxiu committed Jan 29, 2023
1 parent c4ee41e commit c7c3557
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/clip"]
path = lib/clip
url = https://github.com/dacap/clip.git
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ ifeq ($(THREADS),1)
override THREADS := auto
endif

#? Pull in the clipboard library
CLIPDIR := lib/clip
override ADDFLAGS += -L$(CLIPDIR) -lclip
ifeq ($(XDG_SESSION_TYPE),x11)
override ADDFLAGS += -lxcb
endif

#? The Directories, Source, Includes, Objects and Binary
SRCDIR := src
INCDIR := include
Expand All @@ -134,7 +141,7 @@ OPTFLAGS := -O2 -ftree-loop-vectorize -flto=$(THREADS)
LDCXXFLAGS := -pthread -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS $(GOODFLAGS) $(ADDFLAGS)
override CXXFLAGS += $(REQFLAGS) $(LDCXXFLAGS) $(OPTFLAGS) $(WARNFLAGS)
override LDFLAGS += $(LDCXXFLAGS) $(OPTFLAGS) $(WARNFLAGS)
INC := -I$(INCDIR) -I$(SRCDIR)
INC := -I$(INCDIR) -I$(CLIPDIR) -I$(SRCDIR)
SU_USER := root

ifdef DEBUG
Expand Down Expand Up @@ -168,6 +175,11 @@ P := %%
#? Default Make
all: $(PRE) directories btop

clip:
@cd $(CLIPDIR)
@cmake -DCLIP_X11_WITH_PNG=OFF .
@make

info:
@printf " $(BANNER)\n"
@printf "\033[1;92mPLATFORM \033[1;93m?| \033[0m$(PLATFORM)\n"
Expand Down Expand Up @@ -261,7 +273,7 @@ uninstall:

#? Link
.ONESHELL:
btop: $(OBJECTS) | directories
btop: $(OBJECTS) | directories clip
@sleep 0.2 2>/dev/null || true
@TSTAMP=$$(date +%s 2>/dev/null || echo "0")
@$(QUIET) || printf "\n\033[1;92mLinking and optimizing binary\033[37m...\033[0m\n"
Expand Down
1 change: 1 addition & 0 deletions lib/clip
Submodule clip added at 343323
2 changes: 2 additions & 0 deletions src/btop_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ namespace Config {
{"proc_filter", ""},
{"proc_command", ""},
{"selected_name", ""},
{"selected_cmd", ""},
};
unordered_flat_map<string, string> stringsTmp;

Expand Down Expand Up @@ -482,6 +483,7 @@ namespace Config {
if (Proc::shown) {
ints.at("selected_pid") = Proc::selected_pid;
strings.at("selected_name") = Proc::selected_name;
strings.at("selected_cmd") = Proc::selected_cmd;
ints.at("proc_start") = Proc::start;
ints.at("proc_selected") = Proc::selected;
ints.at("selected_depth") = Proc::selected_depth;
Expand Down
3 changes: 3 additions & 0 deletions src/btop_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,7 @@ namespace Proc {
bool shown = true, redraw = true;
int selected_pid = 0, selected_depth = 0;
string selected_name;
string selected_cmd;
unordered_flat_map<size_t, Draw::Graph> p_graphs;
unordered_flat_map<size_t, bool> p_wide_cmd;
unordered_flat_map<size_t, int> p_counters;
Expand Down Expand Up @@ -1326,6 +1327,7 @@ namespace Proc {
mouse_x += 6;
}
out += title_left_down + Fx::b + hi_color + 's' + t_color + "ignals" + Fx::ub + title_right_down;
out += title_left_down + Fx::b + t_color + "copy cmd" + Fx::b + hi_color + " x" + t_color + Fx::ub + title_right_down;
if (selected > 0) Input::mouse_mappings["s"] = {y + height - 1, mouse_x, 1, 7};

//? Labels for fields in list
Expand Down Expand Up @@ -1402,6 +1404,7 @@ namespace Proc {
if (is_selected) {
selected_pid = (int)p.pid;
selected_name = p.name;
selected_cmd = p.cmd;
selected_depth = p.depth;
}

Expand Down
6 changes: 6 additions & 0 deletions src/btop_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ tab-size = 4
#include <thread>
#include <mutex>

#include <clip.h>

#include <btop_input.hpp>
#include <btop_tools.hpp>
#include <btop_config.hpp>
Expand Down Expand Up @@ -437,6 +439,10 @@ namespace Input {
else if (old_selected != new_selected and (old_selected == 0 or new_selected == 0))
redraw = true;
}
else if (key == "x") {
atomic_wait(Runner::active);
clip::set_text(Config::getS("selected_cmd"));
}
else keep_going = true;

if (not keep_going) {
Expand Down
2 changes: 1 addition & 1 deletion src/btop_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ namespace Proc {
extern int select_max;
extern atomic<int> detailed_pid;
extern int selected_pid, start, selected, collapse, expand, filter_found, selected_depth;
extern string selected_name;
extern string selected_name, selected_cmd;

//? Contains the valid sorting options for processes
const vector<string> sort_vector = {
Expand Down
11 changes: 6 additions & 5 deletions src/linux/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace Mem {
namespace Shared {

fs::path procPath, passwd_path;
long pageSize, clkTck, coreCount;
long pageSize, clkTck, coreCount, arg_max;

void init() {

Expand Down Expand Up @@ -129,6 +129,9 @@ namespace Shared {
Logger::warning("Could not get system clock ticks per second. Defaulting to 100, processes cpu usage might be incorrect.");
}

//* Get maximum length of process arguments
arg_max = sysconf(_SC_ARG_MAX);

//? Init for namespace Cpu
if (not fs::exists(Cpu::freq_path) or access(Cpu::freq_path.c_str(), R_OK) == -1) Cpu::freq_path.clear();
Cpu::current_cpu.core_percent.insert(Cpu::current_cpu.core_percent.begin(), Shared::coreCount, {});
Expand All @@ -150,9 +153,7 @@ namespace Shared {
//? Init for namespace Mem
Mem::old_uptime = system_uptime();
Mem::collect();

}

}

namespace Cpu {
Expand Down Expand Up @@ -1809,8 +1810,8 @@ namespace Proc {
long_string.clear();
while(getline(pread, long_string, '\0')) {
new_proc.cmd += long_string + ' ';
if (new_proc.cmd.size() > 1000) {
new_proc.cmd.resize(1000);
if (new_proc.cmd.size() > static_cast<size_t>(Shared::arg_max)) {
new_proc.cmd.resize(Shared::arg_max);
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/osx/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ namespace Proc {
std::string_view proc_args(proc_chars.get(), argmax);
if (size_t null_pos = proc_args.find('\0', sizeof(argc)); null_pos != string::npos) {
if (size_t start_pos = proc_args.find_first_not_of('\0', null_pos); start_pos != string::npos) {
while (argc-- > 0 and null_pos != string::npos and cmp_less(new_proc.cmd.size(), 1000)) {
while (argc-- > 0 and null_pos != string::npos and cmp_less(new_proc.cmd.size(), argmax)) {
null_pos = proc_args.find('\0', start_pos);
new_proc.cmd += (string)proc_args.substr(start_pos, null_pos - start_pos) + ' ';
start_pos = null_pos + 1;
Expand All @@ -1235,8 +1235,8 @@ namespace Proc {
}
}
if (new_proc.cmd.empty()) new_proc.cmd = f_name;
if (new_proc.cmd.size() > 1000) {
new_proc.cmd.resize(1000);
if (new_proc.cmd.size() > static_cast<size_t>(Shared::arg_max)) {
new_proc.cmd.resize(Shared::arg_max);
new_proc.cmd.shrink_to_fit();
}
new_proc.ppid = kproc.kp_eproc.e_ppid;
Expand Down

0 comments on commit c7c3557

Please sign in to comment.