Skip to content

Commit

Permalink
Simplify the display_accumulator.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisant996 committed Jan 15, 2024
1 parent c7c4481 commit b4a9b2a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 52 deletions.
10 changes: 4 additions & 6 deletions clink/lib/include/lib/display_readline.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,12 @@ class display_accumulator
public:
display_accumulator();
~display_accumulator();
void split();
void flush();
static void flush();
private:
void restore();
static void fwrite_proc(FILE*, const char*, int32);
static void fflush_proc(FILE*);
void (*m_saved_fwrite)(FILE*, const char*, int32) = nullptr;
void (*m_saved_fflush)(FILE*) = nullptr;
bool m_active = false;
static void (*s_saved_fwrite)(FILE*, const char*, int32);
static void (*s_saved_fflush)(FILE*);
static int32 s_nested;
static bool s_active;
};
82 changes: 36 additions & 46 deletions clink/lib/src/display_readline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1250,89 +1250,79 @@ COORD measure_readline_display(const char* prompt, const char* buffer, uint32 le


//------------------------------------------------------------------------------
void (*display_accumulator::s_saved_fwrite)(FILE*, const char*, int32) = nullptr;
void (*display_accumulator::s_saved_fflush)(FILE*) = nullptr;
bool display_accumulator::s_active = false;
int32 display_accumulator::s_nested = 0;
static str_moveable s_buf;

//------------------------------------------------------------------------------
display_accumulator::display_accumulator()
{
assert(!m_saved_fwrite);
assert(!m_saved_fflush);
assert(rl_fwrite_function);
assert(rl_fflush_function);
assert(s_nested || s_buf.empty());

if (!s_nested)
{
assert(!s_saved_fwrite);
assert(!s_saved_fflush);
assert(!s_active);
assert(s_buf.empty());
s_saved_fwrite = rl_fwrite_function;
s_saved_fflush = rl_fflush_function;
}

++s_nested;

#ifdef DEBUG
if (s_nested == 1)
{
str<> value;
if (os::get_env("DEBUG_NO_DISPLAY_ACCUMULATOR", value) && atoi(value.c_str()) != 0)
return;
}
else
{
if (!s_active)
return;
}
#endif

m_saved_fwrite = rl_fwrite_function;
m_saved_fflush = rl_fflush_function;
m_active = true;
s_active = true;

rl_fwrite_function = fwrite_proc;
rl_fflush_function = fflush_proc;
}

//------------------------------------------------------------------------------
display_accumulator::~display_accumulator()
{
if (m_active)
if (--s_nested == 0)
{
if (s_nested > 1)
restore();
else
flush();
assert(!m_active);
}
--s_nested;
}

//------------------------------------------------------------------------------
void display_accumulator::split()
{
if (m_active && s_buf.length())
{
m_saved_fwrite(_rl_out_stream, s_buf.c_str(), s_buf.length());
m_saved_fflush(_rl_out_stream);
s_buf.clear();
flush();
rl_fwrite_function = s_saved_fwrite;
rl_fflush_function = s_saved_fflush;
s_saved_fwrite = nullptr;
s_saved_fflush = nullptr;
s_active = false;
}
assert(s_nested >= 0);
}

//------------------------------------------------------------------------------
void display_accumulator::flush()
{
if (!m_active || s_nested > 1)
return;

restore();

if (s_buf.length())
assertimplies(!s_active, s_buf.empty());
if (s_active && !s_buf.empty())
{
rl_fwrite_function(_rl_out_stream, s_buf.c_str(), s_buf.length());
rl_fflush_function(_rl_out_stream);
assert(s_saved_fwrite);
assert(s_saved_fflush);
s_saved_fwrite(_rl_out_stream, s_buf.c_str(), s_buf.length());
s_saved_fflush(_rl_out_stream);
s_buf.clear();
}
}

//------------------------------------------------------------------------------
void display_accumulator::restore()
{
assert(m_active);
assert(m_saved_fwrite);
assert(m_saved_fflush);
rl_fwrite_function = m_saved_fwrite;
rl_fflush_function = m_saved_fflush;
m_saved_fwrite = nullptr;
m_saved_fflush = nullptr;
m_active = false;
}

//------------------------------------------------------------------------------
void display_accumulator::fwrite_proc(FILE* out, const char* text, int32 len)
{
Expand Down Expand Up @@ -1753,7 +1743,7 @@ void display_manager::display()
if (is_CJK_codepage(GetConsoleOutputCP()))
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
coalesce.split();
coalesce.flush();
if (get_console_screen_buffer_info(&csbi) &&
m_last_prompt_line_width != csbi.dwCursorPosition.X)
{
Expand Down

0 comments on commit b4a9b2a

Please sign in to comment.