Files
Nomi/source/main.c
2025-10-30 18:08:11 -07:00

285 lines
8.3 KiB
C

/* #############################################################################
* # nomi
* this is a note takeing system where the user can make notes about the nomi
*
* AUTHER: Preacher
* DATE: 10/23/2025
* #############################################################################
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <tart.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include "commands.h"
#define KEY_BACKSPACE 127
#define COMMAND_MODE 0x01
#define NORMAL_MODE 0x02
#define INSERT_MODE 0x03
#define MIN_LINE_COUNT_RTB 30
unsigned char __Close__ = 0;
void programClose(int sig) {
term_threaded_input_stop();
term_enable_cursor();
__Close__ = 1;
}
typedef struct {
char* text;
int len;
int stringCount;
struct tart_cell cell;
struct tart_cstring textBox[30];
struct tart_vec2 size;
struct tart_vec2 pos;
} richTextBox;
unsigned char mode = NORMAL_MODE;
void SetRichTextBox(richTextBox* rtb, char* text, int len,
struct tart_cell cell) {
int lineCount = 0;
int textOffset = 0;
char* p = text;
unsigned char found = 0;
rtb->stringCount = 5;
int sizeOfString = 0;
for(int i = 0; i < len; i++) {
sizeOfString = i - textOffset;
if(p[i] == '\n') {
// This means that it will start splitting strings
found = 1;
//p[i] = '\0'; // Sets this to be the end of the string
//Add the address to the cstring
if(lineCount == 5) {
break;
}
rtb->textBox[lineCount] = tart_cstring(p + textOffset,
sizeOfString, rtb->cell);
textOffset = i + 1; // Sets the start of the next string.
lineCount++;
}
}
tart_cstring_free(&rtb->textBox[lineCount]);
rtb->textBox[lineCount] = tart_cstring(p + textOffset,
strlen(p), rtb->cell);
}
void drawTextBox(struct tart_window* window, richTextBox* rtb, tart_byte id) {
struct tart_buffer* sb = tart_get_buffer(window, id);
// This makes sure that we dont go outside of memeery with the string count.
for (int lineNo = 0; lineNo < rtb->stringCount; lineNo++) {
tart_draw_cstring_position(sb,
rtb->textBox[lineNo],
(struct tart_vec2){rtb->pos.x,rtb->pos.y + lineNo});
}
}
//############ Init Commands ############
void Command_CloseProgram(void* cmd) {
programClose(0);
}
void Command_WrightToFile(void* cmd) {
// wright to the file
}
void Command_New_Thought(void* cmd) {
// Create A New Thought
}
//############ Init Commands ############
int main (int argc, char *argv[]) {
InitCommands();
AddCommand(Command_CloseProgram, "q");
AddCommand(Command_CloseProgram, "w");
term_disable_cursor();
term_threaded_input_init();
signal(SIGINT, programClose);
char str[255] = "This is a test";
char commandPromt[16] = "Enter Command: ";
char commandInput[255] = "";
unsigned char keyPressed = 0;
// Window Createion
struct tart_window window = tart_create_window();
struct tart_buffer buf = tart_create_buffer(0,
(struct tart_vec2){40,20},
(struct tart_vec2){0,0});
tart_add_buffer(&window, buf);
//Rich TextBox init
int inputIdx = 0;
int commandIdx = 0;
richTextBox inputRtb;
inputRtb.pos = (struct tart_vec2){0,0};
inputRtb.size = (struct tart_vec2){3,3};
SetRichTextBox(&inputRtb, str, 255, NULL_CELL);
struct tart_cstring commandPrompt_cstr = tart_cstring(commandPromt, 16, NULL_CELL);
struct tart_cstring commandInput_cstr = tart_cstring(commandInput, 255, NULL_CELL);
tart_restore_window(&window);
tart_restore_buffer(tart_get_buffer(&window, 0));
for (;!__Close__;) {
// LOOP START
if(keyPressed) {
tart_draw_window(&window, 0);
tart_restore_window(&window);
tart_restore_buffer(tart_get_buffer(&window, 0));
char c = term_tinput();
//Speual
SetRichTextBox(&inputRtb, str, strlen(str), NULL_CELL);
if(mode == INSERT_MODE && keyPressed) {
if(c == '\e') {
mode = NORMAL_MODE;
keyPressed = 0;
term_handled_key();
} else {
if(c == '\t' && keyPressed && inputIdx < 255) {
for(int i = 0; i < 4; i++) {
str[inputIdx] = ' ';
inputIdx++;
}
keyPressed = 0;
}else if (c == KEY_BACKSPACE && keyPressed && inputIdx < 255 && inputIdx > 0) {
inputIdx--;
str[inputIdx] = '\0';
} else {
str[inputIdx] = c;
inputIdx++;
}
keyPressed = 0;
term_handled_key();
}
SetRichTextBox(&inputRtb, str, strlen(str), NULL_CELL);
}
if(mode == COMMAND_MODE) {
if(c == '\e') {
commandIdx = 0;
for(int i = 0; i < 255; i++) {
commandInput[i] = '\0';
}
mode = NORMAL_MODE;
keyPressed = 0;
term_handled_key();
} else {
commandInput_cstr = tart_cstring(commandInput,
strlen(commandInput),
NULL_CELL);
if(c == '\t' && keyPressed && commandIdx + 4 < 255) {
for(int i = 0; i < 4; i++) {
commandInput[commandIdx] = ' ';
commandIdx++;
}
keyPressed = 0;
}else if (c == KEY_BACKSPACE &&
keyPressed &&
commandIdx < 255 &&
commandIdx > 0) {
commandIdx--;
commandInput[commandIdx] = '\0';
} else if (c == '\n' && keyPressed) {
// handle command.
tart_cstring_free(&commandPrompt_cstr);
commandInput_cstr = tart_cstring(commandInput,
strlen(commandInput),
NULL_CELL);
CommandRun(commandInput, commandIdx);
for(int i = 0; i < 255; i++) {
commandInput[i] = '\0';
}
keyPressed = 0;
term_handled_key();
commandIdx = 0;
mode = NORMAL_MODE;
}else {
commandInput[commandIdx] = c;
commandIdx++;
}
keyPressed = 0;
term_handled_key();
commandInput_cstr = tart_cstring(commandInput, strlen(commandInput), NULL_CELL);
}
}
// Key Events
if (c == ':' && mode == NORMAL_MODE) {
mode = COMMAND_MODE;
keyPressed = 0;
term_handled_key();
}
if (c == 'i' && mode == NORMAL_MODE) {
mode = INSERT_MODE;
keyPressed = 0;
term_handled_key();
}
keyPressed = 0;
}
drawTextBox(&window, &inputRtb, 0);
tart_draw_window(&window, 0);
if(mode == COMMAND_MODE) {
tart_draw_cstring_position(tart_get_buffer(&window, 0),
commandInput_cstr,
(struct tart_vec2){16,10});
tart_draw_cstring_position(tart_get_buffer(&window, 0),
commandPrompt_cstr,
(struct tart_vec2){0,10});
}
if(mode == NORMAL_MODE) {
}
if(mode == INSERT_MODE) {
}
// LOOP END
// Draw Begin
// Draw End
keyPressed = term_key_pressed();
}
tart_destroy_window(&window);
return 0;
}