Usermode edit - Can view :)
authorJohn Hodge <[email protected]>
Wed, 25 May 2011 14:25:29 +0000 (14:25 +0000)
committerJohn Hodge <[email protected]>
Wed, 25 May 2011 14:25:29 +0000 (14:25 +0000)
Usermode/Applications/edit_src/Makefile
Usermode/Applications/edit_src/main.c

index 05748b1..116c5d9 100644 (file)
@@ -7,6 +7,6 @@ BIN = edit
 
 -include ../Makefile.tpl
 
-edit.lin:
+edit.lin: main.c
        gcc main.c -o edit.lin -DUSE_LOCAL=1 -Wall
 
index eba9be6..20b11ae 100644 (file)
@@ -47,6 +47,7 @@ void  CursorRight(tFile *File);
 void   CursorLeft(tFile *File);\r
 \r
 void   Term_SetPos(int Row, int Col);\r
+void   Term_ScrollUp(int Count);\r
 \r
 // === GLOBALS ===\r
  int   giProgramState = 0;\r
@@ -55,6 +56,7 @@ tFile *gpCurrentFile;
 #if USE_LOCAL\r
 struct termios gOldTermios;\r
 #endif\r
+ int   giTabSize = 8;  // Someday this will be per-file (maybe)\r
  int   giTerminal_Width = 80;\r
  int   giTerminal_Height = 25;\r
 \r
@@ -223,6 +225,8 @@ void ShowLine(tFile *File, int LineNum, int X, int Y, int W)
        char    *line;\r
        \r
        line = File->LineBuffer[LineNum];\r
+       Term_SetPos(Y, X);\r
+       printf("\x1B[2K");      // Clear line\r
        printf("%6i  ", LineNum+1);\r
        j = 8;\r
        for( k = 0; j < W-1 && line[k]; j++, k++ )\r
@@ -303,51 +307,86 @@ void UpdateDisplayStatus(void)
        \r
        if( lastLine > gpCurrentFile->LineCount )\r
                lastLine = gpCurrentFile->LineCount;\r
-       \r
+\r
+       Term_SetPos( giTerminal_Height - 1, 0 );        \r
        printf("--- Line %i/%i (showing %i to %i)",\r
                gpCurrentFile->CurrentLine + 1, gpCurrentFile->LineCount,\r
-               gpCurrentFile->FirstLine, lastLine);\r
+               gpCurrentFile->FirstLine + 1, lastLine);\r
 }\r
 \r
