First commit
commit
990d5dfbcc
@ -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;i<FILE_ARR_SIZE;i++) {
|
||||
|
||||
for (int i=0;i<global_arr_size;i++) {
|
||||
*(list+i) = '\0';
|
||||
}
|
||||
|
||||
DIR* d2;
|
||||
|
||||
|
||||
|
||||
// int fileNum = findFileCount(directory);
|
||||
|
||||
// int lengthOfArr = FILE_SIZE * sizeof(char *);
|
||||
|
||||
// list = realloc(list,lengthOfArr);
|
||||
|
||||
// int lengthOfArr = 2000;
|
||||
|
||||
// if (lengthOfArr < 200) {
|
||||
// printf("%d\n",lengthOfArr);
|
||||
// abort();
|
||||
// }
|
||||
|
||||
struct dirent* dir2;
|
||||
int count=0;
|
||||
// printw("bruh");
|
||||
d2 = opendir(directory);
|
||||
if (d2) {
|
||||
for (int i=0;i<start;i++) {
|
||||
dir2 = readdir(d2);
|
||||
}
|
||||
|
||||
while ((dir2 = readdir(d2)) != NULL && count<end) {
|
||||
if ((strcmp(dir2->d_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<strlen(*ptrPath);i++){
|
||||
if (*ptrPath[i] != '\0')
|
||||
*ptrPath[i] = '\0';
|
||||
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
void removeDirFromPath (char* ptrPath) {
|
||||
int slashLoc = 0;
|
||||
int counter ;
|
||||
if (isFile(ptrPath)) {
|
||||
counter=1;
|
||||
} else {
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
for (int i=strlen(ptrPath)-1;i>=0;i--) {
|
||||
if (counter==1 && *(ptrPath+i) == '/')
|
||||
slashLoc = i;
|
||||
if (*(ptrPath+i) == '/')
|
||||
counter++;
|
||||
}
|
||||
for (int i=slashLoc+1;i<strlen(ptrPath);i++){
|
||||
if (*(ptrPath+i) != '\0')
|
||||
*(ptrPath+i) = '\0';
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void addSlash(char* ptrPath) {
|
||||
for (int i=strlen(ptrPath)-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;
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
#ifndef FILEMAN_H_
|
||||
#define FILEMAN_H_
|
||||
#include <ncurses.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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
|
Binary file not shown.
@ -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 <locale.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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<mainY+START_WIN;i++) {
|
||||
mvwaddch(stdscr,i,mainX+START_WIN,32);
|
||||
mvwaddch(stdscr,i,mainX+START_WIN+1,32);
|
||||
}
|
||||
mvwaddch(stdscr,mainY+START_WIN,mainX+START_WIN+1,32); //Fills in an empty 'shadow' spot that was missed by the previous for loop.
|
||||
|
||||
for (int i=START_WIN+1;i<=mainX+START_WIN;i++) {
|
||||
mvwaddch(stdscr,mainY+START_WIN,i,32);
|
||||
}
|
||||
wattroff(stdscr,COLOR_PAIR(10));
|
||||
|
||||
/* BLOCK ENDS HERE */
|
||||
|
||||
char* dir = malloc(sizeof(char) * FILE_ARR_SIZE); //Char array for storing the name of the current file/directory
|
||||
char* tempDir = malloc(sizeof(char) * FILE_ARR_SIZE); //Char array for temporarily storing the name of the highlighted file/directory
|
||||
|
||||
|
||||
if (argv[1] == NULL) { //If no argument is provided for file path...
|
||||
dir[0]='/'; //Use root as a default file path
|
||||
dir[1]='\0';
|
||||
} else { //If there is a default path provided...
|
||||
dir = argv[1]; //Use that as the default path
|
||||
if (dir[0] == '.' || dir[0] != '/') {
|
||||
|
||||
|
||||
// DONE - ACCOUNT FOR THE POSSIBILITY THAT USER DIDN'T USE ./ BEFORE FILE NAME
|
||||
|
||||
|
||||
curDir = malloc(sizeof(char) * FILE_ARR_SIZE);
|
||||
getcwd(curDir,FILE_ARR_SIZE);
|
||||
int i=strlen(curDir);
|
||||
int a;
|
||||
if (dir[0] != '.') {
|
||||
a = 0;
|
||||
*(curDir+i) = '/';
|
||||
i++;
|
||||
} else {
|
||||
a = 1;
|
||||
}
|
||||
while (*(dir+a) != '\0') {
|
||||
// strcat(curDir,*(dir+i));
|
||||
*(curDir+i) = *(dir+a);
|
||||
i++;
|
||||
a++;
|
||||
}
|
||||
for (int i=0;i<FILE_ARR_SIZE;i++) {
|
||||
*(dir+i) = '\0';
|
||||
}
|
||||
strcpy(dir,curDir);
|
||||
}
|
||||
|
||||
|
||||
if (!(isFile(dir))) {
|
||||
addSlash(dir); //If it doesn't have a forward slash at the end, add one
|
||||
}
|
||||
}
|
||||
|
||||
fileCountNum = findFileCount(dir); //Set the number of files in the directory using a function
|
||||
|
||||
char** fileList = calloc(FILE_ARR_SIZE,1000); //Allocating memory for the fileList: I used calloc because the list is a double-pointer
|
||||
for (int i=0;i<FILE_ARR_SIZE;i++) { //Initializing the values of the list
|
||||
fileList[i] = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
Adds <mainY+5> 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,"<Up Arrow> - Move Up | <Down Arrow> - Move Down | <Left Arrow> - Go Back | <Right Arrow>/Enter - Enter Directory/Open File\n");
|
||||
wmove(stdscr,2,0);
|
||||
wclrtoeol(stdscr);
|
||||
mvwaddstr(stdscr,2,40,"<Page Up> - Scroll Up | <Page Down> - 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<numCharsToPrint;i++,a++) {
|
||||
|
||||
for (int i=0;i<FILE_ARR_SIZE;i++){
|
||||
*(tempDir+i) = '\0';
|
||||
}
|
||||
|
||||
|
||||
if (i == highlighted) {
|
||||
wattron(mainwin,A_REVERSE);
|
||||
}
|
||||
strcpy(tempDir,dir);
|
||||
strcat(tempDir, (char *)(*(fileList+i)));
|
||||
|
||||
strcat(tempDir,"\0");
|
||||
|
||||
if (isFile(tempDir)) {
|
||||
wattron(mainwin,COLOR_PAIR(4));
|
||||
}
|
||||
|
||||
|
||||
|
||||
mvwprintw(mainwin,a+1,5,"%s",*(i+fileList));
|
||||
refresh();
|
||||
wrefresh(mainwin);
|
||||
wattroff(mainwin,COLOR_PAIR(4));
|
||||
wattroff(mainwin,A_REVERSE);
|
||||
|
||||
}
|
||||
refresh();
|
||||
wrefresh(mainwin);
|
||||
}
|
||||
|
||||
mvwprintw(stdscr,0,1,"%s\n",dir);
|
||||
refresh();
|
||||
wrefresh(mainwin);
|
||||
//break;
|
||||
|
||||
if (hasExited) {
|
||||
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);
|
||||
firstItemIndex=0;
|
||||
highlighted=0;
|
||||
wclear(mainwin);
|
||||
refresh();
|
||||
wrefresh(mainwin);
|
||||
|
||||
hasChosen=false;
|
||||
|
||||
rowCounter=1;
|
||||
hasExited=false;
|
||||
|
||||
wmove(stdscr,1,0);
|
||||
wclrtoeol(stdscr);
|
||||
mvwaddstr(stdscr,1,17,"<Up Arrow> - Move Up | <Down Arrow> - Move Down | <Left Arrow> - Go Back | <Right Arrow>/Enter - Enter Directory/Open File\n");
|
||||
wmove(stdscr,2,0);
|
||||
wclrtoeol(stdscr);
|
||||
mvwaddstr(stdscr,2,40,"<Page Up> - Scroll Up | <Page Down> - 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -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 <locale.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#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<mainY+START_WIN;i++) {
|
||||
mvwaddch(stdscr,i,mainX+START_WIN,32);
|
||||
mvwaddch(stdscr,i,mainX+START_WIN+1,32);
|
||||
}
|
||||
mvwaddch(stdscr,mainY+START_WIN,mainX+START_WIN+1,32); //Fills in an empty 'shadow' spot that was missed by the previous for loop.
|
||||
|
||||
for (int i=START_WIN+1;i<=mainX+START_WIN;i++) {
|
||||
mvwaddch(stdscr,mainY+START_WIN,i,32);
|
||||
}
|
||||
wattroff(stdscr,COLOR_PAIR(10));
|
||||
|
||||
/* BLOCK ENDS HERE */
|
||||
|
||||
char* dir = malloc(sizeof(char) * FILE_ARR_SIZE); //Char array for storing the name of the current file/directory
|
||||
char* tempDir = malloc(sizeof(char) * FILE_ARR_SIZE); //Char array for temporarily storing the name of the highlighted file/directory
|
||||
|
||||
|
||||
if (argv[1] == NULL) { //If no argument is provided for file path...
|
||||
dir[0]='/'; //Use root as a default file path
|
||||
dir[1]='\0';
|
||||
} else { //If there is a default path provided...
|
||||
dir = argv[1]; //Use that as the default path
|
||||
if (dir[0] == '.' || dir[0] != '/') {
|
||||
|
||||
|
||||
// DONE - ACCOUNT FOR THE POSSIBILITY THAT USER DIDN'T USE ./ BEFORE FILE NAME
|
||||
|
||||
|
||||
curDir = malloc(sizeof(char) * FILE_ARR_SIZE);
|
||||
getcwd(curDir,FILE_ARR_SIZE);
|
||||
int i=strlen(curDir);
|
||||
int a;
|
||||
if (dir[0] != '.') {
|
||||
a = 0;
|
||||
*(curDir+i) = '/';
|
||||
i++;
|
||||
} else {
|
||||
a = 1;
|
||||
}
|
||||
while (*(dir+a) != '\0') {
|
||||
// strcat(curDir,*(dir+i));
|
||||
*(curDir+i) = *(dir+a);
|
||||
i++;
|
||||
a++;
|
||||
}
|
||||
for (int i=0;i<FILE_ARR_SIZE;i++) {
|
||||
*(dir+i) = '\0';
|
||||
}
|
||||
strcpy(dir,curDir);
|
||||
}
|
||||
|
||||
|
||||
if (!(isFile(dir))) {
|
||||
addSlash(dir); //If it doesn't have a forward slash at the end, add one
|
||||
}
|
||||
}
|
||||
|
||||
fileCountNum = findFileCount(dir); //Set the number of files in the directory using a function
|
||||
|
||||
char** fileList = calloc(FILE_ARR_SIZE,1000); //Allocating memory for the fileList: I used calloc because the list is a double-pointer
|
||||
for (int i=0;i<FILE_ARR_SIZE;i++) { //Initializing the values of the list
|
||||
fileList[i] = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
Adds <mainY+5> 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,"<Up Arrow> - Move Up | <Down Arrow> - Move Down | <Left Arrow> - Go Back | <Right Arrow>/Enter - Enter Directory/Open File\n");
|
||||
wmove(stdscr,2,0);
|
||||
wclrtoeol(stdscr);
|
||||
mvwaddstr(stdscr,2,40,"<Page Up> - Scroll Up | <Page Down> - 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<numCharsToPrint;i++,a++) {
|
||||
|
||||
for (int i=0;i<FILE_ARR_SIZE;i++){
|
||||
*(tempDir+i) = '\0';
|
||||
}
|
||||
|
||||
|
||||
if (i == highlighted) {
|
||||
wattron(mainwin,A_REVERSE);
|
||||
}
|
||||
strcpy(tempDir,dir);
|
||||
strcat(tempDir, (char *)(*(fileList+i)));
|
||||
|
||||
strcat(tempDir,"\0");
|
||||
|
||||
if (isFile(tempDir)) {
|
||||
wattron(mainwin,COLOR_PAIR(4));
|
||||
}
|
||||
|
||||
|
||||
|
||||
mvwprintw(mainwin,a+1,5,"%s",*(i+fileList));
|
||||
refresh();
|
||||
wrefresh(mainwin);
|
||||
wattroff(mainwin,COLOR_PAIR(4));
|
||||
wattroff(mainwin,A_REVERSE);
|
||||
|
||||
}
|
||||
refresh();
|
||||
wrefresh(mainwin);
|
||||
}
|
||||
|
||||
mvwprintw(stdscr,0,1,"%s\n",dir);
|
||||
refresh();
|
||||
wrefresh(mainwin);
|
||||
//break;
|
||||
|
||||
if (hasExited) {
|
||||
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);
|
||||
firstItemIndex=0;
|
||||
highlighted=0;
|
||||
wclear(mainwin);
|
||||
refresh();
|
||||
wrefresh(mainwin);
|
||||
|
||||
hasChosen=false;
|
||||
|
||||
rowCounter=1;
|
||||
hasExited=false;
|
||||
|
||||
wmove(stdscr,1,0);
|
||||
wclrtoeol(stdscr);
|
||||
mvwaddstr(stdscr,1,17,"<Up Arrow> - Move Up | <Down Arrow> - Move Down | <Left Arrow> - Go Back | <Right Arrow>/Enter - Enter Directory/Open File\n");
|
||||
wmove(stdscr,2,0);
|
||||
wclrtoeol(stdscr);
|
||||
mvwaddstr(stdscr,2,40,"<Page Up> - Scroll Up | <Page Down> - 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,4 @@
|
||||
aqbababa
|
||||
baba
|
||||
abaaaa
|
||||
ababa
|
@ -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 | <Page Down> - Scroll Down | <Page Up> - 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<strlen(text);i++) {
|
||||
if (numNewLinesFound==numLines) {
|
||||
indexToBegin=i;
|
||||
break;
|
||||
}
|
||||
if (*(text+i)=='\n') {
|
||||
numNewLinesFound++;
|
||||
charsOnLine=0;
|
||||
} else {
|
||||
charsOnLine++;
|
||||
}
|
||||
|
||||
if (charsOnLine>=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-2) {
|
||||
wmove(mainwin,selRow,++selCol);
|
||||
} else {
|
||||
selCol=1;
|
||||
wmove(mainwin,++selRow,selCol);
|
||||
}
|
||||
textCounter++;
|
||||
// mvwprintw(mainwin,6,6,"%d",mvwinch(mainwin,selRow,selCol));
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case KEY_HOME:
|
||||
textCounter-=selCol-1;
|
||||
selCol=1;
|
||||
wmove(mainwin,selRow,selCol);
|
||||
break;
|
||||
|
||||
|
||||
case KEY_DOWN:
|
||||
|
||||
if (selCol == 1 && (*(text+textCounter+(findLastChar(mainwin,selRow,mainX)))) != '\0' && selRow<mainY-2) {
|
||||
|
||||
|
||||
while (*(text+textCounter) != '\n' && selCol<mainX-2){
|
||||
textCounter++;
|
||||
selCol++;
|
||||
}
|
||||
textCounter++;
|
||||
selCol=1;
|
||||
|
||||
/*
|
||||
if (findLastChar(mainwin,selRow,mainX) > 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<mainX;i++) {
|
||||
// if (*(text+textCounter+1) == '\n') {
|
||||
// newLineFound=true;
|
||||
// }
|
||||
|
||||
if (i==mainX-1){
|
||||
upArrowDistance=mainX-1;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (textCounter==i) {
|
||||
upArrowDistance=textCounter+1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (i == findLastChar(mainwin,selRow-1,mainX)) {
|
||||
upArrowDistance = findLastChar(mainwin,selRow-1,mainX);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (*(text+textCounter-i) == '\n') {
|
||||
if (i<=2) {
|
||||
upArrowDistance=i;
|
||||
selColOfNewLine=i;
|
||||
} else {
|
||||
upArrowDistance=i-1;
|
||||
}
|
||||
if (newLineFound) {
|
||||
//if (findLastChar(mainwin,selRow-1,mainX) == 0) {
|
||||
// upArrowDistance=2;
|
||||
//}
|
||||
|
||||
break;
|
||||
}
|
||||
newLineFound=true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
textCounter-=upArrowDistance-1;
|
||||
|
||||
// textCounter -= (findLastChar(mainwin,selRow-1,mainX));
|
||||
// selCol=1;
|
||||
wmove(mainwin,--selRow,selCol);
|
||||
// *(int*)0=0;
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case 10:
|
||||
// wmove(mainwin,selRow+1,1);
|
||||
|
||||
|
||||
|
||||
if (selRow < mainY-2) {
|
||||
wmove(mainwin,++selRow,1);
|
||||
|
||||
|
||||
shiftElementsUp(text,textCounter);
|
||||
*(text+textCounter) = '\n';
|
||||
|
||||
|
||||
|
||||
textCounter++;
|
||||
// selCol=1;
|
||||
selCol=1;
|
||||
}
|
||||
break;
|
||||
|
||||
case KEY_DC:
|
||||
// abort();
|
||||
if (!(isLastChar(mainwin,selRow,selCol,mainX))) {
|
||||
wmove(mainwin,selRow,selCol+1);
|
||||
wdelch(mainwin);
|
||||
// textCounter--;
|
||||
shiftElementsDown(text,textCounter);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
// case (('r' & 0x1f)):
|
||||
// wmove(mainwin,selRow,selCol);
|
||||
// break;
|
||||
|
||||
|
||||
case (('s' & 0x1f)):
|
||||
// abort();
|
||||
// shortText = shortenString(text);
|
||||
// if (argc > 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<mainX-2){
|
||||
textCounter++;
|
||||
selCol++;
|
||||
}
|
||||
textCounter++;
|
||||
selCol=1;
|
||||
|
||||
|
||||
// //textCounter += (findLastChar(mainwin,selRow,mainX));
|
||||
wmove(mainwin,selRow,selCol);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case KEY_PPAGE:
|
||||
if (selRow >= 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<strlen(newString)+1;i++) {
|
||||
// (*newString+i) = (*origString+i);
|
||||
strncpy(newString,origString,strlen(newString)+1);
|
||||
// }
|
||||
return newString;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void shiftElementsDown(char* arr,int arrCounter){
|
||||
|
||||
// SHIFTS ELEMENTS LEFT
|
||||
|
||||
int lastElement = strlen(arr)-1;
|
||||
for (int i=arrCounter+1;i<strlen(arr)+10;i++){
|
||||
*(arr+i-1) = *(arr+i);
|
||||
}
|
||||
*(arr+lastElement)='\0';
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void shiftElementsUp(char* arr,int arrCounter) {
|
||||
|
||||
// SHIFTS ELEMENTS RIGHT
|
||||
|
||||
for (int i=strlen(arr)+2;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<strlen(tempSaveLoc));
|
||||
}
|
||||
|
||||
|
||||
|
||||
do {
|
||||
ch = wgetch(saveDiag);
|
||||
if (ch == 10) {
|
||||
break;
|
||||
}
|
||||
if (ch == ('c' & 0x1f)) {
|
||||
break;
|
||||
}
|
||||
getyx(saveDiag,saveY,saveX);
|
||||
if (ch == 127) {
|
||||
if (saveX > 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;i++) {
|
||||
if (mvwinch(mainwin,selRow,i) != 32) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void printFromIndex(WINDOW* mainwin,char* text,int index,int selRow,int selCol,int numLines) {
|
||||
wclear(mainwin);
|
||||
refresh();
|
||||
wrefresh(mainwin);
|
||||
int mainY,mainX;
|
||||
getmaxyx(mainwin,mainY,mainX);
|
||||
|
||||
// int index=indexToBegin;
|
||||
int selRowTemp=1;
|
||||
int selColTemp=1;
|
||||
while((*(text+index) != '\0') && (selRowTemp < mainY-1)) {
|
||||
mvwaddch(mainwin,selRowTemp,selColTemp,*(text+index));
|
||||
selColTemp++;
|
||||
if (*(text+index) == '\n') {
|
||||
selRowTemp++;
|
||||
selColTemp=1;
|
||||
}
|
||||
if (selColTemp >= 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<strlen(charArray);i++) {
|
||||
if (numNewLinesFound==numLines) {
|
||||
indexToBegin=i;
|
||||
break;
|
||||
}
|
||||
if (*(charArray+i)=='\n') {
|
||||
numNewLinesFound++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// *(int*)0 = 0;
|
||||
|
||||
|
||||
|
||||
selRow--;
|
||||
// selCol++;
|
||||
int index=indexToBegin;
|
||||
int curRow=0;
|
||||
while(curRow < selRow) {
|
||||
index++;
|
||||
if (*(charArray+index) == '\n') {
|
||||
curRow++;
|
||||
}
|
||||
if (selRow == curRow) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i=0;i<selCol;i++){
|
||||
index++;
|
||||
}
|
||||
|
||||
// return 'a';
|
||||
|
||||
return *(charArray+index);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
int charToNewLine(char* text,int textCounter) {
|
||||
int returnVal=0;
|
||||
while (*(text+textCounter) == 32 || *(text+textCounter) == '\n'){
|
||||
returnVal++;
|
||||
textCounter--;
|
||||
}
|
||||
return returnVal-1;
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
#ifndef TEXTED_H_
|
||||
#define TEXTED_H_
|
||||
#include <ncurses.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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
|
Loading…
Reference in New Issue