Inline shell command exec



 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
#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);
    }
}
You are definitely making some of the most epic customizations I've ever seen!