summaryrefslogtreecommitdiffstats
path: root/nebu/video/console.c
blob: 40f2e121f3c934f86c8825c5cbc92d207b50e68d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "video/nebu_console.h"

#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#define CONSOLE_DEPTH 100
#define CONSOLE_WIDTH 80

static char buffer[CONSOLE_DEPTH][CONSOLE_WIDTH];
static int position;
static int offset;

void consoleInit() {
  int i;

  for(i = 0; i < CONSOLE_DEPTH; i++)
    buffer[i][0] = '\0';

  position = 0;
  offset = 0;
  // fprintf(stderr, "console initialized\n");
}

void consoleAddLine(char *text) {
  int i = 0, x=0;

  while(i < CONSOLE_WIDTH - 1 && text[i] != 0) {
    buffer[position][i] = text[i];
    i++;
  }
  buffer[position][i] = '\0';
/*  fprintf(stderr, "added \"%s\" to console at buffer[%i] with "
 *	  " offset of %i\n",
 *	  buffer[position], position, offset);
 */

   position++;
   
   /* reposition the buffer to avoid overruns - tim */
   if(position >= 99){
      for(i=0;i<CONSOLE_DEPTH;i++){
	 strcpy(buffer[x], buffer[i]);
	 buffer[i][0] = '\0';
	 ++x;
      }
      position -= 4;
   }
}

void consoleDisplay(void(*func)(char *line, int call), int height) {
  int i;
  int j = 0;
  for(i = 0; i < height; i++) {
    if(*(buffer[ (position + i - height - offset +
		CONSOLE_DEPTH) % CONSOLE_DEPTH ]) != 0)
      func(buffer[ (position + i - height - offset +
		    CONSOLE_DEPTH) % CONSOLE_DEPTH ], j++);
  }
}
  
void consoleScrollForward(int range) {
  offset -= range;
  if(offset < 0) offset = 0;
}

void consoleScrollBackward(int range) {
  offset += range;
  if(offset > CONSOLE_DEPTH - 1)
    offset = CONSOLE_DEPTH - 1;
}

/*
  displayMessage

  post a message to the console and/or stdout/stderr.

  NOTE: Don't put newlines at the end of the format string,
        the function will handle adding them when appropriate.
 */
void displayMessage(outloc_e where, const char *fmt_str, ...) {

  char message[CONSOLE_WIDTH];
  va_list ap;
  
  va_start(ap, fmt_str);
  
  if (where & TO_CONSOLE) {
    vsprintf(message, fmt_str, ap);
    consoleAddLine(message); 
  }

  if (where & TO_STDOUT) {
    vfprintf(stdout, fmt_str, ap);
    fputc('\n', stdout);
  }

  if (where & TO_STDERR) {
    vfprintf(stderr, fmt_str, ap);
    fputc('\n', stderr);
  }
}