Skip to content

Commit

Permalink
Switched ecma48 terminal over to drive a screen buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
mridgers committed May 30, 2018
1 parent 021f9c8 commit 432e36a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
3 changes: 2 additions & 1 deletion clink/terminal/include/terminal/terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

class screen_buffer;
class terminal_in;
class terminal_out;

Expand All @@ -14,5 +15,5 @@ struct terminal
};

//------------------------------------------------------------------------------
terminal terminal_create();
terminal terminal_create(screen_buffer* screen=nullptr);
void terminal_destroy(const terminal& terminal);
36 changes: 18 additions & 18 deletions clink/terminal/src/ecma48_terminal_out.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,43 @@

#include "pch.h"
#include "ecma48_terminal_out.h"

#include <core/array.h>
#include <core/base.h>
#include "ecma48_iter.h"
#include "screen_buffer.h"

//------------------------------------------------------------------------------
ecma48_terminal_out::ecma48_terminal_out(terminal_out& inner)
: m_inner(inner)
ecma48_terminal_out::ecma48_terminal_out(screen_buffer& screen)
: m_screen(screen)
{
}

//------------------------------------------------------------------------------
void ecma48_terminal_out::begin()
{
m_inner.begin();
m_screen.begin();
}

//------------------------------------------------------------------------------
void ecma48_terminal_out::end()
{
m_inner.end();
m_screen.end();
}

//------------------------------------------------------------------------------
void ecma48_terminal_out::flush()
{
m_inner.flush();
m_screen.flush();
}

//------------------------------------------------------------------------------
int ecma48_terminal_out::get_columns() const
{
return m_inner.get_columns();
return m_screen.get_columns();
}

//------------------------------------------------------------------------------
int ecma48_terminal_out::get_rows() const
{
return m_inner.get_rows();
return m_screen.get_rows();
}

//------------------------------------------------------------------------------
Expand All @@ -52,12 +51,10 @@ void ecma48_terminal_out::write_c1(const ecma48_code& code)
ecma48_code::csi<32> csi;
code.decode_csi(csi);

const array<int> params_array(csi.params, csi.param_count);

switch (csi.final)
{
case 'm':
write_sgr(params_array);
set_attributes(csi);
break;
}
}
Expand Down Expand Up @@ -92,7 +89,7 @@ void ecma48_terminal_out::write(const char* chars, int length)
switch (code->get_type())
{
case ecma48_code::type_chars:
m_inner.write(code->get_pointer(), code->get_length());
m_screen.write(code->get_pointer(), code->get_length());
break;

case ecma48_code::type_c0:
Expand All @@ -107,16 +104,18 @@ void ecma48_terminal_out::write(const char* chars, int length)
}

//------------------------------------------------------------------------------
void ecma48_terminal_out::write_sgr(const array<int>& params)
void ecma48_terminal_out::set_attributes(const ecma48_code::csi_base& csi)
{
// Empty parameters to 'CSI SGR' implies 0 (reset).
if (params.empty())
return;
if (csi.param_count == 0)
return m_screen.set_attributes(attributes::defaults);

// Process each code that is supported.
attributes attr;
for (unsigned int param : params)
for (int i = 0; i < csi.param_count; ++i)
{
unsigned int param = csi.params[i];

// Resets
if (param == 0) { attr = attributes::defaults; continue; }
if (param == 49) { attr.reset_bg(); continue; }
Expand Down Expand Up @@ -153,4 +152,5 @@ void ecma48_terminal_out::write_sgr(const array<int>& params)
// TODO: Rgb/xterm256 support for terminals that support it.
}

m_screen.set_attributes(attr);
}
8 changes: 4 additions & 4 deletions clink/terminal/src/ecma48_terminal_out.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#include "ecma48_iter.h"
#include "terminal_out.h"

template <typename T> class array;
class screen_buffer;

//------------------------------------------------------------------------------
class ecma48_terminal_out
: public terminal_out
{
public:
ecma48_terminal_out(terminal_out& inner);
ecma48_terminal_out(screen_buffer& screen);
virtual void begin() override;
virtual void end() override;
virtual void write(const char* chars, int length) override;
Expand All @@ -23,8 +23,8 @@ class ecma48_terminal_out

private:
void write_c1(const ecma48_code& code);
void write_sgr(const array<int>& params);
void write_c0(int c0);
terminal_out& m_inner;
void set_attributes(const ecma48_code::csi_base& csi);
ecma48_state m_state;
screen_buffer& m_screen;
};
10 changes: 7 additions & 3 deletions clink/terminal/src/terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@

#include "pch.h"
#include "terminal.h"
#include "ecma48_terminal_out.h"
#include "win_screen_buffer.h"
#include "win_terminal_in.h"
#include "win_terminal_out.h"

#include <core/base.h>

//------------------------------------------------------------------------------
terminal terminal_create()
terminal terminal_create(screen_buffer* screen)
{
#if defined(PLATFORM_WINDOWS)
if (screen == nullptr)
screen = new win_screen_buffer(); // TODO: this leaks.

return {
new win_terminal_in(),
new win_terminal_out(),
new ecma48_terminal_out(*screen),
};
#else
return {};
Expand Down

0 comments on commit 432e36a

Please sign in to comment.