From 990d5dfbccf98c5850e63b9287878af9719e8c54 Mon Sep 17 00:00:00 2001 From: Rockingcool Date: Mon, 10 Oct 2022 15:49:16 -0500 Subject: [PATCH] First commit --- README.md | 0 fileman.c | 145 ++++++++++ fileman.h | 18 ++ program3-4 | Bin 0 -> 41032 bytes program3-4-2.c | 700 ++++++++++++++++++++++++++++++++++++++++++++++ program3-4.c | 738 +++++++++++++++++++++++++++++++++++++++++++++++++ sample1.txt | 4 + test1.c | 5 + texted.c | 683 +++++++++++++++++++++++++++++++++++++++++++++ texted.h | 21 ++ 10 files changed, 2314 insertions(+) create mode 100644 README.md create mode 100644 fileman.c create mode 100644 fileman.h create mode 100755 program3-4 create mode 100644 program3-4-2.c create mode 100644 program3-4.c create mode 100644 sample1.txt create mode 100644 test1.c create mode 100644 texted.c create mode 100644 texted.h diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/fileman.c b/fileman.c new file mode 100644 index 0000000..0cf7a26 --- /dev/null +++ b/fileman.c @@ -0,0 +1,145 @@ +#include "fileman.h" + + +static int global_arr_size = FILE_ARR_SIZE; + +//int global_arr_size = 5; + +int findFileCount(char* directory) { + int fileCount=0; + DIR* d; + struct dirent* dir; + d = opendir(directory); + + if (d) { + while ((dir = readdir(d)) != NULL) { +// mvwprintw(win,startRow,startCol,"%s",dir->d_name); + if ((strcmp(dir->d_name,".") != 0) && (strcmp(dir->d_name,"..") != 0)) { + fileCount++; + } + //startRow++; + } + closedir(d); + } + return fileCount; +} + + + +void addFiles(char* directory, char** list,int start,int end) { +// for (int i=0;id_name,".") != 0) && (strcmp(dir2->d_name,"..") != 0)) { +// list[count] = dir2->d_name; + *(count+list) = dir2->d_name; + count++; + } + + if (count >= global_arr_size) { + global_arr_size += FILE_ARR_SIZE; + list = realloc(list,(sizeof(char *) * global_arr_size)); +// abort(); + // printf("%d\n",lengthOfArr); + // abort(); + } + + } + closedir(d2); + } +} + + + +/*void removeDirFromPath (char** ptrPath) { + int slashLoc = 0; + int counter = 0; + for (int i=strlen(*ptrPath)-1;i>=0;i--) { + if (counter==1) + slashLoc = i; + if (*ptrPath[i] == '/') + counter++; + } + for (int i=slashLoc+1;i=0;i--) { + if (counter==1 && *(ptrPath+i) == '/') + slashLoc = i; + if (*(ptrPath+i) == '/') + counter++; + } + for (int i=slashLoc+1;i=0;i--) { + if ((*(ptrPath+i) != '\0') && (*(ptrPath+i) != '/')) { + *(ptrPath+i+1) = '/'; + return; + } + } + +} + + +bool isFile(char *ptrDir) { + if (!(fileExists(ptrDir))) { + return true; + } + struct stat pathstat; + stat(ptrDir,&pathstat); + return S_ISREG(pathstat.st_mode); +} + +bool fileExists(char *ptrDir) { + return access(ptrDir,F_OK) == 0; +} diff --git a/fileman.h b/fileman.h new file mode 100644 index 0000000..1e74918 --- /dev/null +++ b/fileman.h @@ -0,0 +1,18 @@ +#ifndef FILEMAN_H_ + #define FILEMAN_H_ + #include + #include + #include + #include + #include + #include + + #define FILE_ARR_SIZE 100 + + void addFiles(char*,char**,int,int); + int findFileCount(char*); + void removeDirFromPath(char*); + void addSlash(char *); + bool isFile(char *); + bool fileExists(char *); +#endif diff --git a/program3-4 b/program3-4 new file mode 100755 index 0000000000000000000000000000000000000000..af5c7a8b8843d6756575c5b559e5244a1ba89311 GIT binary patch literal 41032 zcmeHwdwf*Yx%Qq(CJ6*G;f^RWASkE_SCNP)w}HWOzbL)vFia*Sn#{zR2?0waIEiIC zMAH^6T6+$)wzWOl($-o%G70UH7#&*<0O<7uan!Lti<@H3p&1izH^YA{2a6WsuA^W*Gf(A7_j;@&M-w zoTSf|38?ZqX3tCu6g~$j<%ToUOL~Ds8#>h0 z>UdHOe=}4(v(wq2@tQ5}hLJ6|Ny%lW^A*2NX@l3QKSs;{C-#hhB6s$kuwl1($FmrS2l8VQw7 z5&ZOP5`L`yg-h2M_C}+PI4y58Y#Fd!!C(;=dD&DMcFO;;=&BXBKmYYRK5F{)%=?|q zUmmn>-NWQzIjEC7yolpZUvz&Ce&kW{u^EjL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fv(X8ZqhC9mUetW%6Cot`F0 zypEsZGM++){%baU-mcR}G^0wh?eon#vwU*BWYlSP`^~n06zr^W)$P*HG1_XRo@{;% z>-0^qIZg8Vxyj*wQoakaY&+V%eh$&0W9)BYQGNXkhi zugj_VT{@%E{>uKOEWDP_J_})4dFgbhl9yQ1JuLI5Oekiv{o&sh(Ynm6rn-FP z8m0U-no)vjUp2Zy$t!X?d1{-?&P`)vM$2h5VLf(eoJdtW>Qw(w;vyjVtW~!^J^uFr kF5UKopuBbNkm=8~fTCsF9{_xB@?XD43T(_0$O4W31Kz>ml>h($ literal 0 HcmV?d00001 diff --git a/program3-4-2.c b/program3-4-2.c new file mode 100644 index 0000000..eaf3d83 --- /dev/null +++ b/program3-4-2.c @@ -0,0 +1,700 @@ + +/* + _______ ________ _____ ________ __ _______ __ __ + |__ __| | ______| | _ \ | ______| | | | _____| \ \ / / + | | | |___ | | \ | | |___ | | | |__ \ \/ / + | | | ____| | | | | | ____| | | | ___| / /\ \ + | | | |______ | |_ / | | | | |____ | |_____ / / \ \ + |_| |________| |_____ / |_| |_______| |_______| /__/ \__\ + +*/ + +/* TedFlex - A File Explorer, File Viewer, and Text Editor. + +A combination of two of my programs: Ted (Text EDitor) and Flex (FiLe EXplorer). + +This is an ncurses tool allows you to browse your files and view/edit them in the same window. + +*/ + + +#include "texted.h" +#include "fileman.h" + +#include +#include + +#define MAX_LINE_LENGTH 142857 //This is the maximum length of a line that we can read from a file. +#define START_WIN 5 //This is the position on the terminal window from where we will start drawing our ncurses window. + + + +int main(int argc,char** argv) { + int xLen,yLen,mainX,mainY; //values for the x-length and y-length of stdscr (i.e. the terminal window), and the 'main window' inside it. + int fileCountNum; //Number of files in a given directory + int highlighted=0; //Index of the highlighted file + int choice; //stores the user's key press + int fileRow; //Line number that the user is on when they choose to open a file + int fileCol; //ISN'T USED ELSEWHERE IN THE PROGRAM - SHOULD BE REMOVED + int numNewLines; //Number of new-line characters read in a file + int rowCounter; //Number of rows to skip reading in a file, when the user presses Page Down + bool eofFound; //Self-explanatory; is EOF found? + int colorScheme; //stores the value of the color scheme - changes when user hits Ctrl-D + int colCounter; //ISN'T USED ELSEWHERE IN THE PROGRAM - SHOULD BE REMOVED + char tempChar; //Holds the 'current' character while reading from a file. + int numCharsToPrint; //Weird naming - should be renamed to 'numLinesToPrint' - Number of lines to print in the file explorer. + int numDowns=0; //Stores the number of times the user has pressed the Down Arrow / Page Down + bool hasExited; //Has the user exited from TextView mode? + bool hasChosen; //Has the user chosen to edit or view a file? + char editOrView; //Did the user choose to edit or view a file? + char line[MAX_LINE_LENGTH]; //Holds a line in a text file when the user chooses to view a file + FILE* ptrFile; //Used to open a file to read from + + + bool hasCopied; + FILE* tempFileCopy; + + + char* curDir; + + setlocale(LC_CTYPE,""); + + initscr(); //NCURSES - Initializes screen to begin doing stuff in ncurses + getmaxyx(stdscr,yLen,xLen); /*NCURSES - Gets the maximum x- and y-length of stdscr, which represents the terminal/TTY window + that is running the program. Used to make calculations for the 'main' window based on the size. */ + + curs_set(0); //NCURSES - Makes the cursor (the white rectangle) invisible. + noecho(); //NCURSES - prevents echoing of user choices. + cbreak(); //NCURSES - Similar to Raw Mode, key presses are registered immediately after they are typed. + if (has_colors() == TRUE) { + start_color(); //NCURSES - Initialize color - TODO - Disable colors if color support is not found. + } + +/* NCURSES - The next block of code creates colors and color pairs for our window. */ + + init_color(COLOR_CYAN,500,500,500); + init_color(COLOR_MAGENTA,150,150,150); + init_color(COLOR_YELLOW,650,650,650); + init_color(COLOR_GREEN,40,291,177); + + init_pair(0,COLOR_WHITE,COLOR_BLACK); + init_pair(1,COLOR_BLACK,COLOR_CYAN); + init_pair(2,COLOR_WHITE,COLOR_MAGENTA); + init_pair(4,COLOR_GREEN,COLOR_CYAN); + init_pair(6,COLOR_WHITE,COLOR_BLUE); + init_pair(8,COLOR_BLACK,COLOR_CYAN); + init_pair(10,COLOR_BLACK,COLOR_BLACK); + init_pair(11,COLOR_BLACK,COLOR_YELLOW); + init_pair(12,COLOR_WHITE,COLOR_BLACK); + +/* BLOCK ENDS HERE */ + + colorScheme=1; // Default value for the color scheme. + + rowCounter = 1; // Set this to zero, because we don't want to skip anything + colCounter = 1; + + WINDOW* mainwin = newwin(yLen-7,xLen-9,START_WIN,START_WIN); /*NCURSES - This creates our 'main' window, that will be + used to display ~99% of the actual content.*/ + + wbkgd(stdscr,COLOR_PAIR(6)); //NCURSES - Sets the default foreground and background color for stdscr. + wbkgd(mainwin,COLOR_PAIR(8)); //NCURSES - Sets the default foreground and background color for mainwin. + keypad(mainwin,true); //NCURSES - Allows the use of 'keypad' values: Arrow Keys, Function Keys, etc. + getmaxyx(mainwin,mainY,mainX); //NCURSES - Gets the maximum x- and y-values of the mainwin, and stores them in the appropriate variables. + + wattron(stdscr,COLOR_PAIR(10)); //NCURSES - Sets the color for the 'shadows' in the following block of code. + +/* NCURSES - The following block of code adds 'shadows' to the mainwin. */ + + for (int i=START_WIN+1;i files in the directory 'dir' to the fileList. The reason behind the limit + in the number of files added is that, with large directories (e.g. /lib), I encountered a + weird bug where, after a certain number of files, the list corrupts itself. My solution + was to add a limited number of files at a given time, and change those files depending on + where the user is in the file list. + */ + + + if (!(isFile(dir))) { + addFiles(dir,fileList,0,mainY+5); + } + + int firstItemIndex = 0; //Initializes the index of the first item in the list + + /* This block prints some helpful information regarding the controls to navigte within the program */ + + wmove(stdscr,1,0); + wclrtoeol(stdscr); + mvwaddstr(stdscr,1,17," - Move Up | - Move Down | - Go Back | /Enter - Enter Directory/Open File\n"); + wmove(stdscr,2,0); + wclrtoeol(stdscr); + mvwaddstr(stdscr,2,40," - Scroll Up | - Scroll Down\n"); + + /* BlOCK ENDS HERE */ + + while (true) { + + +// getmaxyx(stdscr,yLen,xLen); +// getmaxyx(mainwin,mainY,mainX); + + wmove(stdscr,3,0); + wclrtoeol(stdscr); + + if (highlighted == firstItemIndex+mainY) { + firstItemIndex++; + refresh(); + wrefresh(mainwin); + } + + + + if (isFile(dir)) { + if (hasChosen == false) { + mvwaddstr(mainwin,5,5,"Do you want to (e)dit or (v)iew this file? "); + editOrView='$'; + if (!(fileExists(dir))) { + while (editOrView != 'e') { + editOrView = wgetch(mainwin); + } + } + else { + while (editOrView != 'e' && editOrView != 'v') { + editOrView = wgetch(mainwin); + } + } + if (editOrView == 'e') { + curs_set(2); + run(dir); + curs_set(0); + wmove(stdscr,1,0); + wclrtoeol(stdscr); + wmove(stdscr,2,0); + wclrtoeol(stdscr); + hasExited = true; + } + } + if (editOrView == 'v' /*&& hasChosen == false*/) { + wclear(mainwin); + refresh(); + wrefresh(mainwin); + mvwprintw(stdscr,3,0,"Line %d",rowCounter); + + ptrFile = fopen(dir,"r"); + rewind(ptrFile); + fileRow=1; + for (int i=0;i<(rowCounter-1);i++) { + fgets(line,MAX_LINE_LENGTH,ptrFile); + } + + while(fgets(line, MAX_LINE_LENGTH, ptrFile)) { + mvwprintw(mainwin,fileRow,1,"%s",line); + fileRow++; + } + fclose(ptrFile); + refresh(); + wrefresh(mainwin); + hasChosen=true; + } + } else { + + if (mainY < fileCountNum) { + if (mainY < fileCountNum-firstItemIndex){ + numCharsToPrint = mainY; + } else { + numCharsToPrint = mainY-1; + } + } else { + numCharsToPrint = fileCountNum; + } + + + + + + for (int i=0,a=0;i - Move Up | - Move Down | - Go Back | /Enter - Enter Directory/Open File\n"); + wmove(stdscr,2,0); + wclrtoeol(stdscr); + mvwaddstr(stdscr,2,40," - Scroll Up | - Scroll Down"); + + + } else { + choice=wgetch(mainwin); + switch (choice) { + + case (('d' & 0x1f)): + + if (colorScheme == 0) { + wbkgd(stdscr,COLOR_PAIR(6)); + init_color(COLOR_GREEN,40,291,177); + init_pair(4,COLOR_GREEN,COLOR_CYAN); + wbkgd(mainwin,COLOR_PAIR(8)); + colorScheme++; + refresh(); + wrefresh(mainwin); + break; + } else if (colorScheme == 1){ + wbkgd(stdscr,COLOR_PAIR(2)); + init_pair(4,COLOR_GREEN,COLOR_BLACK); + wbkgd(mainwin,COLOR_PAIR(0)); + colorScheme++; + refresh(); + wrefresh(mainwin); + break; + } else if (colorScheme == 2) { + wbkgd(stdscr,COLOR_PAIR(12)); + init_color(COLOR_GREEN,40,438,7); + init_pair(4,COLOR_GREEN,COLOR_YELLOW); + wbkgd(mainwin,COLOR_PAIR(11)); + colorScheme=0; + refresh(); + wrefresh(mainwin); + break; + + + } + break; + + + + case KEY_UP: + + if (isFile(dir)) { + if (rowCounter <= 1) { + rowCounter = 1; + break; + } + + rowCounter--; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + break; + } + + + + + highlighted--; + if (highlighted == -1) { + highlighted=0; + } + if (highlighted < 1) { + if (firstItemIndex > 0) { + firstItemIndex--; + highlighted++; + addFiles(dir,(char **)fileList,firstItemIndex,mainY+5); + numDowns--; + + + if (highlighted > mainY-3) + highlighted=mainY-3; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + } + } + break; + case KEY_DOWN: + + if (isFile(dir)) { + numNewLines = 0; + eofFound = false; + ptrFile = fopen(dir,"r"); + for (int i=0;i<(rowCounter-1);i++) { + fgets(line,MAX_LINE_LENGTH,ptrFile); + } + + while (true) { + tempChar = fgetc(ptrFile); + if (tempChar == '\n') { + numNewLines++; + } + if (tempChar == EOF) { + eofFound = true; + break; + } + if (numNewLines >= (mainY-5)) { + break; + } + } + if (eofFound) { + break; + } + fclose(ptrFile); + rowCounter++; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + break; + } + + + if (highlighted > mainY-3) { + break; + } + + if (fileCountNum < mainY-1 && highlighted+numDowns+1 >= fileCountNum) { + break; + } + + + highlighted++; + /* + if (highlighted+numDowns >= fileCountNum) { + if (numCharsToPrint-1 < mainY-2) { + highlighted = numCharsToPrint-1; + } else { + highlighted = mainY-2; + } + break; + } + */ + + + + /* + + if (highlighted > mainY-2) { + if (firstItemIndex < fileCountNum-1) { + firstItemIndex++; + highlighted--; + numDowns++; + addFiles(dir,(char **)fileList,firstItemIndex,mainY+5); + + wclear(mainwin); + refresh(); + wrefresh(mainwin); + } + } + + + + + */ + break; + + case KEY_NPAGE: + if (isFile(dir)) { + numNewLines = 0; + eofFound = false; + ptrFile = fopen(dir,"r"); + for (int i=0;i<(rowCounter-1);i++) { + fgets(line,MAX_LINE_LENGTH,ptrFile); + } + + while (true) { + tempChar = fgetc(ptrFile); + if (tempChar == '\n') { + numNewLines++; + } + if (tempChar == EOF) { + eofFound = true; + break; + } + if (numNewLines >= (mainY-5)) { + break; + } + } + if (eofFound) { + break; + } + fclose(ptrFile); + rowCounter += mainY-5; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + break; + } + + + + + + if (strcmp("linux",getenv("TERM")) == 0) { + mainY+=1; + } + + if (fileCountNum - firstItemIndex > mainY-3) { + firstItemIndex++; + numDowns++; + addFiles(dir,(char **)fileList,firstItemIndex,mainY+5); + wclear(mainwin); + refresh(); + wrefresh(mainwin); + } + + if (strcmp("linux",getenv("TERM")) == 0) { + mainY-=1; + } + + break; + + case KEY_PPAGE: + + + if (isFile(dir)) { + if (rowCounter-mainY+5 <= 1) { + rowCounter = 1; + break; + } + + rowCounter -= mainY+5; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + break; + } + + + if (firstItemIndex > 0) { + firstItemIndex--; + + addFiles(dir,(char **)fileList,firstItemIndex,mainY+5); + + if (highlighted > firstItemIndex+mainY-3) + highlighted=firstItemIndex+mainY-3; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + } + break; + +/* *************************************************************************************** */ +// STUFF TO IMPLEMENT +// +// 1. Show the content of files in another ncurses window +//////////// 2. Use Arrow Keys to scroll in file (line-by-line) +//////////// 3. Define symbolic constants for commonly used values +/* *************************************************************************************** */ + + + + case KEY_RIGHT: + case 10: + if (!(isFile(dir))) { + strcat(dir,*(fileList+highlighted)); + if (!(isFile(dir))) { + strcat(dir,"/"); + } + move(0,1); + printw("%s",dir); + fileCountNum = findFileCount(dir); + firstItemIndex=0; + addFiles(dir,(char **)fileList,firstItemIndex,mainY+5); + numDowns=0; + + highlighted=0; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + } + break; + + case KEY_LEFT: + removeDirFromPath(dir); + highlighted=0; + move(0,0); + wclrtoeol(stdscr); + refresh(); + wrefresh(mainwin); + mvwprintw(stdscr,0,1,"%s",dir); + fileCountNum = findFileCount(dir); + addFiles(dir,(char **)fileList,0,mainY+5); + numDowns=0; + firstItemIndex=0; + highlighted=0; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + + hasChosen=false; + + rowCounter=1; + break; + + case ('n' & 0x1f): +// WINDOW* newFileWin = newwin(6,50,mainY/2-3,mainX/2-25); +// mvwaddstr(newFileWin,1,1,"Enter the name of the file you wish to create"); + char* newFileLoc = getSaveLoc(mainX,mainY,"$$$"); + char* newSaveLoc = malloc(sizeof(char) * (strlen(dir) + strlen(newFileLoc)) + 1); + strcpy(newSaveLoc,dir); +// strcat(newSaveLoc,"/"); + strcat(newSaveLoc,newFileLoc); + curs_set(2); + run(newSaveLoc); + curs_set(0); + refresh(); + wmove(mainwin,2,0); + wclrtoeol(mainwin); + wrefresh(mainwin); + break; + + case ('p' & 0x1f): + char* tempFileDir = malloc(sizeof(char) * MAX_LINE_LENGTH); + strcpy(tempFileDir,dir); +// strcat(tempFileDir,"/"); + strcat(tempFileDir,fileList[highlighted]); + ptrFile = fopen(tempFileDir,"r"); + tempFileCopy = fopen("/tmp/tedflexTempFile","w"); + char tempChar = 'a'; + while(!feof(ptrFile)) { + fscanf(ptrFile,"%c",&tempChar); + fprintf(tempFileCopy,"%c",tempChar); + } + hasCopied = true; + fclose(tempFileCopy); + fclose(ptrFile); +// rewind(ptrFile); + break; + + + + default: + break; + + } + } + + if (choice == 113){ + endwin(); + break; + } + } + + + return 0; +} + + + + + + + + + diff --git a/program3-4.c b/program3-4.c new file mode 100644 index 0000000..63bddb0 --- /dev/null +++ b/program3-4.c @@ -0,0 +1,738 @@ + +/* + _______ ________ _____ ________ __ _______ __ __ + |__ __| | ______| | _ \ | ______| | | | _____| \ \ / / + | | | |___ | | \ | | |___ | | | |__ \ \/ / + | | | ____| | | | | | ____| | | | ___| / /\ \ + | | | |______ | |_ / | | | | |____ | |_____ / / \ \ + |_| |________| |_____ / |_| |_______| |_______| /__/ \__\ + +*/ + +/* TedFlex - A File Explorer, File Viewer, and Text Editor. + +A combination of two of my programs: Ted (Text EDitor) and Flex (FiLe EXplorer). + +This is an ncurses tool allows you to browse your files and view/edit them in the same window. + +*/ + + +#include "texted.h" +#include "fileman.h" + +#include +#include +#include +#include + +#define MAX_LINE_LENGTH 142857 //This is the maximum length of a line that we can read from a file. +#define START_WIN 5 //This is the position on the terminal window from where we will start drawing our ncurses window. + + + +int main(int argc,char** argv) { + int xLen,yLen,mainX,mainY; //values for the x-length and y-length of stdscr (i.e. the terminal window), and the 'main window' inside it. + int fileCountNum; //Number of files in a given directory + int highlighted=0; //Index of the highlighted file + int choice; //stores the user's key press + int fileRow; //Line number that the user is on when they choose to open a file + int fileCol; //ISN'T USED ELSEWHERE IN THE PROGRAM - SHOULD BE REMOVED + int numNewLines; //Number of new-line characters read in a file + int rowCounter; //Number of rows to skip reading in a file, when the user presses Page Down + bool eofFound; //Self-explanatory; is EOF found? + int colorScheme; //stores the value of the color scheme - changes when user hits Ctrl-D + int colCounter; //ISN'T USED ELSEWHERE IN THE PROGRAM - SHOULD BE REMOVED + char tempChar; //Holds the 'current' character while reading from a file. + int numCharsToPrint; //Weird naming - should be renamed to 'numLinesToPrint' - Number of lines to print in the file explorer. + int numDowns=0; //Stores the number of times the user has pressed the Down Arrow / Page Down + bool hasExited; //Has the user exited from TextView mode? + bool hasChosen; //Has the user chosen to edit or view a file? + char editOrView; //Did the user choose to edit or view a file? + char line[MAX_LINE_LENGTH]; //Holds a line in a text file when the user chooses to view a file + FILE* ptrFile; //Used to open a file to read from + + + bool hasCopied; + FILE* tempFileCopy; + + + char* curDir; + + char* fileCopied; + + setlocale(LC_CTYPE,""); + + initscr(); //NCURSES - Initializes screen to begin doing stuff in ncurses + getmaxyx(stdscr,yLen,xLen); /*NCURSES - Gets the maximum x- and y-length of stdscr, which represents the terminal/TTY window + that is running the program. Used to make calculations for the 'main' window based on the size. */ + + curs_set(0); //NCURSES - Makes the cursor (the white rectangle) invisible. + noecho(); //NCURSES - prevents echoing of user choices. + cbreak(); //NCURSES - Similar to Raw Mode, key presses are registered immediately after they are typed. + if (has_colors() == TRUE) { + start_color(); //NCURSES - Initialize color - TODO - Disable colors if color support is not found. + } + +/* NCURSES - The next block of code creates colors and color pairs for our window. */ + + init_color(COLOR_CYAN,500,500,500); + init_color(COLOR_MAGENTA,150,150,150); + init_color(COLOR_YELLOW,650,650,650); + init_color(COLOR_GREEN,40,291,177); + + init_pair(0,COLOR_WHITE,COLOR_BLACK); + init_pair(1,COLOR_BLACK,COLOR_CYAN); + init_pair(2,COLOR_WHITE,COLOR_MAGENTA); + init_pair(4,COLOR_GREEN,COLOR_CYAN); + init_pair(6,COLOR_WHITE,COLOR_BLUE); + init_pair(8,COLOR_BLACK,COLOR_CYAN); + init_pair(10,COLOR_BLACK,COLOR_BLACK); + init_pair(11,COLOR_BLACK,COLOR_YELLOW); + init_pair(12,COLOR_WHITE,COLOR_BLACK); + +/* BLOCK ENDS HERE */ + + colorScheme=1; // Default value for the color scheme. + + rowCounter = 1; // Set this to zero, because we don't want to skip anything + colCounter = 1; + + WINDOW* mainwin = newwin(yLen-7,xLen-9,START_WIN,START_WIN); /*NCURSES - This creates our 'main' window, that will be + used to display ~99% of the actual content.*/ + + wbkgd(stdscr,COLOR_PAIR(6)); //NCURSES - Sets the default foreground and background color for stdscr. + wbkgd(mainwin,COLOR_PAIR(8)); //NCURSES - Sets the default foreground and background color for mainwin. + keypad(mainwin,true); //NCURSES - Allows the use of 'keypad' values: Arrow Keys, Function Keys, etc. + getmaxyx(mainwin,mainY,mainX); //NCURSES - Gets the maximum x- and y-values of the mainwin, and stores them in the appropriate variables. + + wattron(stdscr,COLOR_PAIR(10)); //NCURSES - Sets the color for the 'shadows' in the following block of code. + +/* NCURSES - The following block of code adds 'shadows' to the mainwin. */ + + for (int i=START_WIN+1;i files in the directory 'dir' to the fileList. The reason behind the limit + in the number of files added is that, with large directories (e.g. /lib), I encountered a + weird bug where, after a certain number of files, the list corrupts itself. My solution + was to add a limited number of files at a given time, and change those files depending on + where the user is in the file list. + */ + + + if (!(isFile(dir))) { + addFiles(dir,fileList,0,mainY+5); + } + + int firstItemIndex = 0; //Initializes the index of the first item in the list + + /* This block prints some helpful information regarding the controls to navigte within the program */ + + wmove(stdscr,1,0); + wclrtoeol(stdscr); + mvwaddstr(stdscr,1,17," - Move Up | - Move Down | - Go Back | /Enter - Enter Directory/Open File\n"); + wmove(stdscr,2,0); + wclrtoeol(stdscr); + mvwaddstr(stdscr,2,40," - Scroll Up | - Scroll Down\n"); + + /* BlOCK ENDS HERE */ + + while (true) { + + +// getmaxyx(stdscr,yLen,xLen); +// getmaxyx(mainwin,mainY,mainX); + + wmove(stdscr,3,0); + wclrtoeol(stdscr); + + if (highlighted == firstItemIndex+mainY) { + firstItemIndex++; + refresh(); + wrefresh(mainwin); + } + + + + if (isFile(dir)) { + if (hasChosen == false) { + mvwaddstr(mainwin,5,5,"Do you want to (e)dit or (v)iew this file? "); + editOrView='$'; + if (!(fileExists(dir))) { + while (editOrView != 'e') { + editOrView = wgetch(mainwin); + } + } + else { + while (editOrView != 'e' && editOrView != 'v') { + editOrView = wgetch(mainwin); + } + } + if (editOrView == 'e') { + curs_set(2); + run(dir); + curs_set(0); + wmove(stdscr,1,0); + wclrtoeol(stdscr); + wmove(stdscr,2,0); + wclrtoeol(stdscr); + hasExited = true; + } + } + if (editOrView == 'v' /*&& hasChosen == false*/) { + wclear(mainwin); + refresh(); + wrefresh(mainwin); + mvwprintw(stdscr,3,0,"Line %d",rowCounter); + + ptrFile = fopen(dir,"r"); + rewind(ptrFile); + fileRow=1; + for (int i=0;i<(rowCounter-1);i++) { + fgets(line,MAX_LINE_LENGTH,ptrFile); + } + + while(fgets(line, MAX_LINE_LENGTH, ptrFile)) { + mvwprintw(mainwin,fileRow,1,"%s",line); + fileRow++; + } + fclose(ptrFile); + refresh(); + wrefresh(mainwin); + hasChosen=true; + } + } else { + + if (mainY < fileCountNum) { + if (mainY < fileCountNum-firstItemIndex){ + numCharsToPrint = mainY; + } else { + numCharsToPrint = mainY-1; + } + } else { + numCharsToPrint = fileCountNum; + } + + + + + + for (int i=0,a=0;i - Move Up | - Move Down | - Go Back | /Enter - Enter Directory/Open File\n"); + wmove(stdscr,2,0); + wclrtoeol(stdscr); + mvwaddstr(stdscr,2,40," - Scroll Up | - Scroll Down"); + + + } else { + choice=wgetch(mainwin); + switch (choice) { + + case (('d' & 0x1f)): + + if (colorScheme == 0) { + wbkgd(stdscr,COLOR_PAIR(6)); + init_color(COLOR_GREEN,40,291,177); + init_pair(4,COLOR_GREEN,COLOR_CYAN); + wbkgd(mainwin,COLOR_PAIR(8)); + colorScheme++; + refresh(); + wrefresh(mainwin); + break; + } else if (colorScheme == 1){ + wbkgd(stdscr,COLOR_PAIR(2)); + init_pair(4,COLOR_GREEN,COLOR_BLACK); + wbkgd(mainwin,COLOR_PAIR(0)); + colorScheme++; + refresh(); + wrefresh(mainwin); + break; + } else if (colorScheme == 2) { + wbkgd(stdscr,COLOR_PAIR(12)); + init_color(COLOR_GREEN,40,438,7); + init_pair(4,COLOR_GREEN,COLOR_YELLOW); + wbkgd(mainwin,COLOR_PAIR(11)); + colorScheme=0; + refresh(); + wrefresh(mainwin); + break; + + + } + break; + + + + case KEY_UP: + + if (isFile(dir)) { + if (rowCounter <= 1) { + rowCounter = 1; + break; + } + + rowCounter--; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + break; + } + + + + + highlighted--; + if (highlighted == -1) { + highlighted=0; + } + if (highlighted < 1) { + if (firstItemIndex > 0) { + firstItemIndex--; + highlighted++; + addFiles(dir,(char **)fileList,firstItemIndex,mainY+5); + numDowns--; + + + if (highlighted > mainY-3) + highlighted=mainY-3; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + } + } + break; + case KEY_DOWN: + + if (isFile(dir)) { + numNewLines = 0; + eofFound = false; + ptrFile = fopen(dir,"r"); + for (int i=0;i<(rowCounter-1);i++) { + fgets(line,MAX_LINE_LENGTH,ptrFile); + } + + while (true) { + tempChar = fgetc(ptrFile); + if (tempChar == '\n') { + numNewLines++; + } + if (tempChar == EOF) { + eofFound = true; + break; + } + if (numNewLines >= (mainY-5)) { + break; + } + } + if (eofFound) { + break; + } + fclose(ptrFile); + rowCounter++; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + break; + } + + + if (highlighted > mainY-3) { + break; + } + + if (fileCountNum < mainY-1 && highlighted+numDowns+1 >= fileCountNum) { + break; + } + + + highlighted++; + /* + if (highlighted+numDowns >= fileCountNum) { + if (numCharsToPrint-1 < mainY-2) { + highlighted = numCharsToPrint-1; + } else { + highlighted = mainY-2; + } + break; + } + */ + + + + /* + + if (highlighted > mainY-2) { + if (firstItemIndex < fileCountNum-1) { + firstItemIndex++; + highlighted--; + numDowns++; + addFiles(dir,(char **)fileList,firstItemIndex,mainY+5); + + wclear(mainwin); + refresh(); + wrefresh(mainwin); + } + } + + + + + */ + break; + + case KEY_NPAGE: + if (isFile(dir)) { + numNewLines = 0; + eofFound = false; + ptrFile = fopen(dir,"r"); + for (int i=0;i<(rowCounter-1);i++) { + fgets(line,MAX_LINE_LENGTH,ptrFile); + } + + while (true) { + tempChar = fgetc(ptrFile); + if (tempChar == '\n') { + numNewLines++; + } + if (tempChar == EOF) { + eofFound = true; + break; + } + if (numNewLines >= (mainY-5)) { + break; + } + } + if (eofFound) { + break; + } + fclose(ptrFile); + rowCounter += mainY-5; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + break; + } + + + + + + if (strcmp("linux",getenv("TERM")) == 0) { + mainY+=1; + } + + if (fileCountNum - firstItemIndex > mainY-3) { + firstItemIndex++; + numDowns++; + addFiles(dir,(char **)fileList,firstItemIndex,mainY+5); + wclear(mainwin); + refresh(); + wrefresh(mainwin); + } + + if (strcmp("linux",getenv("TERM")) == 0) { + mainY-=1; + } + + break; + + case KEY_PPAGE: + + + if (isFile(dir)) { + if (rowCounter-mainY+5 <= 1) { + rowCounter = 1; + break; + } + + rowCounter -= mainY+5; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + break; + } + + + if (firstItemIndex > 0) { + firstItemIndex--; + + addFiles(dir,(char **)fileList,firstItemIndex,mainY+5); + + if (highlighted > firstItemIndex+mainY-3) + highlighted=firstItemIndex+mainY-3; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + } + break; + +/* *************************************************************************************** */ +// STUFF TO IMPLEMENT +// +// 1. Show the content of files in another ncurses window +//////////// 2. Use Arrow Keys to scroll in file (line-by-line) +//////////// 3. Define symbolic constants for commonly used values +/* *************************************************************************************** */ + + + + case KEY_RIGHT: + case 10: + if (!(isFile(dir))) { + strcat(dir,*(fileList+highlighted)); + if (!(isFile(dir))) { + strcat(dir,"/"); + } + move(0,1); + printw("%s",dir); + fileCountNum = findFileCount(dir); + firstItemIndex=0; + addFiles(dir,(char **)fileList,firstItemIndex,mainY+5); + numDowns=0; + + highlighted=0; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + } + break; + + case KEY_LEFT: + removeDirFromPath(dir); + highlighted=0; + move(0,0); + wclrtoeol(stdscr); + refresh(); + wrefresh(mainwin); + mvwprintw(stdscr,0,1,"%s",dir); + fileCountNum = findFileCount(dir); + addFiles(dir,(char **)fileList,0,mainY+5); + numDowns=0; + firstItemIndex=0; + highlighted=0; + wclear(mainwin); + refresh(); + wrefresh(mainwin); + + hasChosen=false; + + rowCounter=1; + break; + + case ('n' & 0x1f): +// WINDOW* newFileWin = newwin(6,50,mainY/2-3,mainX/2-25); +// mvwaddstr(newFileWin,1,1,"Enter the name of the file you wish to create"); + char* newFileLoc = getSaveLoc(mainX,mainY,"$$$"); + char* newSaveLoc = malloc(sizeof(char) * (strlen(dir) + strlen(newFileLoc)) + 1); + strcpy(newSaveLoc,dir); +// strcat(newSaveLoc,"/"); + strcat(newSaveLoc,newFileLoc); + curs_set(2); + run(newSaveLoc); + curs_set(0); + refresh(); + wmove(mainwin,2,0); + wclrtoeol(mainwin); + wrefresh(mainwin); + break; + + case ('p' & 0x1f): + char* tempFileDir = malloc(sizeof(char) * MAX_LINE_LENGTH); + strcpy(tempFileDir,dir); +// strcat(tempFileDir,"/"); + strcat(tempFileDir,fileList[highlighted]); + fileCopied = malloc(sizeof(char) * strlen(fileList[highlighted]) * 2); + strcpy(fileCopied,fileList[highlighted]); + ptrFile = fopen(tempFileDir,"r"); + tempFileCopy = fopen("/tmp/tedflexTempFile","w"); + char tempChar = 'a'; + while(!feof(ptrFile)) { + fscanf(ptrFile,"%c",&tempChar); + if (!feof(ptrFile)) { + fprintf(tempFileCopy,"%c",tempChar); + } + } + hasCopied = true; + fclose(tempFileCopy); + fclose(ptrFile); +// rewind(ptrFile); + break; + + case ('v' & 0x1f): + if (hasCopied) { + char* tempFileDir = malloc(sizeof(char) * MAX_LINE_LENGTH); + strcpy(tempFileDir,dir); + tempFileCopy = fopen("/tmp/tedflexTempFile","r"); + char* tempFileCopied = malloc((strlen(tempFileDir)+strlen(fileCopied))*2); + strcpy(tempFileCopied,tempFileDir); + strcat(tempFileCopied,fileCopied); + int fd = open(tempFileCopied,O_CREAT | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR); + if (errno != EEXIST) { + strcat(tempFileDir,fileCopied); + ptrFile = fopen(tempFileDir,"w"); + char tempChar = 'a'; + while(!feof(tempFileCopy)) { + fscanf(tempFileCopy,"%c",&tempChar); + if (!feof(tempFileCopy)) { + fprintf(ptrFile,"%c",tempChar); + } + } + hasCopied=0; + free(fileCopied); + } + + + } + fileCountNum = findFileCount(dir); + addFiles(dir,(char **)fileList,0,mainY+5); + wclear(mainwin); + refresh(); + wrefresh(mainwin); + + + default: + break; + + } + } + + if (choice == 113){ + endwin(); + break; + } + } + + + return 0; +} + + + + + + + + + diff --git a/sample1.txt b/sample1.txt new file mode 100644 index 0000000..6fac5ba --- /dev/null +++ b/sample1.txt @@ -0,0 +1,4 @@ +aqbababa +baba +abaaaa +ababa diff --git a/test1.c b/test1.c new file mode 100644 index 0000000..3817298 --- /dev/null +++ b/test1.c @@ -0,0 +1,5 @@ +#include + +int main() { +*(int*) 0 = 0; +} diff --git a/texted.c b/texted.c new file mode 100644 index 0000000..97152fb --- /dev/null +++ b/texted.c @@ -0,0 +1,683 @@ +#include "texted.h" +#define MAX_SIZE 1048576 + +/* ********************************************************************************************************************************************** */ + +// STUFF TO IMPLEMENT + +// ******DONE****** 1. Characters should be removed from array after backspace (maybe keep track of which element in the array the cursor is on. +// ******DONE****** 2. Enter should add newline character to array. +// ******DONE****** 3. Backspace and Left Arrow at first character in row should move up to the last character on the previous row. (vice versa for right arrow) +// ********(PAGE UP AND) PAGE DOWN SOMETIMES DOESN'T MOVE REAL CURSOR ACCORDINGLY - TEXTCOUNTER SHOULD BE ADJUSTED******** 4. Page scrolling up/down (initially with page up/page down keys). - Idea: Implement by printing from nth new line character till first null character. +// 5. (OPTIONAL): Convert the mainwin into a pad, to make scrolling easier. +// **KIND OF DONE** 6. Word wrapping. +// ******DONE****** 7. Getting Save File Location from argv +// ******DONE?***** 8. Up Arrow and Down Arrow Keys. +// ******DONE****** 9. Integrate with file manager program. +// ******DONE****** 10. Opening and editing text files +// **MOSTLY DONE*** 11. Save Dialog. +// ******DONE****** 12. Insert character into array after pressing left arrow and typing a character. + +// SOLVE ERROR WITH LAST LINE IN A FILE (IT DOESN'T HAVE MAINX CHARACTERS AND IT DOESN'T HAVE TWO NEWLINES) + +// ******DONE****** 13. Backspace at first character ignores spaces. +// ******DONE?***** 14. Adding character at last line of window. +// ******DONE?***** 15. Page down/Page up with word wrapping. + + +/* ********************************************************************************************************************************************** */ + + + + +int run(char* dir) { + char* text; + char* saveLocation; + char* tempSaveLoc; + int textCounter; + int selRow=1; +// int numCharsCurrent; +// int numCharsNext; + int selCol=1; + int xSize,ySize,mainX,mainY; + int charArrayCounter; + FILE* myFile; + int ch; + int numLines; + bool newLineFound; +// int textIndex; + char* shortText; + int upArrowDistance; + int charsOnLine; + + + text = malloc(MAX_SIZE * (sizeof(char))); + + initscr(); + noecho(); +// cbreak(); + raw(); + getmaxyx(stdscr,ySize,xSize); + +// mvwaddstr(stdscr,2,5,"^S - Save | ^C - Quit | ^R - Reset Cursor"); + + + mvwaddstr(stdscr,2,5,"^S - Save | ^C - Quit | - Scroll Down | - Scroll Up"); + + + +// WINDOW* boxwin = newwin(ySize-7,xSize-8,5,5); + WINDOW* mainwin = newwin(ySize-7,xSize-9,5,5); + keypad(mainwin,true); +// box(boxwin,0,0); + getmaxyx(mainwin,mainY,mainX); + refresh(); + wrefresh(mainwin); +// wrefresh(boxwin); + wmove(mainwin,selRow,selCol); +// ch = wgetch(mainwin); + ch = ' '; + textCounter=0; + numLines=0; + int numNewLinesFound=0; + int indexToBegin=0; + FILE* readFile; + int tempSelRow,tempSelCol; + +// if (argc > 1) { + if (readFile = fopen(dir,"r")) { + fread(text,MAX_SIZE,1,readFile); + printFromIndex(mainwin,text,0,1,1,0); +// } else { +// strcpy(argv[1],tempSaveLoc); + } +// } + wmove(mainwin,selRow,selCol); + + while (ch != ('s' & 0x1f) && (ch != ('c' & 0x1f))) { + indexToBegin=0; + numNewLinesFound=0; + charsOnLine=0; + if (numLines > 0) { + for (int i=0;i=mainX-2){ + numNewLinesFound++; + charsOnLine=0; + } + } + } + // if (indexToBegin!=0){ + printFromIndex(mainwin,text,indexToBegin,selRow,selCol,numLines); + // } +// box(mainwin,0,0); + + + + // getyx(mainwin,selRow,selCol); + wmove(stdscr,1,1); + wclrtoeol(stdscr); + mvwprintw(stdscr,1,1,"%d,%d",selRow,selCol); + + mvwprintw(stdscr,3,1,"%c",findCharInArray(text,2,1,numLines)); + + wmove(mainwin,selRow,selCol); + + ch = wgetch(mainwin); + switch (ch) { + case 7: + case KEY_BACKSPACE: + case 8: + case 127: + if (selCol > 1) { + wmove(mainwin,selRow,--selCol); + wdelch(mainwin); + textCounter--; + shiftElementsDown(text,textCounter); + + } else if (selCol == 1 && selRow > 1) { + textCounter--; + --selRow; + selCol=findLastChar(mainwin,selRow,mainX)+charToNewLine(text,textCounter); + wmove(mainwin,selRow,selCol); + shiftElementsDown(text,textCounter); + + } + break; + + case KEY_LEFT: + if (selCol > 1) { + wmove(mainwin,selRow,--selCol); + textCounter--; + } else if (selCol == 1 && selRow > 1) { + textCounter--; + --selRow; + selCol=findLastChar(mainwin,selRow,mainX)+charToNewLine(text,textCounter); + if (selCol==2) { + selCol--; +// *(int*)0=0; + } + wmove(mainwin,selRow,selCol); + } + + break; + + case KEY_RIGHT: +// if (!(isLastChar(mainwin,selRow,selCol,mainX))) { + + + if (*(text+textCounter) != '\0') { + if (*(text+textCounter) != '\n' && selCol mainX-3) { + textCounter--; + } + + textCounter += (findLastChar(mainwin,selRow,mainX)); + + +// textCounter += (findLastCharWithArray(mainwin,stdscr,selRow,mainX,text,textCounter,numLines)); +// selCol=1; + */ + + wmove(mainwin,++selRow,selCol); + } + break; + + case KEY_UP: + + if (selRow-1 > 0 && (selCol == 1 && (*(text+textCounter-(findLastChar(mainwin,selRow-1,mainX)))) != '\0')) { + + int selColOfNewLine = 500; + newLineFound=false; + upArrowDistance=0; + for (int i=1;i 1) { + saveLocation = getSaveLoc(mainX,mainY,dir); +// } else { +// saveLocation = getSaveLoc(mainX,mainY,"$$$"); +// } + if (saveLocation == "$$$") { + ch = ' '; + wmove(mainwin,selRow,selCol); + break; + } +// myFile = fopen("savedFile.txt","w"); + myFile = fopen(saveLocation,"w"); + mvwaddstr(stdscr,1,1,saveLocation); + charArrayCounter=0; + do { + fputc(*(text+charArrayCounter),myFile); + charArrayCounter++; +// } while (charArrayCounter < (strlen(shortText))); + } while (*(text+charArrayCounter) != '\0'); + + if (*(text+charArrayCounter) != '\n') { + fputc('\n',myFile); + } +// fputc('\0',myFile); + fclose(myFile); +// wgetch(mainwin); + break; + + case KEY_NPAGE: + if (selRow > 1) { + numLines++; + selRow--; + wmove(mainwin,selRow,selCol); + } else { + if (selCol == 1) { + numLines++; + + while (*(text+textCounter) != '\n' && selCol= mainY-2 && selCol == 1 && selRow > 1) { + textCounter -= (findLastChar(mainwin,selRow-1,mainX)); + selRow--; + } + if (numLines > 0){ + selCol=1; + wmove(mainwin,selRow,selCol); + numLines--; + selRow++; + } + // if (numLines == 0) { + // selRow++; + // } + break; + default: +// waddch(mainwin,ch); + mvwinsch(mainwin,selRow,selCol,ch); + if (*(text+textCounter+1-1) != '\0') { + shiftElementsUp(text,textCounter); + } + *(text + textCounter) = ch; + textCounter++; + + + if (selCol >= mainX-1) { + selCol=2; + selRow++; + wmove(mainwin,selRow,selCol); + } else { + + + // selCol++; + +// /*Temporary uncomment*/ +// selCol++; + // wmove(mainwin,selRow,selCol+1); + + wmove(mainwin,selRow,++selCol); + + } + +// mvwprintw(stdscr,1,2,"%d",*(text+textCounter-1)); + + +// mvwprintw(mainwin,6,6,"%d",ch); + break; + + } +// mvwprintw(mainwin,3,3,"%d",textCounter); +// box(boxwin,0,0); + } + + endwin(); + + +} + + + +char* shortenString (char* origString) { + int numNullChars = 0; + char* newString = malloc((strlen(origString)+1) * (sizeof(char))); +// for (int i=0;i=arrCounter;i--) { + *(arr+i+1) = *(arr+i); + } + *(arr+arrCounter) = '\0'; + + + +} + +char* getSaveLoc(int mainX,int mainY, char* tempSaveLoc) { + WINDOW* saveDiag = newwin(8,40,((mainY/2)-4),((mainX/2)-20)); + char* saveLoc = malloc(SAVE_LOC_SIZE * sizeof(char)); + box(saveDiag,0,0); + refresh(); + wrefresh(saveDiag); + mvwaddstr(saveDiag,1,1,"Save to?"); + mvwhline(saveDiag,4,2,0,36); + wmove(saveDiag,3,2); + int i=0; + char ch = 65; + raw(); + int saveY,saveX; +// cbreak(); + if (tempSaveLoc != "$$$") { + do { + wmove(saveDiag,3,i+2); + + waddch(saveDiag,*(tempSaveLoc+i)); + *(saveLoc+i) = *(tempSaveLoc+i); + i++; + } while (i 2) { + wmove(saveDiag,3,i+1); +// wdelch(saveDiag); + waddch(saveDiag,32); + wmove(saveDiag,3,i+1); + *(saveLoc+i-1) = '\0'; + i--; + } + } else { + wmove(saveDiag,3,i+2); + waddch(saveDiag,ch); + *(saveLoc+i) = ch; + i++; + } + refresh(); + wrefresh(saveDiag); + } while (ch != 10 && ch != ('c' & 0x1f)); +// raw(); + wclear(saveDiag); + refresh(); + wrefresh(saveDiag); +// return shortenString(saveLoc); + if (ch == ('c' & 0x1f)) { + return "$$$"; + } + return saveLoc; + + + +} + + +int findLastChar(WINDOW* mainwin,int selRow,int mainX) { + int selCol=0; + for (int i=mainX-1;i>=1;i--){ + if (mvwinch(mainwin,selRow,i) != 32) { + selCol=i; + break; + } + } + + if (selCol==0){ + return selCol; + } + + return selCol+1; +} + +int findLastCharWithArray(WINDOW* mainwin,WINDOW* stdscr,int selRow,int mainX,char* text, int textCounter,int numLines) { + int selCol=0; + for (int i=0;i>mainX;i++){ +// if (findCharInArray(text,selRow,i+1,numLines) == mvwinch(mainwin,selRow+1,1)) { + + mvwprintw(stdscr,3,1,"%c\n",findCharInArray(text,selRow,i+1,numLines)); + + if (findCharInArray(text,selRow,i+1,numLines) == '\n') { + + abort(); + selCol=i; +// *(int*)0 = 0; + break; + } + } + return selCol+1; +} + + +bool isLastChar(WINDOW* mainwin,int selRow,int selCol,int mainX) { + for (int i=selCol;i= mainX-1) { + selColTemp=1; + selRowTemp++; +// index++; + wmove(mainwin,selRowTemp,selColTemp); + } + index++; + } + + +// box(boxwin,0,0); + refresh(); +// wrefresh(boxwin); +// wmove(mainwin,selRow,selCol); + wmove(mainwin,numLines,selCol); + if (numLines == 0) { + wmove(mainwin,++selRow,selCol); + } +} + + +char findCharInArray(char* charArray, int selRow, int selCol, int numLines) { + int indexToBegin=0; + int numNewLinesFound=0; + if (numLines > 0) { + for (int i=0;i + #include + #include + + #define SAVE_LOC_SIZE 500 + + char* shortenString(char*); + void shiftElementsDown(char*,int); + void shiftElementsUp(char*,int); + char* getSaveLoc(int,int,char*); + int findLastChar(WINDOW*,int,int); + int findLastCharWithArray(WINDOW*,WINDOW*,int,int,char*,int,int); + bool isLastChar(WINDOW*,int,int,int); + void printFromIndex(WINDOW*,char*,int,int,int,int); + char findCharInArray(char*,int,int,int); + int charToNewLine(char*,int); + int run(char*); + +#endif