Files
Nomi/source/main.c
2025-11-06 15:04:01 -08:00

336 lines
10 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"
#include "nomi.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();
__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;
rtb->text = text;
int sizeOfString = 0;
}
void drawTextBox(struct tart_window* window, richTextBox* rtb, tart_byte id) {
struct tart_buffer* sb = tart_get_buffer(window, id);
int lineNo = 0;
int lineStart = 0;
for(int i = 0; i < strlen(rtb->text); i++) {
if(rtb->text[i] == '\n') {
rtb->textBox[0] = tart_cstring(
&rtb->text[0]+lineStart,
i - lineStart,
NULL_CELL);
tart_draw_cstring_position(sb,
rtb->textBox[0],
(struct tart_vec2){rtb->pos.x, rtb->pos.y+lineNo});
tart_cstring_free(&rtb->textBox[0]);
lineNo++;
lineStart = i + 1;
}
}
rtb->textBox[0] = tart_cstring(
&rtb->text[0]+lineStart,
strlen(rtb->text) - lineStart,
NULL_CELL);
tart_draw_cstring_position(sb,
rtb->textBox[0],
(struct tart_vec2){rtb->pos.x, rtb->pos.y+lineNo});
tart_cstring_free(&rtb->textBox[0]);
}
//############ 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);
float animStep = 0;
char str[255] = "This is a test";
char commandPromt[16] = "Enter Command: ";
char commandInput[255] = "";
unsigned char keyPressed = 0;
char title[30] = "this is a test";
trunk tr = CreateTrunk(title,create_branch(NULL, (nomi_vec2){-0.5,1}, 0, TART_COLOR_GREEN_FOREGROUND));
AddBranch(&tr, create_branch(&tr.branches[0],(nomi_vec2){0.2,2*1}, 10, TART_COLOR_GREEN_FOREGROUND));
AddBranch(&tr, create_branch(&tr.branches[1],(nomi_vec2){-0.5,1}, 50, TART_COLOR_GREEN_FOREGROUND));
AddBranch(&tr, create_branch(&tr.branches[0],(nomi_vec2){-0.5,1}, 50, TART_COLOR_GREEN_FOREGROUND));
NomiInit(&tr);
// Window Createion
struct tart_window window = tart_create_window();
struct tart_buffer buf = tart_create_buffer(0,
term_current_size(),
(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));
unsigned char branch1Color = TART_COLOR_GREEN_FOREGROUND;
unsigned char branch2Color = TART_COLOR_GREEN_FOREGROUND;
unsigned char branch3Color = TART_COLOR_GREEN_FOREGROUND;
for (;!__Close__;) {
// LOOP START
tart_restore_window(&window);
tart_restore_buffer(tart_get_buffer(&window, 0));
//b = create_branch(NULL, (nomi_vec2){-0.5,1}, 0, branch1Color);
//b1 = create_branch(&b,(nomi_vec2){0.2,2*1}, 10, branch2Color);
//b2 = create_branch(&b,(nomi_vec2){-1.0,1}, 50, branch3Color);
if(keyPressed) {
char c = term_tinput();
//if( c == '1') {
// branch1Color = TART_COLOR_WHITE_FOREGROUND;
// branch2Color = TART_COLOR_GREEN_FOREGROUND;
// branch3Color = TART_COLOR_GREEN_FOREGROUND;
//}
//if( c == '2') {
// branch2Color = TART_COLOR_WHITE_FOREGROUND;
// branch1Color = TART_COLOR_GREEN_FOREGROUND;
// branch3Color = TART_COLOR_GREEN_FOREGROUND;
//}
//if( c == '3') {
// branch3Color = TART_COLOR_WHITE_FOREGROUND;
// branch1Color = TART_COLOR_GREEN_FOREGROUND;
// branch2Color = TART_COLOR_GREEN_FOREGROUND;
//}
//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();
}
if(mode == NORMAL_MODE) {
//NomiNavigation(c);
branchId* ids = NomiNavigation(c);
for(int i = 0; i < MAX_BRANCHES; i++) {
str[i] = ids[i]+48;
}
str[MAX_BRANCHES] = '\n';
ids = LocateBranch(&tr);
for(int i = 0; i < MAX_BRANCHES; i++) {
str[1+MAX_BRANCHES+i] = ids[i]+48;
}
str[(MAX_BRANCHES*2)+1] = '\n';
ids = LocateBranch(&tr);
for(int i = 0; i < MAX_BRANCHES; i++) {
str[2+(MAX_BRANCHES*2)+i] = ids[i]+48;
}
term_handled_key();
}
keyPressed = 0;
}
drawTextBox(&window, &inputRtb, 0);
LocateBranch(&tr);
// Drawing Branches
DrawTrunk(&tr, &window, 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);
term_enable_cursor();
return 0;
}