srw / srw.c

Diff from to
 #define ESC         0x1b
 #define DEL         0x7f
 
+#define cursor_scp()  printf("\033[s")
+#define cursor_rcp()  printf("\033[u")
 #define cursor_clr()  printf("\033[J")
 #define cursor_beg()  printf("\033[1G")
+#define cursor_up(n)  printf("\033[%dA", MAX(1, (n)))
 #define cursor_fwd(n) printf("\033[%dC", MAX(1, (n)))
 #define cursor_org()  printf("\033[1;1H")
 
 static pid_t child;
 static struct winsize ws;
 static int winch = 0;
+static int incomplete = 0;
 
 static struct termios set, dfl;
 
 
 		if (FD_ISSET(mfd, &rd)) {
 			erase_usr();
-			for (rdlen = read(mfd, buf, sizeof(buf)); rdlen > 0; rdlen = read(mfd, buf, sizeof(buf)))
+			if (incomplete) {
+				cursor_rcp();
+				fflush(stdout);
+			}
+			for (rdlen = read(mfd, buf, sizeof(buf)); rdlen > 0; rdlen = read(mfd, buf, sizeof(buf))) {
 				my_write(STDOUT, buf, rdlen);
+				incomplete = buf[rdlen-1] != '\n';
+			}
 			if (rdlen < 0 && errno != EAGAIN)
 				warn("failed to read from fd %d", mfd);
+			if (incomplete) {
+				tcflag_t old = set.c_oflag;
+				set.c_oflag &= ~ONLCR;
+				tcsetattr(STDIN, TCSANOW, &set);
+				printf("\n");
+				cursor_up(1);
+				cursor_scp();
+				printf("\r\n");
+				fflush(stdout);
+				set.c_oflag = old;
+				tcsetattr(STDIN, TCSANOW, &set);
+			}
 			print_usr();
 		}
 	}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.