#define STB_SPRINTF_IMPLEMENTATION
#include "../stb/stb_sprintf.h"
#define cro_make_string(_name, _size) \
char _ ## _name ## _space[_size] = ""; \
String _name = {.str = _ ## _name ## _space, .size = 0, .memory_size = _size};
#define cro_stringf(_string, _fmt, ...) \
_string.size = stbsp_snprintf(_string.str, _string.memory_size, _fmt, __VA_ARGS__)
CUSTOM_COMMAND_SIG(cro_shell_exec) {
if (cro_selection_active) {
View_Summary view = get_active_view(app, AccessAll);
Buffer_Summary buffer = get_buffer(app, view.buffer_id, AccessAll);
cro_make_string(dir, 1024);
if(buffer.file_name) {
cro_stringf(dir, "%s", buffer.file_name);
} else {
char _buf[1024];
get_4ed_path(app, _buf, sizeof(_buf));
_buf[sizeof(_buf)-1] = 0;
cro_stringf(dir, "%s", _buf);
}
remove_last_folder(&dir);
Range range = get_range(&view);
char command_space[4096];
if(range.max-range.min>sizeof(command_space)) {
message_printf(app, "Failed to run command, command_space too small\n");
return;
}
buffer_read_range(app, &buffer, range.min, range.max, command_space);
get_view_next_looped(app, &view, AccessAll);
exec_system_command(app, &view,
buffer_identifier(literal("*custom shell command*")),
dir.str, dir.size,
command_space, range.max-range.min,
CLI_OverlapWithConflict);
} else {
Query_Bar bar;
bar.prompt = make_lit_string("Command: ");
char space[1024];
bar.string = make_fixed_width_string(space);
if (!query_user_string(app, &bar)) return;
end_query_bar(app, &bar, 0);
cro_make_string(dir, 512);
View_Summary view = get_active_view(app, AccessAll);
Buffer_Summary buffer = get_buffer(app, view.buffer_id, AccessAll);
int32_t build_dir_type = get_build_directory(app, &buffer, &dir);
exec_system_command(app, &view,
buffer_identifier(literal("*custom shell command*")),
dir.str, dir.size,
bar.string.str, bar.string.size,
CLI_OverlapWithConflict);
}
}