Luna/kernel/src/fs/devices/ConsoleDevice.cpp
2023-03-23 21:34:09 +01:00

59 lines
1.3 KiB
C++

#include "fs/devices/ConsoleDevice.h"
#include "video/TextConsole.h"
#include <luna/Buffer.h>
#include <luna/CString.h>
#include <luna/Vector.h>
static Buffer g_console_input;
Result<SharedPtr<Device>> ConsoleDevice::create()
{
return (SharedPtr<Device>)TRY(make_shared<ConsoleDevice>());
}
Result<usize> ConsoleDevice::read(u8* buf, usize, usize length) const
{
if (length > g_console_input.size()) length = g_console_input.size();
memcpy(buf, g_console_input.data(), length);
memmove(g_console_input.data(), g_console_input.data() + length, g_console_input.size() - length);
g_console_input.try_resize(g_console_input.size() - length).release_value();
return length;
}
Result<usize> ConsoleDevice::write(const u8* buf, usize, usize length)
{
TextConsole::write((const char*)buf, length);
return length;
}
bool ConsoleDevice::blocking() const
{
return g_console_input.size() == 0;
}
static Vector<u8> g_temp_input;
void ConsoleDevice::did_press_key(char key)
{
if (key == '\b')
{
if (g_temp_input.try_pop().has_value()) TextConsole::putwchar(L'\b');
return;
}
g_temp_input.try_append((u8)key).value();
if (key == '\n')
{
g_console_input.append_data(g_temp_input.data(), g_temp_input.size()).value();
g_temp_input.clear();
}
TextConsole::putchar(key);
}