Permalink
Browse files

stautus bars: add message bar

  • Loading branch information...
chenweigao committed Nov 19, 2018
1 parent 67f01dc commit 70f8034c0da2ca60dbc5748281dac9638d0a0c93
Showing with 55 additions and 10 deletions.
  1. +55 −10 ConsoleApplication4/kilo.c
@@ -4,15 +4,17 @@
#define _BSD_SOURCE
#define _GNU_SOURCE

#include<unistd.h>
#include<termios.h>
#include<stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include<stdio.h>
#include <stdio.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

/*** defines ***/
#define CTRL_KEY(k) ((k) & 0x1f) //bitwise-ANDs 00011111
@@ -43,14 +45,17 @@ typedef struct erow {

struct editorConfig
{
int cx, cy; // cursor position
int cx, cy; // cursor position, current line
int rx;
int rowoff;
int coloff;
int screenrows;
int screencols;
int numrows; // 0 or 1
erow *row;
char *filename;
char statusmsg[80];
time_t statusmsg_time;
struct termios orig_termios;
};

@@ -256,6 +261,9 @@ void editorAppendRow(char *s, size_t len) {
/*** file i/o ***/

void editorOpen(char *filename) {
free(E.filename);
E.filename = strdup(filename); //strdup: makes a copy of the given string

FILE *fp = fopen(filename, "r");
if (!fp) die("fopen");

@@ -365,13 +373,38 @@ void editorDrawRows(struct abuf *ab) {

void editorDrawStatusBar(struct abuf *ab) {
abAppend(ab, "\x1b[7m", 4);
int len = 0;
char status[80], rstatus[80];

int len = snprintf(status, sizeof(status), "%.20s - %d lines",
E.filename ? E.filename : "[No Name]", E.numrows);
int rlen = snprintf(rstatus, sizeof(rstatus), "%d/%d",
E.cy + 1, E.numrows);
if (len > E.screencols) len = E.screencols;
abAppend(ab, status, len);
while (len < E.screencols)
{
abAppend(ab, " ", 1);
len++;
if (E.screencols - len == rlen)
{
abAppend(ab, rstatus, rlen);
break;
}
else
{
abAppend(ab, " ", 1);
len++;
}
}
abAppend(ab, "\x1b[m", 3);
abAppend(ab, "\r\n", 2);
}

void editorDrawMessageBar(struct abuf *ab) {
abAppend(ab, "\x1b[K", 3);
int meglen = strlen(E.statusmsg);
if (meglen > E.screencols) meglen = E.screencols;
if (meglen && time(NULL) - E.statusmsg_time < 5) {
abAppend(ab, E.statusmsg, meglen);
}
}

void editorRefreshScreen() {
@@ -389,6 +422,7 @@ void editorRefreshScreen() {

editorDrawRows(&ab);
editorDrawStatusBar(&ab);
editorDrawMessageBar(&ab);

char buf[32];
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.rx - E.coloff) + 1);
@@ -403,6 +437,14 @@ void editorRefreshScreen() {
abFree(&ab);
}

void editorSetStatusMessage(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vsnprintf(E.statusmsg, sizeof(E.statusmsg), fmt, ap);
va_end(ap);
E.statusmsg_time = time(NULL);
}

/*** input ***/
void editorMoveCursor(int key) {
erow *row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
@@ -512,9 +554,10 @@ void initEditor() {
E.row = NULL;
E.rowoff = 0;
E.coloff = 0;

E.statusmsg[0] = '\0';
E.statusmsg_time = 0;
if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
E.screenrows -= 1; //a line for status bar
E.screenrows -= 2; //2 lines for status bar
}

int main(int argc, char *argv[]) {
@@ -526,6 +569,8 @@ int main(int argc, char *argv[]) {
editorOpen(argv[1]);
}

editorSetStatusMessage("HELP: Ctrl-Q = quit");

while (1)
{
editorRefreshScreen();

0 comments on commit 70f8034

Please sign in to comment.