/* FILE WRITER HTFWrite.h
** ===========
**
** This version of the stream object just writes to a C file.
** The file is assumed open and left open.
**
** Bugs:
** strings written must be less than buffer size.
*/
#include "HTUtils.h"
#include "HTFWriter.h"
#include "HTFormat.h"
#include "HTAlert.h"
#include "HTFile.h"
#include "LYLeaks.h"
#define FREE(x) if (x) {free(x); x = NULL;}
/* Stream Object
** ------------
*/
struct _HTStream {
CONST HTStreamClass * isa;
FILE * fp;
char * end_command;
char * remove_command;
BOOL announce;
};
/*_________________________________________________________________________
**
** B L A C K H O L E C L A S S
**
** There is only one black hole instance shared by anyone
** who wanst a black hole. These black holes don't radiate,
** they just absorb data.
*/
PRIVATE void HTBlackHole_put_character ARGS2(HTStream *, me, char, c)
{}
PRIVATE void HTBlackHole_put_string ARGS2(HTStream *, me, CONST char*, s)
{}
PRIVATE void HTBlackHole_write ARGS3(HTStream *, me, CONST char*, s, int, l)
{}
PRIVATE void HTBlackHole_free ARGS1(HTStream *, me)
{}
PRIVATE void HTBlackHole_abort ARGS2(HTStream *, me, HTError, e)
{}
/* Black Hole stream
** -----------------
*/
PRIVATE CONST HTStreamClass HTBlackHoleClass =
{
"BlackHole",
HTBlackHole_free,
HTBlackHole_abort,
HTBlackHole_put_character, HTBlackHole_put_string,
HTBlackHole_write
};
PRIVATE HTStream HTBlackHoleInstance =
{
&HTBlackHoleClass,
NULL,
NULL,
NULL,
NO
};
/* Black hole craetion
*/
PUBLIC HTStream * HTBlackHole NOARGS
{
return &HTBlackHoleInstance;
}
/*_________________________________________________________________________
**
** F I L E A C T I O N R O U T I N E S
** Bug:
** All errors are ignored.
*/
/* Character handling
** ------------------
*/
PRIVATE void HTFWriter_put_character ARGS2(HTStream *, me, char, c)
{
putc(c, me->fp);
}
/* String handling
** ---------------
**
** Strings must be smaller than this buffer size.
*/
PRIVATE void HTFWriter_put_string ARGS2(HTStream *, me, CONST char*, s)
{
fputs(s, me->fp);
}
/* Buffer write. Buffers can (and should!) be big.
** ------------
*/
PRIVATE void HTFWriter_write ARGS3(HTStream *, me, CONST char*, s, int, l)
{
fwrite(s, 1, l, me->fp);
}
/* Free an HTML object
** -------------------
**
** Note that the SGML parsing context is freed, but the created
** object is not,
** as it takes on an existence of its own unless explicitly freed.
*/
PRIVATE void HTFWriter_free ARGS1(HTStream *, me)
{
fclose(me->fp);
if (me->end_command) { /* Temp file */
_HTProgress(me->end_command); /* Tell user what's happening */
system(me->end_command);
FREE(me->end_command);
if (me->remove_command) {
system(me->remove_command);
FREE(me->remove_command);
}
}
FREE(me);
}
/* End writing
*/
PRIVATE void HTFWriter_abort ARGS2(HTStream *, me, HTError, e)
{
fclose(me->fp);
if (me->end_command) { /* Temp file */
if (TRACE) fprintf(stderr,
"HTFWriter: Aborting: file not executed.\n");
FREE(me->end_command);
if (me->remove_command) {
system(me->remove_command);
FREE(me->remove_command);
}
}
FREE(me);
}
/* Structured Object Class
** -----------------------
*/
PRIVATE CONST HTStreamClass HTFWriter = /* As opposed to print etc */
{
"FileWriter",
HTFWriter_free,
HTFWriter_abort,
HTFWriter_put_character, HTFWriter_put_string,
HTFWriter_write
};
/* Subclass-specific Methods
** -------------------------
*/
PUBLIC HTStream* HTFWriter_new ARGS1(FILE *, fp)
{
HTStream* me;
if (!fp) return NULL;
me = (HTStream*)malloc(sizeof(*me));
if (me == NULL) outofmem(__FILE__, "HTML_new");
me->isa = &pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */package ui
import (
"fmt"
"github.com/gdamore/tcell"
"github.com/gdamore/tcell/views"
"github.com/mattn/go-runewidth"
)
// A context allows you to draw in a sub-region of the terminal
type Context struct {
screen tcell.Screen
viewport *views.ViewPort
x, y int
onPopover func(*Popover)
}
func (ctx *Context) X() int {
x, _, _, _ := ctx.viewport.GetPhysical()
return x
}
func (ctx *Context) Y() int {
_, y, _, _ := ctx.viewport.GetPhysical()
return y
}
func (ctx *Context) Width() int {
width, _ := ctx.viewport.Size()
return width
}
func (ctx *Context) Height() int {
_, height := ctx.viewport.Size()
return height
}
func NewContext(width, height int, screen tcell.Screen, p func(*Popover)) *Context {
vp := views.NewViewPort(screen, 0, 0, width, height)
return &Context{screen, vp, 0, 0, p}
}
func (ctx *Context) Subcontext(x, y, width, height int) *Context {
vp_width, vp_height := ctx.viewport.Size()
if x < 0 || y < 0 {
panic(fmt.Errorf("Attempted to create context with negative offset"))
}
if x+width > vp_width || y+height > vp_height {
panic(fmt.Errorf("Attempted to create context larger than parent"))
}
vp := views.NewViewPort(ctx.viewport, x, y, width, height)
return &Context{ctx.screen, vp, ctx.x + x, ctx.y + y, ctx.onPopover}
}
func (ctx *Context) SetCell(x, y int, ch rune, style tcell.Style) {
width, height := ctx.viewport.Size()
if x >= width || y >= height {
panic(fmt.Errorf("Attempted to draw outside of context"))
}
crunes := []rune{}
ctx.viewport.SetContent(x, y, ch, crunes, style)
}
func (ctx *Context) Printf(x, y int, style tcell.Style,
format string, a ...interface{}) int {
width, height := ctx.viewport.Size()
if x >= width || y >= height {
panic(fmt.Errorf("Attempted to draw outside of context"))
}
str := fmt.Sprintf(format, a...)
old_x := x
newline := func() bool {
x = old_x
for _, ch := range str {
switch ch {
case '\n':
if !newline() {
return runewidth.StringWidth(str)
}
case '\r':
x = old_x
default:
crunes := []rune{}
ctx.viewport.SetContent(x, y, ch, crunes, style)
x += runewidth.RuneWidth(ch)
if x == old_x+width {
if !newline() {
return runewidth.StringWidth(str)
}
}
}
}
return runewidth.StringWidth(str)
}
func (ctx *Context) Fill(x, y, width, height int, rune rune, style tcell.Style) {
vp := views.NewViewPort(ctx.viewport, x, y, width, height)
vp.Fill(rune, style)
}
func (ctx *Context) SetCursor(x, y int) {
ctx.screen.ShowCursor(ctx.x+x, ctx.y+y)
}
func (ctx *Context) HideCursor() {
ctx.screen.HideCursor()
}
func (ctx *Context) Popover(x, y, width, height int, d Drawable) {
ctx.onPopover(&Popover{
x: ctx.x + x,
y: ctx.y + y,
width: width,
height: height,
content: d,
})
}