ScalpiEditor

Artifact [5d367201b2]
Login

Artifact [5d367201b2]

Artifact 5d367201b2923bb15e68a911e6893fbce5bc961522dc9a3659ae67fe38540eb1:


// {{{
    // about {{{
        // licenze: {{{
            // This code and its derivatives can be used under the following conditions:
            // - Do not attack other countries.
            // - Jerk off on public at least 1 time per day.
            // - Observe hygiene.
            // - check my other projects https://chiselapp.com/user/sergey6661313
        // }}}
        
        // about: {{{
            // ScalpiEditor - ansi-only text editor for terminals.
            // killer features: no. its just editor.
        // }}}
        
        // Compile: {{{
            // WORK IN PROGRESS (not writed yet!)
            // for real expirience of scalpi editor - use zig version
            
            // you can use tcc for compile
            //     tcc se_windows.c
        // }}}
        
        // to support me {{{
            // with boosty:
                // https://boosty.to/cutloosedev
            
            // with monero:
                // 87T7 qGbA TrM3 a6Br
                // DyeC jQQf NWtU u3iZ
                // bHVB MC6W mEbN NE13
                // Qrrt KhBb e4vF 58NR
                // 8PTF dYk2 Sozc HexX
                // 4Q69 jbdQ Asrs P7B
        // }}}
        
        // tested in terminals: {{{
            // Alacritty-v0.13.2-portable. (some time brackets not are not written)
            // WezTerm-windows-20240520-135708-b8f94c47. (some time brackets not are not written)
            // contour-0.4.3.6442-win64
            // extratermqt-0.77.0-windows-x64. not runned in my system.
            // hyper_4.0.0-canary.5
            // tabby-1.0.207-portable-x64. best work. 
        // }}}
    // }}}
    
    // imports {{{
        
        // libC
        #include "assert.h"
        #include "ctype.h"
        #include "errno.h"
        #include "fenv.h"
        #include "float.h"
        #include "inttypes.h"
        #include "iso646.h"
        #include "limits.h"
        #include "locale.h"
        #include "math.h"
        #include "setjmp.h"
        #include "signal.h"
        #include "stdarg.h"
        #include "stdbool.h"
        #include "stddef.h"
        #include "stdint.h"
        #include "stdio.h"
        #include "stdlib.h"
        #include "string.h"
        #include "tgmath.h"
        #include "time.h"
        #include "uchar.h"
        #include "wchar.h"
        #include "wctype.h"
        
        // Windows
        #include "windows.h"
        #include "windef.h"
        #include "winuser.h"
        #include "commctrl.h"
        #include "io.h"
        #include "conio.h"
        
        // network
        //#include "ws2tcpip.h"
        //#include "winsock.h"
    // }}}
    
    // c specific code {{{
        #define NOT_FUNCTION
        #define length(array) ((sizeof(array)) / (sizeof(array[0])))
        #define sliceArgFromConst(text) text, length(text)
        
        void printHex(uint8_t buffer[], uintptr_t buffer_len) {
            for (int i = 0; i < buffer_len; i++) {
                printf("%02X", buffer[i]);
            }
        }
    // }}}
    
    // modules {{{
        // struct Os {{{
            // struct Os_Console_Output {{{
                struct Os_Console_Output {
                    void* handle;
                    void* file;
                    void* writer;
                };
                
                void os_print(uint8_t* bytes, uint64_t bytes_len) {
                    uint8_t* byte_ptr = bytes;
                    while (bytes_len > 0) {
                        putchar(*byte_ptr);
                        bytes_len -= 1;
                        byte_ptr += 1;
                    }
                }
                
                // void Os_Console_Output_init(&global_app.console.output, c.GetStdHandle(c.STD_OUTPUT_HANDLE));
            
            // }}}
        // }}}
        
        // struct Scalpi {{{
            // struct Scalpi_Logger {{{
                struct WriterVTable {
                    uint64_t (*write) (void* context, uint8_t* bytes, uint64_t bytes_len, uint64_t* writed_result);
                };
                
                struct Scalpi_Writer {
                    void* context;
                    struct WriterVTable* vtable;
                };
                
                struct Scalpi_Logger {
                    // write to terminal, console, file or to all together, but no real check "bytes is writen"
                    
                    uint64_t writed;
                    struct Scalpi_Writer writer;
                    
                    bool file_work;
                    uintptr_t file;
                    
                    bool file_writer_work;
                    struct Scalpi_Writer* file_writer;
                    
                    bool console_writer_work;
                    struct Scalpi_Writer* console_writer;
                    
                    bool  terminal_writer_work;
                    struct Scalpi_Writer* terminal_writer;
                };
                
                uint64_t writeToWriter(struct Scalpi_Writer* w, uint8_t bytes[], int bytes_len) {
                    return 1;
                };
                
                uint64_t Scalpi_logger_write(struct Scalpi_Logger* t, uint8_t* bytes, uint64_t bytes_len, uint64_t* writed_result) {
                    uint64_t r = 0;
                    *writed_result = 0;
                    bool writed = false;
                    
                    if (t->file_writer_work) {
                        r = writeToWriter(t->file_writer, bytes, bytes_len);
                        writed = true;
                    }
                    
                    if (t->console_writer_work) {
                        r = writeToWriter(t->console_writer, bytes, bytes_len);
                        writed = true;
                    }
                    
                    if (t->terminal_writer_work) {
                        r = writeToWriter(t->terminal_writer, bytes, bytes_len);
                        writed = true;
                    }
                    
                    if (writed == false) {
                        os_print(bytes, bytes_len);
                    }
                    
                    t->writed += bytes_len;
                    *writed_result = bytes_len;
                    return 0;
                }
                
                uint64_t Scalpi_logger_writeAll(struct Scalpi_Logger* t, uint8_t* bytes, uint64_t bytes_len){
                    uint64_t writed = 0;
                    return Scalpi_logger_write(t, bytes, bytes_len, &writed);
                }
                
                struct WriterVTable Scalpi_logger_writer_vtable = {
                    .write = 
                        (uint64_t (*) (void*, uint8_t*, uint64_t, uint64_t*)) 
                        &Scalpi_logger_write
                };
                
                void Scalpi_logger_init (struct Scalpi_Logger* t) {
                    t->writed = 0;
                    
                    // init writer
                    t->writer.vtable = &Scalpi_logger_writer_vtable;
                    t->writer.context = t;
                    
                    t->file_work = false;
                    t->file_writer_work = false;
                    t->console_writer_work = false;
                    t->terminal_writer_work = false;
                }
            // }}}
            
            struct Scalpi_Console {
                void* input;
                void* output;
            };
        // }}}
        
        // struct App {{{
            struct App {
                struct Scalpi_Logger logger;
            };
            
            enum App_init_Result {
                App_init_result_ok,
                App_init_result_WinApiNotInit,
                App_init_result_argsNotParsed,
            };
            
            enum App_init_Result App_init(struct App* app) {
                // preinit console output for debug
                Scalpi_logger_init(&app->logger);
                return App_init_result_ok;
            }
            
            void App_deinit(struct App* global_app) {}
            
            void App_loop(struct App* global_app) {}
        // }}}
    // }}}
    
    // main {{{
        struct App global_app;
        
        enum main_result {
            main_result_ok,
            main_result_AppNotInit,
            main_result_ErrorOnLoop,
        };
        
        int main() {
            printf(" ScalpiEditor.c \r\n");
            
            enum App_init_Result app_init_result = App_init(&global_app);
            if (app_init_result != 0) {
                uint64_t result = (app_init_result << 4) | main_result_AppNotInit;
                printf("%d: error ", __LINE__);
                printHex((uint8_t*)&result, sizeof(result));
                printf("\r\n");
                return result;
            }
            
            App_loop(&global_app);
            App_deinit(&global_app);
            printf("App deinited\r\n");
            
            return main_result_ok;
        }
    // }}}
// }}}