Convert hello.console to use a namespacing struct

This commit is contained in:
Josh Holtrop 2022-03-28 15:18:31 -04:00
parent bcec23ef89
commit f904ec2b48
2 changed files with 149 additions and 146 deletions

View File

@ -7,7 +7,9 @@ import uefi;
import hello.hello; import hello.hello;
import core.stdc.stdarg; import core.stdc.stdarg;
/** struct console
{
/**
* Format a hexadecimal value to a string. * Format a hexadecimal value to a string.
* *
* @param s String buffer. * @param s String buffer.
@ -15,8 +17,8 @@ import core.stdc.stdarg;
* @param ptr If true, always output all 16 digits, and separate the upper and * @param ptr If true, always output all 16 digits, and separate the upper and
* lower halves with an underscore. * lower halves with an underscore.
*/ */
private size_t format_hex(CHAR16 * s, ulong v, bool ptr) private static size_t format_hex(CHAR16 * s, ulong v, bool ptr)
{ {
string hex_chars = "0123456789ABCDEF"; string hex_chars = "0123456789ABCDEF";
bool print = ptr; bool print = ptr;
size_t si = 0u; size_t si = 0u;
@ -37,16 +39,16 @@ private size_t format_hex(CHAR16 * s, ulong v, bool ptr)
} }
} }
return si; return si;
} }
/** /**
* Format a decimal value to a string. * Format a decimal value to a string.
* *
* @param s String buffer. * @param s String buffer.
* @param v Value to format. * @param v Value to format.
*/ */
private size_t format_dec(CHAR16 * s, ulong v) private static size_t format_dec(CHAR16 * s, ulong v)
{ {
string dec_chars = "0123456789"; string dec_chars = "0123456789";
bool print; bool print;
char[20] buf; char[20] buf;
@ -62,16 +64,16 @@ private size_t format_dec(CHAR16 * s, ulong v)
s[si] = buf[buf_i - si - 1u]; s[si] = buf[buf_i - si - 1u];
} }
return buf_i; return buf_i;
} }
/** /**
* Write a string to the console. * Write a string to the console.
* *
* @param s Format string. * @param s Format string.
* @param args Variable arguments structure. * @param args Variable arguments structure.
*/ */
void write(string s, va_list args) public static void write(string s, va_list args)
{ {
__gshared static CHAR16[256] s16; __gshared static CHAR16[256] s16;
size_t i = 0u; size_t i = 0u;
bool escape = false; bool escape = false;
@ -127,52 +129,53 @@ void write(string s, va_list args)
} }
s16[i++] = 0u; s16[i++] = 0u;
st.ConOut.OutputString(st.ConOut, &s16[0]); st.ConOut.OutputString(st.ConOut, &s16[0]);
} }
/** /**
* Write a string to the console. * Write a string to the console.
* *
* @param s Format string. * @param s Format string.
*/ */
extern (C) void write(string s, ...) public static extern (C) void write(string s, ...)
{ {
va_list args; va_list args;
va_start(args, s); va_start(args, s);
write(s, args); write(s, args);
va_end(args); va_end(args);
} }
/** /**
* Write a string to the console. * Write a string to the console.
* *
* @param s Format string. * @param s Format string.
*/ */
extern (C) void writeln(string s, ...) public static extern (C) void writeln(string s, ...)
{ {
va_list args; va_list args;
va_start(args, s); va_start(args, s);
write(s, args); write(s, args);
write("\r\n", args); write("\r\n", args);
va_end(args); va_end(args);
} }
/** /**
* Write a message to press any key and wait for a key to be pressed. * Write a message to press any key and wait for a key to be pressed.
*/ */
void wait_key() public static void wait_key()
{ {
writeln("Press any key..."); writeln("Press any key...");
st.ConIn.Reset(st.ConIn, FALSE); st.ConIn.Reset(st.ConIn, FALSE);
EFI_INPUT_KEY key; EFI_INPUT_KEY key;
while (st.ConIn.ReadKeyStroke(st.ConIn, &key) == EFI_NOT_READY) while (st.ConIn.ReadKeyStroke(st.ConIn, &key) == EFI_NOT_READY)
{ {
} }
} }
/** /**
* Clear the console. * Clear the console.
*/ */
void clear() public static void clear()
{ {
st.ConOut.ClearScreen(st.ConOut); st.ConOut.ClearScreen(st.ConOut);
}
} }

View File

@ -4,7 +4,7 @@
module hello.hello; module hello.hello;
import uefi; import uefi;
import console = hello.console; import hello.console;
import hello.scratch; import hello.scratch;
import hulk.bootinfo; import hulk.bootinfo;
import hulk.header; import hulk.header;