begin loading the file in read only mode
This commit is contained in:
1 parent
b3bce35448
commit
acb4fb33eb
10 files changed
+165
-53
No files matched your search
Vendored
+7
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"*.ejs": "html",
|
||||||
|
"ncurses.h": "c",
|
||||||
|
"stdio.h": "c"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef CONFIG_H
|
||||||
|
#define CONFIG_H
|
||||||
|
|
||||||
|
extern char programName[];
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,59 +1,20 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "openFile.h"
|
||||||
|
|
||||||
int main() {
|
int main(int argc, char *argv[]) {
|
||||||
struct termios oldt, newt;
|
char* path;
|
||||||
char ch;
|
if (argc > 1) {
|
||||||
|
path = argv[1];
|
||||||
// Get current terminal attributes
|
} else {
|
||||||
tcgetattr(STDIN_FILENO, &oldt);
|
path = "";
|
||||||
newt = oldt;
|
}
|
||||||
|
int resp = openFile(path, 1);
|
||||||
// Set terminal to raw mode (disable buffering and echoing)
|
if (resp != 0) {
|
||||||
newt.c_lflag &= ~(ICANON | ECHO);
|
return resp;
|
||||||
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
|
|
||||||
|
|
||||||
char lastCh[10];
|
|
||||||
int lastChSize = sizeof(lastCh);
|
|
||||||
int lastChIndex = lastChSize - 1;
|
|
||||||
|
|
||||||
// Listen for key presses
|
|
||||||
while (1) {
|
|
||||||
ch = getchar();
|
|
||||||
printf("Key pressed: %02X\n", ch);
|
|
||||||
|
|
||||||
switch (ch) {
|
|
||||||
case 0x41:
|
|
||||||
case 0x42:
|
|
||||||
case 0x43:
|
|
||||||
case 0x44:
|
|
||||||
for (int i = 0; i < lastChSize; i++){
|
|
||||||
printf("%02X ", lastCh[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
printf("%02X %02X\n", lastCh[(lastChIndex+(lastChSize-1)) % lastChSize], lastCh[lastChIndex]);
|
|
||||||
printf("%i %i\n", (lastChIndex+(lastChSize-1)) % lastChSize, lastChIndex);
|
|
||||||
if (lastCh[(lastChIndex+(lastChSize-1)) % lastChSize] == 0x1B && lastCh[lastChIndex] == 0x5B){
|
|
||||||
|
|
||||||
}
|
|
||||||
printf("Arrow key pressed %02X\n", ch);
|
|
||||||
break;
|
|
||||||
case 0x7F:
|
|
||||||
printf("Backspace key pressed\n");
|
|
||||||
break;
|
|
||||||
case 0x0A:
|
|
||||||
printf("Enter key pressed\n");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
lastChIndex = (lastChIndex + 1) % lastChSize;
|
|
||||||
lastCh[lastChIndex] = ch;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore terminal attributes
|
|
||||||
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
+49
@@ -0,0 +1,49 @@
|
|||||||
|
#include "openFile.h"
|
||||||
|
#include "config.h"
|
||||||
|
#include "render.h"
|
||||||
|
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int openFile(char* path, int mode) {
|
||||||
|
char* content = "";
|
||||||
|
|
||||||
|
if (strlen(path) != 0) {
|
||||||
|
FILE *fptr;
|
||||||
|
fptr = fopen(path, "r");
|
||||||
|
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);
|
||||||
|
content = malloc(fsize + 1);
|
||||||
|
fread(content, 1, fsize, fptr);
|
||||||
|
content[fsize] = 0;
|
||||||
|
fclose(fptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FileData fileData = {path, content, 0, 0, 0};
|
||||||
|
|
||||||
|
|
||||||
|
initscr();
|
||||||
|
cbreak();
|
||||||
|
keypad(stdscr, TRUE);
|
||||||
|
|
||||||
|
render(fileData);
|
||||||
|
|
||||||
|
int choice;
|
||||||
|
while (1) {
|
||||||
|
choice = getch(); // Get user input
|
||||||
|
render(fileData); // Re-render the menu
|
||||||
|
}
|
||||||
|
|
||||||
|
end:
|
||||||
|
free(content);
|
||||||
|
// Clean up and exit ncurses
|
||||||
|
endwin();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef OPENFILE_H
|
||||||
|
#define OPENFILE_H
|
||||||
|
|
||||||
|
#include <ncurses.h>
|
||||||
|
|
||||||
|
struct FileData {
|
||||||
|
char* path;
|
||||||
|
char* content;
|
||||||
|
int cursor_x;
|
||||||
|
int cursor_y;
|
||||||
|
int mode;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Function to open a file
|
||||||
|
int openFile(char* path, int mode);
|
||||||
|
|
||||||
|
#endif // OPENFILE_H
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
#include "openFile.h"
|
||||||
|
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void render(struct FileData fileData) {
|
||||||
|
clear();
|
||||||
|
|
||||||
|
// Display Header
|
||||||
|
printw("%s\n", fileData.path);
|
||||||
|
int screenWidth = getmaxx(stdscr);
|
||||||
|
for (int i = 0; i < screenWidth; i++) {
|
||||||
|
printw("-");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display content
|
||||||
|
int widthOfLineNumText = 4;
|
||||||
|
int contentBeginX = widthOfLineNumText + 3;
|
||||||
|
int contentBeginY = 2;
|
||||||
|
int contentHeight = LINES - 4;
|
||||||
|
|
||||||
|
int LineNum = 1;
|
||||||
|
int newLine = 1;
|
||||||
|
for (int i = 0; i < strlen(fileData.content); i++) {
|
||||||
|
if (newLine) {
|
||||||
|
printw("%4d | ", LineNum);
|
||||||
|
LineNum++;
|
||||||
|
newLine = 0;
|
||||||
|
}
|
||||||
|
if (fileData.content[i] == '\n') {
|
||||||
|
newLine = 1;
|
||||||
|
}
|
||||||
|
printw("%c", fileData.content[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// footer
|
||||||
|
move(LINES - 2, 0);
|
||||||
|
for (int i = 0; i < screenWidth; 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);
|
||||||
|
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
Reference in new issue
Block a user