start adding modes
This commit is contained in:
1 parent
acb4fb33eb
commit
8b797dcf0a
4 files changed
+239
-52
No files matched your search
@@ -6,15 +6,12 @@
|
||||
#include <unistd.h>
|
||||
#include "openFile.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char* path;
|
||||
if (argc > 1) {
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *path = "";
|
||||
if (argc > 1)
|
||||
{
|
||||
path = argv[1];
|
||||
} else {
|
||||
path = "";
|
||||
}
|
||||
int resp = openFile(path, 1);
|
||||
if (resp != 0) {
|
||||
return resp;
|
||||
}
|
||||
return openFile(path);
|
||||
}
|
||||
+177
-12
@@ -7,43 +7,208 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int openFile(char* path, int mode) {
|
||||
char* content = "";
|
||||
|
||||
if (strlen(path) != 0) {
|
||||
struct lineAndCharNum
|
||||
{
|
||||
int lineNum;
|
||||
int charNum;
|
||||
};
|
||||
|
||||
struct lineAndCharNum getLineAndCharNum(char *content)
|
||||
{
|
||||
int numLines = 1;
|
||||
int numChars = strlen(content);
|
||||
for (int i = 0; i < strlen(content); i++)
|
||||
{
|
||||
if (content[i] == '\n')
|
||||
{
|
||||
numLines++;
|
||||
}
|
||||
}
|
||||
struct lineAndCharNum lineAndCharNum = {numLines, numChars};
|
||||
return lineAndCharNum;
|
||||
}
|
||||
|
||||
int lineLength(char *content, int cursor_y)
|
||||
{
|
||||
int numLines = 0;
|
||||
int numChars = strlen(content);
|
||||
int across = 0;
|
||||
for (int i = 0; i < strlen(content); i++)
|
||||
{
|
||||
if (content[i] == '\n')
|
||||
{
|
||||
if (numLines == cursor_y)
|
||||
{
|
||||
return across;
|
||||
}
|
||||
numLines++;
|
||||
across = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
across++;
|
||||
}
|
||||
}
|
||||
return across;
|
||||
}
|
||||
|
||||
int openFile(char *path)
|
||||
{
|
||||
ESCDELAY = 0;
|
||||
char *content = malloc(1);
|
||||
|
||||
if (strlen(path) != 0)
|
||||
{
|
||||
FILE *fptr;
|
||||
fptr = fopen(path, "r");
|
||||
if(fptr != NULL) {
|
||||
if (fptr != NULL)
|
||||
{
|
||||
// Read the contents of the file into the content variable
|
||||
fseek(fptr, 0, SEEK_END);
|
||||
long fsize = ftell(fptr);
|
||||
fseek(fptr, 0, SEEK_SET);
|
||||
free(content);
|
||||
content = malloc(fsize + 1);
|
||||
fread(content, 1, fsize, fptr);
|
||||
content[fsize] = 0;
|
||||
fclose(fptr);
|
||||
}
|
||||
}
|
||||
int mode = 0;
|
||||
|
||||
struct FileData fileData = {path, content, 0, 0, 0};
|
||||
|
||||
struct FileData fileData = {&path, &content, &mode, 0, 0, 0, 0, 0};
|
||||
|
||||
initscr();
|
||||
cbreak();
|
||||
keypad(stdscr, TRUE);
|
||||
|
||||
|
||||
struct lineAndCharNum lineAndCharNum = getLineAndCharNum(content);
|
||||
fileData.numLines = lineAndCharNum.lineNum;
|
||||
fileData.numChars = lineAndCharNum.charNum;
|
||||
render(fileData);
|
||||
|
||||
int across = 0;
|
||||
|
||||
int choice;
|
||||
while (1) {
|
||||
int cursorLineLength;
|
||||
int running = 1;
|
||||
while (1)
|
||||
{
|
||||
choice = getch(); // Get user input
|
||||
if (mode != 2)
|
||||
{
|
||||
switch (choice)
|
||||
{
|
||||
case KEY_UP:
|
||||
if (fileData.cursor_y > 0)
|
||||
{
|
||||
fileData.cursor_y--;
|
||||
if (fileData.cursor_y < fileData.scroll)
|
||||
{
|
||||
fileData.scroll--;
|
||||
}
|
||||
int cursorLineLength = lineLength(content, fileData.cursor_y);
|
||||
fileData.cursor_x = across;
|
||||
if (fileData.cursor_x > cursorLineLength)
|
||||
{
|
||||
fileData.cursor_x = cursorLineLength;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
if (fileData.cursor_y < fileData.numLines - 1)
|
||||
{
|
||||
fileData.cursor_y++;
|
||||
if (fileData.cursor_y - (LINES - 4) == fileData.scroll)
|
||||
{
|
||||
fileData.scroll++;
|
||||
}
|
||||
int cursorLineLength = lineLength(content, fileData.cursor_y);
|
||||
fileData.cursor_x = across;
|
||||
if (fileData.cursor_x > cursorLineLength)
|
||||
{
|
||||
fileData.cursor_x = cursorLineLength;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
if (across > 0)
|
||||
{
|
||||
across--;
|
||||
}
|
||||
cursorLineLength = lineLength(content, fileData.cursor_y);
|
||||
fileData.cursor_x = across;
|
||||
if (fileData.cursor_x > cursorLineLength)
|
||||
{
|
||||
fileData.cursor_x = cursorLineLength;
|
||||
}
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
int lineWidth = lineLength(content, fileData.cursor_y);
|
||||
if (across < lineWidth)
|
||||
{
|
||||
across++;
|
||||
}
|
||||
cursorLineLength = lineLength(content, fileData.cursor_y);
|
||||
fileData.cursor_x = across;
|
||||
if (fileData.cursor_x > cursorLineLength)
|
||||
{
|
||||
fileData.cursor_x = cursorLineLength;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case 0:
|
||||
switch (choice)
|
||||
{
|
||||
case ':':
|
||||
mode = 2;
|
||||
break;
|
||||
case 'i':
|
||||
mode = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
switch (choice)
|
||||
{
|
||||
case 27:
|
||||
mode = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
switch (choice)
|
||||
{
|
||||
case 27:
|
||||
mode = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!running)
|
||||
{
|
||||
break;
|
||||
}
|
||||
lineAndCharNum = getLineAndCharNum(content);
|
||||
fileData.numLines = lineAndCharNum.lineNum;
|
||||
fileData.numChars = lineAndCharNum.charNum;
|
||||
render(fileData); // Re-render the menu
|
||||
}
|
||||
|
||||
end:
|
||||
free(content);
|
||||
// Clean up and exit ncurses
|
||||
endwin();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
+9
-5
@@ -3,15 +3,19 @@
|
||||
|
||||
#include <ncurses.h>
|
||||
|
||||
struct FileData {
|
||||
char* path;
|
||||
char* content;
|
||||
struct FileData
|
||||
{
|
||||
char **path;
|
||||
char **content;
|
||||
int *mode;
|
||||
int cursor_x;
|
||||
int cursor_y;
|
||||
int mode;
|
||||
int scroll;
|
||||
int numLines;
|
||||
int numChars;
|
||||
};
|
||||
|
||||
// Function to open a file
|
||||
int openFile(char* path, int mode);
|
||||
int openFile(char *path);
|
||||
|
||||
#endif // OPENFILE_H
|
||||
@@ -1,58 +1,79 @@
|
||||
#include "openFile.h"
|
||||
#include "config.h"
|
||||
|
||||
#include <ncurses.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void render(struct FileData fileData) {
|
||||
void render(struct FileData fileData)
|
||||
{
|
||||
char *content = *fileData.content;
|
||||
clear();
|
||||
|
||||
|
||||
// Display Header
|
||||
printw("%s\n", fileData.path);
|
||||
int screenWidth = getmaxx(stdscr);
|
||||
for (int i = 0; i < screenWidth; i++) {
|
||||
printw("%s - %s\n", programName, *fileData.path);
|
||||
for (int i = 0; i < COLS; i++)
|
||||
{
|
||||
printw("-");
|
||||
}
|
||||
|
||||
// Display content
|
||||
int widthOfLineNumText = 4;
|
||||
char *line = malloc(1);
|
||||
sprintf(line, "%d", fileData.numLines);
|
||||
int widthOfLineNumText = strlen(line) + 1;
|
||||
int contentBeginX = widthOfLineNumText + 3;
|
||||
int contentBeginY = 2;
|
||||
int contentHeight = LINES - 4;
|
||||
int contentWidth = COLS - contentBeginX - 1;
|
||||
|
||||
int LineNum = 1;
|
||||
int newLine = 1;
|
||||
for (int i = 0; i < strlen(fileData.content); i++) {
|
||||
if (newLine) {
|
||||
printw("%4d | ", LineNum);
|
||||
int length = strlen(content);
|
||||
for (int i = 0; (i < length+1)&&(LineNum <= (fileData.scroll+contentHeight+contentBeginY-1)); i++)
|
||||
{
|
||||
if (LineNum < fileData.scroll+1)
|
||||
{
|
||||
if (i<length && content[i] == '\n')
|
||||
{
|
||||
LineNum++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (newLine)
|
||||
{
|
||||
char* line = malloc(1);
|
||||
sprintf(line, "%d", LineNum);
|
||||
for (int i = 0; i < widthOfLineNumText - strlen(line); i++)
|
||||
{
|
||||
printw(" ");
|
||||
}
|
||||
printw("%s", line);
|
||||
printw(" | ", LineNum);
|
||||
LineNum++;
|
||||
newLine = 0;
|
||||
free(line);
|
||||
}
|
||||
if (fileData.content[i] == '\n') {
|
||||
if (i<length && content[i] == '\n')
|
||||
{
|
||||
newLine = 1;
|
||||
}
|
||||
printw("%c", fileData.content[i]);
|
||||
if (i<length)
|
||||
{
|
||||
printw("%c", content[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// footer
|
||||
move(LINES - 2, 0);
|
||||
for (int i = 0; i < screenWidth; i++) {
|
||||
for (int i = 0; i < COLS; i++)
|
||||
{
|
||||
printw("-");
|
||||
}
|
||||
int numLines = 0;
|
||||
int numChars = 0;
|
||||
for (int i = 0; i < strlen(fileData.content); i++) {
|
||||
if (fileData.content[i] == '\n') {
|
||||
numLines++;
|
||||
numChars = 0;
|
||||
} else {
|
||||
numChars++;
|
||||
}
|
||||
}
|
||||
printw("Lines: %d, Chars: %d, Cursor: %d, %d", numLines, numChars, fileData.cursor_x, fileData.cursor_y);
|
||||
|
||||
move(contentBeginY, contentBeginX);
|
||||
printw("Lines: %d, Chars: %d, Cursor: %d, %d, Mode: %d", fileData.numLines, fileData.numChars, fileData.cursor_x, fileData.cursor_y, *fileData.mode);
|
||||
|
||||
move(contentBeginY + fileData.cursor_y-fileData.scroll, contentBeginX + fileData.cursor_x);
|
||||
|
||||
refresh();
|
||||
free(line);
|
||||
}
|
||||
Reference in new issue
Block a user