add scroll across and make for loops more efficent

This commit is contained in:
William Bell committed 2024-04-30 14:03:47 +01:00
1 parent 3ee113dbc2
commit 707150fb58
3 files changed
+69 -22

No files matched your search

+5 -1
View File
@@ -1,6 +1,10 @@
full:
make clean
make build
build:
mkdir -p bin
gcc -o bin/main *.c -lncurses
gcc -o bin/mte *.c -lncurses
clean:
rm -rf bin
+32 -8
View File
@@ -19,15 +19,14 @@ struct lineAndCharNum getLineAndCharNum(char *content)
{
int numLines = 1;
int numChars = strlen(content);
for (int i = 0; i < strlen(content); i++)
for (int i = 0; i < numChars; i++)
{
if (content[i] == '\n')
{
numLines++;
}
}
struct lineAndCharNum lineAndCharNum = {numLines, numChars};
return lineAndCharNum;
return (struct lineAndCharNum){numLines, numChars};
}
int lineLength(char *content, int cursor_y)
@@ -35,7 +34,7 @@ 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++)
for (int i = 0; i < numChars; i++)
{
if (content[i] == '\n')
{
@@ -64,7 +63,7 @@ int cursorToCharPos(char *content, int cursor_x, int cursor_y)
int numChars = strlen(content);
int across = 0;
int i;
for (i = 0; i < strlen(content); i++)
for (i = 0; i < numChars; i++)
{
if (content[i] == '\n')
{
@@ -293,7 +292,7 @@ int openFile(char *path)
default:
if (isprint(choice))
{
sprintf(path, "%s, %02X", path, choice);
// sprintf(path, "%s, %02X", path, choice);
int cursorPos = cursorToCharPos(content, fileData.cursor_x, fileData.cursor_y);
char *newContent = malloc(strlen(content) + 2);
strncpy(newContent, content, cursorPos);
@@ -362,6 +361,31 @@ int openFile(char *path)
path = newPath;
}
break;
case 'l':
if (strlen(command) > 2)
{
char *line = malloc(strlen(command) - 2);
strncpy(line, command + 2, strlen(command) - 2);
line[strlen(command) - 2] = '\0';
int lineNum = atoi(line);
int numLines = 0;
int numChars = strlen(content);
int i;
for (i = 0; i < numChars; i++)
{
if (content[i] == '\n')
{
numLines++;
if (numLines == lineNum)
{
break;
}
}
}
fileData.cursor_y = numLines - 1;
fileData.cursor_x = 0;
}
break;
default:
break;
}
@@ -395,9 +419,9 @@ int openFile(char *path)
{
fileData.scroll = fileData.cursor_y;
}
if (fileData.cursor_y - (LINES - 4) == fileData.scroll)
if (fileData.cursor_y - (LINES - 4) >= fileData.scroll)
{
fileData.scroll++;
fileData.scroll = fileData.cursor_y - (LINES - 4) + 1;
}
lineAndCharNum = getLineAndCharNum(content);
fileData.numLines = lineAndCharNum.lineNum;
+32 -13
View File
@@ -19,14 +19,18 @@ void render(struct FileData fileData)
char *line = malloc(1);
sprintf(line, "%d", fileData.numLines);
int widthOfLineNumText = strlen(line) + 1;
int contentBeginX = widthOfLineNumText + 3;
if (widthOfLineNumText < 4)
{
widthOfLineNumText = 4;
}
int contentBeginX = widthOfLineNumText + 2;
int contentBeginY = 2;
int contentHeight = LINES - 4;
int contentWidth = COLS - contentBeginX - 1;
for (int i = 0; i < COLS; i++)
{
if (i == contentBeginX - 2)
if (i == contentBeginX - 1)
{
printw("╤");
}
@@ -42,6 +46,7 @@ void render(struct FileData fileData)
int newLine = 1;
int length = strlen(content);
int lineLength = 0;
int scroll_x = fileData.cursor_x / (contentWidth + 1);
for (int i = 0; (i < length + 1) && (LineNum <= (fileData.scroll + contentHeight + contentBeginY - 1)); i++)
{
if (LineNum < fileData.scroll + 1)
@@ -57,33 +62,47 @@ void render(struct FileData fileData)
char *line = malloc(1);
lineLength = 0;
sprintf(line, "%d", LineNum);
for (int i = 0; i < widthOfLineNumText - strlen(line); i++)
int linelen = strlen(line);
for (int i = 0; i < widthOfLineNumText - linelen; i++)
{
printw(" ");
}
printw("%s", line);
free(line);
printw(" │ ");
printw(" ");
LineNum++;
newLine = 0;
}
if (i < length && content[i] == '\n')
if ((i < length))
{
newLine = 1;
}
if ((i < length) && (lineLength-1 < contentWidth))
{
printw("%c", content[i]);
if (content[i] == '\n')
{
newLine = 1;
}
if ((lineLength >= scroll_x * contentWidth) && (lineLength < (contentWidth * (scroll_x + 1))))
{
printw("%c", content[i]);
}
else if (newLine)
{
printw("\n");
}
lineLength++;
}
}
// side line
for (int i = 0; i < contentHeight; i++)
{
move(i + contentBeginY, contentBeginX - 1);
printw("│");
}
// footer
move(LINES - 2, 0);
for (int i = 0; i < COLS; i++)
{
if (i == contentBeginX - 2)
if (i == contentBeginX - 1)
{
printw("╧");
}
@@ -111,7 +130,7 @@ void render(struct FileData fileData)
{
case 0:
case 1:
move(contentBeginY + fileData.cursor_y - fileData.scroll, contentBeginX + fileData.cursor_x);
move(contentBeginY + fileData.cursor_y - fileData.scroll, contentBeginX + (((fileData.cursor_x - 1) % (contentWidth)) + 1));
break;
case 2:
move(LINES - 1, 1 + strlen(*fileData.command));