-void CursorUp(tFile *File)\r
+void UpdateCursorPosition(void)\r
 {\r
-       if( File->CurrentLine > 0 )\r
+       tFile   *File = gpCurrentFile;\r
+        int    screen_x, i;\r
+       const char      *tmp;\r
+\r
+       // Determine the position (handling tab characters)\r
+       screen_x = 0;\r
+       tmp = File->LineBuffer[File->CurrentLine];\r
+       for( i = 0; tmp[i] && i < File->CurrentPos; i ++ )\r
        {\r
-               File->CurrentLine --;\r
-               if( File->FirstLine > File->CurrentLine )\r
+               switch(*tmp)\r
                {\r
-                       File->FirstLine = File->CurrentLine;\r
-                       UpdateDisplayFull();\r
-               }\r
-               else\r
-               {\r
-                       UpdateDisplayLine(File->CurrentLine + 1);\r
-                       UpdateDisplayLine(File->CurrentLine);\r
-                       UpdateDisplayStatus();\r
+               case '\t':      screen_x += giTabSize;  break;\r
+               default:        screen_x ++;    break;\r
                }\r
        }\r
+\r
+       Term_SetPos( File->CurrentLine - File->FirstLine, 8 + screen_x);\r
+}\r
+\r
+void CursorUp(tFile *File)\r
+{\r
+       if( File->CurrentLine <= 0 )\r
+               return ;\r
+       \r
+       File->CurrentLine --;\r
+       if( File->FirstLine > File->CurrentLine )\r
+       {\r
+               Term_ScrollUp(File->FirstLine - File->CurrentLine);\r
+               File->FirstLine = File->CurrentLine;\r
+//             UpdateDisplayLine(File->FirstLine + giTerminal_Height-2);\r
+               UpdateDisplayLine(File->FirstLine);\r
+//             UpdateDisplayFull();\r
+       }\r
+       else\r
+       {\r
+               UpdateDisplayLine(File->CurrentLine + 1);\r
+               UpdateDisplayLine(File->CurrentLine);\r
+       }\r
+       UpdateDisplayStatus();\r
+       \r
+       UpdateCursorPosition();\r
 }\r
 \r
 void CursorDown(tFile *File)\r
 {\r
-       if( File->CurrentLine+1 < File->LineCount )\r
+        int    threshold;\r
+       // Bounds check\r
+       if( File->CurrentLine + 1 >= File->LineCount )\r
+               return ;\r
+       \r
+       File->CurrentLine ++;\r
+       // Check if scroll is needed\r
+       threshold = File->CurrentLine - (giTerminal_Height-2); \r
+       if( File->FirstLine < threshold )\r
        {\r
-               File->CurrentLine ++;\r
-               if( File->FirstLine < File->CurrentLine - (giTerminal_Height-2) )\r
-               {\r
-                       File->FirstLine = File->CurrentLine - (giTerminal_Height-2);\r
-                       UpdateDisplayFull();\r
-               }\r
-               else\r
-               {\r
-                       UpdateDisplayLine(File->CurrentLine - 1);\r
-                       UpdateDisplayLine(File->CurrentLine);\r
-                       UpdateDisplayStatus();\r
-               }\r
+               Term_ScrollUp( File->FirstLine - threshold );\r
+               File->FirstLine = threshold;\r
+               UpdateDisplayLine(File->FirstLine + giTerminal_Height-2);\r
        }\r
+       else\r
+       {\r
+               // Else, just update the previous and new lines (and status)\r
+               UpdateDisplayLine(File->CurrentLine - 1);\r
+               UpdateDisplayLine(File->CurrentLine);\r
+       }\r
+       UpdateDisplayStatus();\r
+       \r
+       UpdateCursorPosition();\r
 }\r
 \r
-void CursorRight(tFile *File)\r
+void CursorLeft(tFile *File)\r
 {\r
        if( File->CurrentPos > 0 )\r
        {\r
@@ -355,16 +394,18 @@ void CursorRight(tFile *File)
                UpdateDisplayLine(File->CurrentLine);\r
                UpdateDisplayStatus();\r
        }\r
+       UpdateCursorPosition();\r
 }\r
 \r
-void CursorLeft(tFile *File)\r
+void CursorRight(tFile *File)\r
 {\r
        if( File->LineBuffer[File->CurrentLine][File->CurrentPos+1] )\r
        {\r
-               File->CurrentPos --;\r
+               File->CurrentPos ++;\r
                UpdateDisplayLine(File->CurrentLine);\r
                UpdateDisplayStatus();\r
        }\r
+       UpdateCursorPosition();\r
 }\r
 \r
 void Term_SetPos(int Row, int Col)\r
@@ -372,3 +413,14 @@ void Term_SetPos(int Row, int Col)
        printf("\x1B[%i;%iH", Row+1, Col+1);    // Set cursor\r
        fflush(stdout);\r
 }\r
+\r
+void Term_ScrollUp(int Count)\r
+{\r
+       printf("\x1B[r");\r
+//     printf("\x1B[1;%ir", giTerminal_Height-1);\r
+       if(Count < 0)\r
+               printf("\x1B[%iM", -Count);\r
+       else\r
+               printf("\x1B[%iL", Count);\r
+       fflush(stdout);\r
+}\r

UCC git Repository :: git.ucc.asn.au