ScalpiEditor

Artifact [e48709dd4c]
Login

Artifact [e48709dd4c]

Artifact e48709dd4c2feef622c01b9ebe9db64cd1c255713458061d3ffdec16e7a0b91a:


// {{{
    // 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.
        // }}}
        // WORK IN PROGRESS (not writed yet!)
        // for real expirience of scalpi editor - use zig version
        // Compile: {{{
       	//     zig cc 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)
        
        // Iterator {{{
            struct Iterator {
                int pos;
                int step;
                int start;
                int end;
                uint8_t stopped;
                uint8_t runned;
            };
            
            void Iterator_init(struct Iterator* this, int start, int end, int step) {
                this->stopped = 0;
                this->runned = 0;
                
                this->start = start;
                this->end = end;
                this->step = step;
            }
            
            void Iterator_next(struct Iterator* this) {
                if (this->stopped ) return;
                
                if (this->runned == 0) {
                    this->runned = 1;
                    return;
                } 
                
                if (this->end - this->start < this->step) {
                    this->stopped = true;
                    this->pos = this->end;
                    return;
                }
                
                this->pos += this->step;
            }
        // }}}
    // }}}
    
    // modules {{{
        // struct Os {{{
            // struct Os_Console_Output {{{
                struct Os_Console_Output {
                    void* handle;
                    void* file;
                    void* writer;
                };
                
                uint64_t 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(&app.console.output, c.GetStdHandle(c.STD_OUTPUT_HANDLE));
                
            // }}}
        // }}}
        
        // struct Scalpi {{{
            // Scalpi_logger {{{
                struct Writer {
                    context: *WriterVTable;
                };
                
                struct WriterVTable {
                    uint64_t (*write) (void* context, uint8_t* bytes, uint64_t bytes_len, uint64_t* writed_result);
                }
                
                struct Scalpi_logger {
                    // write to terminal, console, file or to all together, but no real check "bytes is writen"
                    
                    uint64_t writed;
                    Scalpi_Writer writer;
                    
                    bool file_work;
                    uintptr_t file;
                    
                    bool file_writer_work;
                    Writer file_writer;
                    
                    bool console_writer_work;
                    Writer console_writer;
                    
                    bool  terminal_writer_work;
                    Writer terminal_writer;
                };
                
                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 (file_writer_work) {
                        r = writeToWriter(&t.file_writer, bytes, bytes_len);
                        writed = true;
                    }
                    
                    if (console_writer_work) {
                        r = writeToWriter(&t.console_writer, bytes, bytes_len);
                        writed = true;
                    }
                    
                    if (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_Console console;
            };
            
            enum App_init_result {
                App_init_result_ok,
                App_init_result_WinApiNotInit,
                App_init_result_argsNotParsed,
            };
            
            uint64_t App_init(struct App* app) {
                uint64_t r = 0;
                
                // preinit console output for debug
                Scalpi_logger_init(&app.logger);
                Scalpi_logger_writeAll(&app->logger, sliceArgFromConst("writed_by_logger\r\n"));
                Scalpi_logger_writeAll(&app->logger, "writed_by_logger\r\n", 17);
                //Os_Console_Output_init(&app.console.output, c.GetStdHandle(c.STD_OUTPUT_HANDLE));
                
                //Scalpi_DebugHelper_on_exit(&process->debug_helper, __LINE__);
                
                // pre init console output for debug
                
                // try app.console.output.init(c.GetStdHandle(c.STD_OUTPUT_HANDLE));
                // t.logger.console_writer = &t.console.output.writer;
                
                // mem
                // try t.mem.init();
                // errdefer t.mem.deinit();
                // const allocator = &t.mem.allocator;
                // fuck(allocator);
                
                // Os init
                // try t.winapi.init();
                // errdefer t.winapi.deinit();
                
                // console
                // try t.console.init(t.winapi.console_input_handle, t.winapi.console_output_handle);
                // try log.writeAll(" - - - OwO - - -\r");
                
                // terminal
                // t.terminal.init(&t.console) catch return error.TerminalNotInit;
                // errdefer t.terminal.deinit();
                //t.terminal.output.convert_t = true;
                //t.terminal.output.convert_n_to_rn = true;
                //t.terminal.output.apply_r = true;
                //t.terminal.output.apply_n = true;
                
                // editor
                // try t.editor.init(allocator);
                // errdefer t.editor.deinit();
                
                // parse_args
                // try t.parseArgs();
                
                // t.editor.views.flat.setAsView() catch return error.AnViewFlatSetAsView;
                // try t.editor.views.flat.fold();
                // try t.editor.views.folded.markThisLine();
                
                //
                // t.logger.terminal_writer = &t.terminal.output.writer;
                // t.logger.console_writer = null;
                // t.working = true;
                
                return r;
            }
            
            void App_deinit(struct App* app) {}
            
            void App_loop(struct App* app) {}
        // }}}
    // }}}
    
    // main {{{
        struct App app;
        
        enum main_result {
            main_result_ok,
            main_result_AppNotInit,
            main_result_ErrorOnLoop,
        };
        
        int main() {
            uint64_t r; // result (error state) for errorable function;
            printf(" ScalpiEditor.c \r\n");
            
            r = App_init(&app);
            if (r != 0) {
                r = (r << 4) | main_result_AppNotInit;
                printf("%d: result %llu \r\n", __LINE__, r);
                return r;
            }
            
            App_loop(&app);
            App_deinit(&app);
            printf("App deinited\r\n");
            
            return main_result_ok;
        }
    // }}}
// }}}