scrollback struct, strip \r from reads, realloc scrollback
This commit is contained in:
		
							parent
							
								
									ced79ea7f0
								
							
						
					
					
						commit
						938e1db316
					
				
							
								
								
									
										72
									
								
								bt.c
									
									
									
									
									
								
							
							
						
						
									
										72
									
								
								bt.c
									
									
									
									
									
								
							| @ -10,13 +10,18 @@ | |||||||
| #include <string.h> | #include <string.h> | ||||||
| #include <raylib.h> | #include <raylib.h> | ||||||
| 
 | 
 | ||||||
|  | typedef struct scrollback { | ||||||
|  |     float height; | ||||||
|  |     float ypos; | ||||||
|  |     int capacity; | ||||||
|  |     int length; | ||||||
|  |     char *buf; | ||||||
|  | } scrollback; | ||||||
|  | 
 | ||||||
| // PTY
 | // PTY
 | ||||||
| int master; | int master; | ||||||
| int slave; | int slave; | ||||||
| 
 | 
 | ||||||
| // Scrollback
 |  | ||||||
| char *scrollback; |  | ||||||
| 
 |  | ||||||
| void spawn(void) { | void spawn(void) { | ||||||
|     openpty(&master, &slave, NULL, NULL, NULL); |     openpty(&master, &slave, NULL, NULL, NULL); | ||||||
|     pid_t p = fork(); |     pid_t p = fork(); | ||||||
| @ -52,7 +57,7 @@ Font *load_font() { | |||||||
|     return fontDefault; |     return fontDefault; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int read_pty(char *buf) { | int read_pty(scrollback *sb) { | ||||||
|     ssize_t nread; |     ssize_t nread; | ||||||
|     char readbuf[256]; |     char readbuf[256]; | ||||||
|     switch (nread = read(master, readbuf, sizeof(readbuf))) { |     switch (nread = read(master, readbuf, sizeof(readbuf))) { | ||||||
| @ -66,20 +71,33 @@ int read_pty(char *buf) { | |||||||
|         printf("EOF\n"); |         printf("EOF\n"); | ||||||
|         return 0; |         return 0; | ||||||
|     default: |     default: | ||||||
|  |         if (sb->length + nread > sb->capacity) { | ||||||
|  |             sb->capacity *= 2; | ||||||
|  |             sb->buf = realloc(sb->buf, sb->capacity); | ||||||
|  |         } | ||||||
|         if (readbuf[nread - 1] == '\n') { |         if (readbuf[nread - 1] == '\n') { | ||||||
|             printf("this\n"); |             printf("this\n"); | ||||||
|             nread--; |             nread--; | ||||||
|         } |         } | ||||||
|         readbuf[nread] = '\0'; |         int nreturns = 0; | ||||||
|         strcat(buf, readbuf); |         for (int i = 0; i < nread; i++) { | ||||||
|         printf("Amount of bytes read %lu\n", nread); |             if (readbuf[i] == '\r') { | ||||||
|         printf("FD: %i Buf %s\n", master, buf); |                 nreturns++; | ||||||
|         return nread; |                 for (int j = i; j < nread; j++) { | ||||||
|  |                     readbuf[j] = readbuf[j + 1]; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         readbuf[nread - nreturns] = '\0'; | ||||||
|  |         strcat(sb->buf, readbuf); | ||||||
|  |         return nread - nreturns; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int main(void) { | int main(void) { | ||||||
|     spawn(); |     spawn(); | ||||||
|  |   | ||||||
|  |     scrollback sb = { 0 }; | ||||||
| 
 | 
 | ||||||
|     const int screenWidth = 800; |     const int screenWidth = 800; | ||||||
|     const int screenHeight = 550; |     const int screenHeight = 550; | ||||||
| @ -89,10 +107,11 @@ int main(void) { | |||||||
|     SetTargetFPS(120); |     SetTargetFPS(120); | ||||||
| 
 | 
 | ||||||
|     Font *fontDefault = load_font(); |     Font *fontDefault = load_font(); | ||||||
|      | 
 | ||||||
|     scrollback = malloc(4098); |     sb.capacity = 2048; | ||||||
|     scrollback[0] = '\0'; |     sb.buf = malloc(sb.capacity); | ||||||
|     int sb_len = 0; |     sb.buf[0] = '\0'; | ||||||
|  |     sb.length = 0; | ||||||
|     char buf[128]; |     char buf[128]; | ||||||
|     int buf_len = 0; |     int buf_len = 0; | ||||||
|      |      | ||||||
| @ -103,6 +122,8 @@ int main(void) { | |||||||
|     FD_ZERO(&rset); |     FD_ZERO(&rset); | ||||||
|     FD_SET(master, &rset); |     FD_SET(master, &rset); | ||||||
| 
 | 
 | ||||||
|  |     sb.ypos = 0; | ||||||
|  | 
 | ||||||
|     while (!WindowShouldClose()) { |     while (!WindowShouldClose()) { | ||||||
|         int key = GetCharPressed(); |         int key = GetCharPressed(); | ||||||
|         fontsize += GetMouseWheelMove(); |         fontsize += GetMouseWheelMove(); | ||||||
| @ -111,15 +132,15 @@ int main(void) { | |||||||
|                 buf[buf_len] = (char)key; |                 buf[buf_len] = (char)key; | ||||||
|                 buf[buf_len + 1] = '\0'; |                 buf[buf_len + 1] = '\0'; | ||||||
|                 buf_len++; |                 buf_len++; | ||||||
|                 scrollback[sb_len] = (char)key; |                 sb.buf[sb.length] = (char)key; | ||||||
|                 scrollback[sb_len + 1] = '\0'; |                 sb.buf[sb.length + 1] = '\0'; | ||||||
|                 sb_len++; |                 sb.length++; | ||||||
|             } |             } | ||||||
|             key = GetCharPressed(); |             key = GetCharPressed(); | ||||||
|         } |         } | ||||||
|         if (IsKeyPressed(KEY_ENTER)) { |         if (IsKeyPressed(KEY_ENTER)) { | ||||||
|             sb_len -= buf_len; |             sb.length -= buf_len; | ||||||
|             scrollback[sb_len] = '\0'; |             sb.buf[sb.length] = '\0'; | ||||||
|             write(master, buf, buf_len); |             write(master, buf, buf_len); | ||||||
|             write(master, "\r", 1); |             write(master, "\r", 1); | ||||||
|             buf[0] = '\0'; |             buf[0] = '\0'; | ||||||
| @ -129,27 +150,26 @@ int main(void) { | |||||||
|             if (buf_len > 0) { |             if (buf_len > 0) { | ||||||
|                 buf_len--; |                 buf_len--; | ||||||
|                 buf[buf_len] = '\0'; |                 buf[buf_len] = '\0'; | ||||||
|                 sb_len--; |                 sb.length--; | ||||||
|                 scrollback[sb_len] = '\0'; |                 sb.buf[sb.length] = '\0'; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         // Drawing
 |         // Drawing
 | ||||||
|         BeginDrawing(); |         BeginDrawing(); | ||||||
|         /* update(buf, &buf_len, rset); */ |  | ||||||
|         { |         { | ||||||
|             int nread = read_pty(scrollback); |             int nread = read_pty(&sb); | ||||||
|             if (nread > 0) { |             if (nread > 0) { | ||||||
|                 /* printf("Amount of bytes read %i\n", nread); */ |                 sb.length += nread; | ||||||
|                 sb_len += nread; |                 new_read = true; | ||||||
|             } |             } | ||||||
|             ClearBackground(RAYWHITE); |             ClearBackground(RAYWHITE); | ||||||
|             DrawTextEx(*fontDefault, scrollback, (Vector2){ 10 , 10 }, fontsize, 1, BLACK); |             DrawTextEx(*fontDefault, sb.buf, (Vector2){ 0, sb.ypos }, fontsize, 1, BLACK); | ||||||
|         } |         } | ||||||
|         EndDrawing(); |         EndDrawing(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     free(sb.buf); | ||||||
|     close(master); |     close(master); | ||||||
|     CloseWindow(); |     CloseWindow(); | ||||||
|     return 0; |  | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user