Browse files

wrote render_buffer(); untested

  • Loading branch information...
1 parent 8ec5f4e commit 003269bab01b1758e962bb62db55e65e8474839c ec429 committed Jan 2, 2011
Showing with 167 additions and 43 deletions.
  1. +2 −2 bits.c
  2. +5 −5 bits.h
  3. +130 −21 buffer.c
  4. +5 −2 buffer.h
  5. +8 −3 colour.c
  6. +5 −4 colour.h
  7. +8 −3 ttyesc.c
  8. +4 −3 ttyesc.h
View
4 bits.c
@@ -19,7 +19,7 @@ char * fgetl(FILE *fp)
return(lout);
}
-int wordline(char *msg, int x, char **out, colour lc)
+int wordline(const char *msg, int x, char **out, colour lc)
{
int tabx=x;
if((!tabx) || (tabx*2>width))
@@ -171,7 +171,7 @@ void append_char(char **buf, int *l, int *i, char c)
}
}
-void append_str(char **buf, int *l, int *i, char *str)
+void append_str(char **buf, int *l, int *i, const char *str)
{
while(*str) // not the most tremendously efficient implementation, but conceptually simple at least
{
View
10 bits.h
@@ -1,9 +1,9 @@
#pragma once
/*
- quIRC - simple terminal-based IRC client
- Copyright (C) 2010 Edward Cree
-
+ quIRC - simple terminal-based IRC client
+ Copyright (C) 2010 Edward Cree
+
See quirc.c for license information
bits: general helper functions
*/
@@ -24,10 +24,10 @@ char *strndup(const char *s, size_t size);
#endif
char * fgetl(FILE *); // gets a line of string data; returns a malloc-like pointer
-int wordline(char *msg, int x, char **out, colour lc); // prepares a string for printing, breaking lines in between words
+int wordline(const char *msg, int x, char **out, colour lc); // prepares a string for printing, breaking lines in between words
void init_char(char **buf, int *l, int *i); // initialises a string buffer in heap. *buf becomes a malloc-like pointer
void append_char(char **buf, int *l, int *i, char c); // adds a character to a string buffer in heap (and realloc()s if needed)
-void append_str(char **buf, int *l, int *i, char *str); // adds a string to a string buffer in heap (and realloc()s if needed)
+void append_str(char **buf, int *l, int *i, const char *str); // adds a string to a string buffer in heap (and realloc()s if needed)
void crush(char **buf, int len);
void scrush(char **buf, int len);
char *mktag(char *fmt, char *from);
View
151 buffer.c
@@ -238,7 +238,7 @@ int add_to_buffer(int buf, colour lc, char *lt)
bufs[buf].ptr=(bufs[buf].ptr+1)%bufs[buf].nlines;
if(bufs[buf].ptr==0)
bufs[buf].filled=true;
- bufs[buf].rendered=false; // mark tab dirty
+ bufs[buf].rendered=false; // mark tab dirty; TODO be more intelligent about avoiding full re-renders
bufs[buf].alert=true;
bufs[cbuf].alert=false;
return(0);
@@ -265,6 +265,7 @@ int redraw_buffer(void)
{
printf("%s", bufs[cbuf].lpt[l]);
}
+ printf(CLA "\n");
switch(bufs[cbuf].type)
{
case STATUS:
@@ -300,32 +301,140 @@ int redraw_buffer(void)
bufs[cbuf].alert=false;
return(0);
}
-/*
- int sl = ( bufs[cbuf].filled ? (bufs[cbuf].ptr+max(bufs[cbuf].nlines-(bufs[cbuf].scroll+height-3), 1))%bufs[cbuf].nlines : max(bufs[cbuf].ptr-(bufs[cbuf].scroll+height-3), 0) );
- int el = ( bufs[cbuf].filled ? (bufs[cbuf].ptr+bufs[cbuf].nlines-bufs[cbuf].scroll)%bufs[cbuf].nlines : max(bufs[cbuf].ptr-bufs[cbuf].scroll, 0) );
- int dl=el-sl;
- if(dl<0) dl+=bufs[cbuf].nlines;
- printf(CLS LOCATE, height-(dl+1), 1);
- resetcol();
- int l;
- for(l=sl;l!=el;l=(l+1)%bufs[cbuf].nlines)
- {
- setcolour(bufs[cbuf].lc[l]);
- if(full_width_colour)
+
+int render_buffer(int buf)
+{
+ if(bufs[buf].rendered)
+ return(0); // nothing to do
+ int l=start;
+ int pl;
+ for(pl=0;pl<PBUFSIZ;pl++)
+ {
+ if(bufs[buf].lpt[pl]) free(bufs[buf].lpt[pl];
+ bufs[buf].lpt[pl]=NULL;
+ }
+ pl=0;
+ int apl=0;
+ bool bot=false, // passed screen bottom (scroll and ascroll)?
+ bot1=false; // within the logical line of screen bottom (scroll)?
+ int as=bufs[buf].astart;
+ char *curline=NULL, *last=NULL, *n=NULL;
+ bool filled=false;
+ int overfill=0;
+ while(!(bot && (((bufs[buf].pscrbot-height-(overfill%PBUFSIZ)+2*PBUFSIZ)%PBUFSIZ)>((pl-bufs[buf].pscrbot+PBUFSIZ)%PBUFSIZ))))
+ {
+ if(!curline)
{
- printf("%s" CLR, bufs[cbuf].lt[l]);
- resetcol();
- printf("\n");
+ if(last) free(last); last=NULL;
+ char *tag;int ci,cl;
+ init_char(&curline, &cl, &ci);
+ s_setcolour(bufs[buf].lc[l], &curline, &cl, &ci);
+ append_str(&curline, &cl, &ci, bufs[buf].ltag[l]);
+ wordline(bufs[buf].lt[l], strlen(curline), &curline, bufs[buf].lc);
+ if(full_width_colour)
+ {
+ append_str(&curline, &cl, &ci, CLR);
+ s_resetcol(&curline, &cl, &ci);
+ }
+ else
+ {
+ s_resetcol(&curline, &cl, &ci);
+ append_str(&curline, &cl, &ci, CLR);
+ }
+ append_char(&curline, &cl, &ci, '\n');
+ n=strchr(out, '\n');
+ free(ltd);
+ last=curline;
+ apl=0;
+ }
+ if(!*n) n=NULL;
+ char oldc;
+ if(n)
+ {
+ oldc=*n++;
+ *n=0;
+ }
+ if(as)
+ {
+ as--;
}
else
{
- printf("%s", bufs[cbuf].lt[l]);
- resetcol();
- printf(CLR "\n");
+ bufs[buf].lpt[pl]=strdup(curline);
+ pl=(pl+1)%PBUFSIZ;
+ if(filled)
+ {
+ overfill++;
+ }
+ else
+ {
+ if(!pl)
+ filled=true;
+ }
+ }
+ if(n)
+ {
+ *n=oldc;
+ curline=n;
+ n=strchr(curline, '\n');
+ apl++;
+ if(bot1 && (apl==bufs[buf].ascroll))
+ {
+ bot=true;
+ bufs[buf].pscrbot=pl;
+ }
+ }
+ else
+ {
+ curline=NULL;
+ if(as)
+ {
+ bufs[buf].astart=as;bufs[buf].start++;
+ }
+ if(bot1)
+ {
+ bot=true; // weren't enough physical lines in this logical line
+ bufs[buf].pscrbot=pl;
+ bufs[buf].scroll=(bufs[buf].scroll+1)%bufs[buf].nlines;
+ bufs[buf].ascroll=0;
+ }
+ l=(l+1)%bufs[buf].nlines;
+ if(l==bufs[buf].ptr)
+ {
+ if(!bot)
+ {
+ bufs[buf].scroll=l-1;
+ bufs[buf].ascroll=apl;
+ bufs[buf].pscrbot=pl;
+ }
+ break;
+ }
+ else if(bufs[buf].scroll==l)
+ {
+ bot1=true;
+ if(bufs[buf].ascroll==0)
+ {
+ bot=true;
+ bufs[buf].pscrbot=pl;
+ }
+ }
}
}
- printf(CLA "\n");
-*/
+ if(filled)
+ {
+ bufs[buf].pstart=(pl+1)%PBUFSIZ;
+ if(overfill)
+ {
+ bufs[buf].astart+=overfill; // so at least it'll be less wasteful next time
+ }
+ }
+ else
+ {
+ bufs[buf].pstart=0;
+ }
+ if(last) free(last); last=NULL;
+}
+
int buf_print(int buf, colour lc, char *lt)
{
if(!bufs) return(2);
View
7 buffer.h
@@ -52,8 +52,10 @@ typedef struct _buf
char *topic; // used for CHANNELs
int nlines; // number of lines allocated
int ptr; // pointer to current unproc line
- int scroll; // unproc line of current pscrbot (distance up from ptr)
- int ascroll; // proc line (lpt offset) of start of [scroll]
+ int start; // unproc line of current pstart
+ int astart; // physical line within [start]
+ int scroll; // unproc line of screen bottom
+ int ascroll; // physical line within [scroll]
colour *lc; // array of colours for lines
char **lt; // array of (unprocessed) text for lines
char **ltag; // array of (unprocessed) tag text for lines
@@ -100,6 +102,7 @@ int init_buffer(int buf, btype type, char *bname, int nlines);
int free_buffer(int buf);
int add_to_buffer(int buf, colour lc, char *lt);
int redraw_buffer(void);
+int render_buffer(int buf);
int buf_print(int buf, colour lc, char *lt); // don't include trailing \n, because buf_print appends CLR \n
int w_buf_print(int buf, colour lc, char *lt, char *lead);
int e_buf_print(int buf, colour lc, message pkt, char *lead);
View
11 colour.c
@@ -1,7 +1,7 @@
/*
- quIRC - simple terminal-based IRC client
- Copyright (C) 2010 Edward Cree
-
+ quIRC - simple terminal-based IRC client
+ Copyright (C) 2010 Edward Cree
+
See quirc.c for license information
colour: defined colours & mirc-colour-compat
*/
@@ -13,6 +13,11 @@ int setcolour(colour c)
return(setcol(c.fore, c.back, c.hi, c.ul));
}
+int s_setcolour(colour c, char **rv, int *l, int *i)
+{
+ return(s_setcol(c.fore, c.back, c.hi, c.ul, rv, l, i));
+}
+
colour c_mirc(int fore, int back)
{
/*mIRC colours:
View
9 colour.h
@@ -1,8 +1,8 @@
#pragma once
/*
- quIRC - simple terminal-based IRC client
- Copyright (C) 2010 Edward Cree
-
+ quIRC - simple terminal-based IRC client
+ Copyright (C) 2010 Edward Cree
+
See quirc.c for license information
colour: defined colours & mirc-colour-compat
*/
@@ -21,7 +21,8 @@ colour;
#include "ttyesc.h"
-int setcolour(colour);
+int setcolour(colour); // wrapper for setcol
+int s_setcolour(colour, char **, int *, int *); // wrapper for s_setcol
colour c_mirc(int, int);
int c_init(void);
View
11 ttyesc.c
@@ -1,7 +1,7 @@
/*
- quIRC - simple terminal-based IRC client
- Copyright (C) 2010 Edward Cree
-
+ quIRC - simple terminal-based IRC client
+ Copyright (C) 2010 Edward Cree
+
See quirc.c for license information
ttyesc: ANSI Terminal Escape Sequences
*/
@@ -52,6 +52,11 @@ int resetcol(void)
return(setcol(7, 0, false, false));
}
+int s_resetcol(char **rv, int *l, int *i)
+{
+ return(s_setcol(7, 0, false, false, rv, l, i));
+}
+
int settitle(char *newtitle)
{
printf("\033]0;%s\007", newtitle);
View
7 ttyesc.h
@@ -1,8 +1,8 @@
#pragma once
/*
- quIRC - simple terminal-based IRC client
- Copyright (C) 2010 Edward Cree
-
+ quIRC - simple terminal-based IRC client
+ Copyright (C) 2010 Edward Cree
+
See quirc.c for license information
ttyesc: ANSI Terminal Escape Sequences
*/
@@ -21,4 +21,5 @@
int setcol(int fore, int back, bool hi, bool ul); // sets the text colour
int s_setcol(int fore, int back, bool hi, bool ul, char **rv, int *l, int *i); // writes a setcol-like string with append_char (see bits.h)
int resetcol(void); // default setcol() values
+int s_resetcol(char **rv, int *l, int *i); // s_setcol to the colour set by resetcol
int settitle(char *newtitle); // sets the window title if running in a term in a window system (eg. xterm)

0 comments on commit 003269b

Please sign in to comment.