Skip to content

Commit

Permalink
remove old code
Browse files Browse the repository at this point in the history
  • Loading branch information
apache-hb committed Mar 20, 2024
1 parent abc10dc commit f0e2d9b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 270 deletions.
61 changes: 2 additions & 59 deletions src/frontend/gui/include/editor/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,60 +1,11 @@
#pragma once

#include "core/analyze.h"
#include "std/str.h"

#include <stdint.h>

namespace ed
{
/// @brief how to format the memory value
enum memory_format_t : int
{
/// @brief use the IEC format (1024 bytes per kilobyte)
eIEC,

/// @brief use the SI format (1000 bytes per kilobyte)
eSI,

eFormatCount
};

/// @brief format a human readable string representation of the memory value
///
/// @pre @p buffer points to a valid memory location of at least 32 bytes
///
/// @param value the value to format
/// @param buffer the buffer to write the string to
/// @param format the format to use
///
/// @return the number of characters written to the buffer
size_t memory_to_chars(uintmax_t value, OUT_WRITES(32) char *buffer, memory_format_t format);

/// @brief format a human readable string representation of the memory value in IEC format
///
/// @pre @p buffer points to a valid memory location of at least 32 bytes
///
/// @param value the value to format
/// @param buffer the buffer to write the string to
///
/// @return the number of characters written to the buffer
size_t memory_to_chars_iec(uintmax_t value, OUT_WRITES(32) char *buffer);

/// @brief format a human readable string representation of the memory value in SI format
///
/// @pre @p buffer points to a valid memory location of at least 32 bytes
///
/// @param value the value to format
/// @param buffer the buffer to write the string to
///
/// @return the number of characters written to the buffer
size_t memory_to_chars_si(uintmax_t value, OUT_WRITES(32) char *buffer);

memory_format_t get_memory_format();
void format_memory(uintmax_t value, char *buffer);

char get_thousands_separator();
size_t format_large_number(intmax_t value, OUT_WRITES(64) char *buffer, char seperator);

template<size_t N>
class SmallString
{
Expand All @@ -65,7 +16,7 @@ namespace ed
static SmallString sprintf(const char *format, auto&&... args)
{
SmallString<N> result;
result.length = snprintf(result.buffer, N, format, args...);
result.length = str_sprintf(result.buffer, N, format, args...);
return result;
}

Expand All @@ -80,12 +31,4 @@ namespace ed
{
return SmallString<N>::sprintf(format, args...);
}

template<size_t N>
SmallString<N> numfmt(intmax_t value, char thousands = ed::get_thousands_separator())
{
SmallString<N> result;
result.length = format_large_number(value, result.buffer, thousands);
return result;
}
}
2 changes: 1 addition & 1 deletion src/frontend/gui/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ class EditorUi
{
menu_t windows_menu = {
.name = "Windows",
.header = { &version_info_panel },
.header = { &version_info_panel, &module_panel },
.sections = {
menu_section_t {
.name = "Memory",
Expand Down
122 changes: 62 additions & 60 deletions src/frontend/gui/src/panels/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ using namespace ed;

// helpers

static const ImGuiTableFlags kCallbackTableFlags
= ImGuiTableFlags_BordersV
| ImGuiTableFlags_BordersOuterH
| ImGuiTableFlags_Resizable
| ImGuiTableFlags_RowBg
| ImGuiTableFlags_NoHostExtendX
| ImGuiTableFlags_NoBordersInBody;

static std::string_view from_text_view(const text_view_t& view)
{
return std::string_view(view.text, view.length);
Expand All @@ -27,20 +35,57 @@ static void draw_version(const char *id, version_t version)
ImGui::Text("%s: %d.%d.%d", id, major, minor, patch);
}

static bool begin_table_node(const void *ptr_id, const char *label, int columns, ImGuiTableFlags flags)
{
if (ImGui::TreeNode(ptr_id, label))
{
if (ImGui::BeginTable(label, columns, flags))
{
return true;
}
else
{
ImGui::TreePop();
}
}

return false;
}

static void end_table_node()
{
ImGui::EndTable();
ImGui::TreePop();
}

static void draw_diagnostics(diagnostic_list_t list)
{
if (ImGui::TreeNode((void*)&list, "Diagnostics: %zu", list.count))
if (begin_table_node((void*)&list, "Diagnostics", 4, kCallbackTableFlags))
{
ImGui::TableSetupColumn("ID");
ImGui::TableSetupColumn("Severity");
ImGui::TableSetupColumn("Brief");
ImGui::TableSetupColumn("Description");
ImGui::TableHeadersRow();

for (size_t i = 0; i < list.count; i++)
{
const diagnostic_t *diag = list.diagnostics[i];
char label[128];
(void)std::snprintf(label, std::size(label), "Diagnostic %s | %s", diag->id, severity_name(diag->severity));
ImGui::SeparatorText(label);
ImGui::TextWrapped("%s", diag->brief ? diag->brief : "no brief");
ImGui::TextWrapped("%s", diag->description ? diag->description : "no description");
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TextUnformatted(diag->id);

ImGui::TableNextColumn();
ImGui::TextUnformatted(severity_name(diag->severity));

ImGui::TableNextColumn();
ImGui::TextUnformatted(diag->brief ? diag->brief : "no brief");

ImGui::TableNextColumn();
ImGui::TextUnformatted(diag->description ? diag->description : "no description");
}
ImGui::TreePop();

end_table_node();
}
}

Expand All @@ -58,22 +103,6 @@ static void draw_feature(const char *name, bool supported)
}
}

static void text_memory(uintmax_t value)
{
char buffer[64];
ed::format_memory(value, buffer);
ImGui::Text("%s", buffer);
ImGui::SetItemTooltip("%ju bytes", value);
}

static void draw_memory(const char *label, uintmax_t value)
{
ImGui::Text("%s: ", label);
ImGui::SameLine();

text_memory(value);
}

// module info

ModuleInfoPanel::ModuleInfoPanel(const module_info_t& info, panel_info_t setup)
Expand Down Expand Up @@ -122,14 +151,6 @@ LanguageInfoPanel::LanguageInfoPanel(const language_t& lang, panel_info_t setup)
}
}

static const ImGuiTableFlags kCallbackTableFlags
= ImGuiTableFlags_BordersV
| ImGuiTableFlags_BordersOuterH
| ImGuiTableFlags_Resizable
| ImGuiTableFlags_RowBg
| ImGuiTableFlags_NoHostExtendX
| ImGuiTableFlags_NoBordersInBody;

static float get_tooltip_width()
{
ImVec2 ttsize = ImGui::CalcTextSize("(?)");
Expand Down Expand Up @@ -163,37 +184,14 @@ static void draw_row(const char *name, const void *pfn, const char *tooltip)
draw_function_address(pfn);
}

static bool begin_table_node(const void *ptr_id, const char *label, int columns, ImGuiTableFlags flags)
{
if (ImGui::TreeNode(ptr_id, label))
{
if (ImGui::BeginTable(label, columns, flags))
{
return true;
}
else
{
ImGui::TreePop();
}
}

return false;
}

static void end_table_node()
{
ImGui::EndTable();
ImGui::TreePop();
}

void LanguageInfoPanel::draw_content()
{
float ttwidth = get_tooltip_width();
ModuleInfoPanel::draw_info();
ImGui::Text("Default extensions: %s", extensions.c_str());

draw_memory("Context struct size", lang.context_size);
draw_memory("AST struct size", lang.ast_size);
ImGui::Text("Context struct size: %zu", lang.context_size);
ImGui::Text("AST struct size: %zu", lang.ast_size);

if (begin_table_node((void*)&lang.builtin, "Builtin", 2, kCallbackTableFlags))
{
Expand All @@ -203,16 +201,19 @@ void LanguageInfoPanel::draw_content()

for (size_t i = 0; i < lang.builtin.length; i++)
{
const char *name = "no name";
if (lang.builtin.names)
name = lang.builtin.names[i];
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TextUnformatted(lang.builtin.names[i]);
ImGui::TextUnformatted(name ? name : "no name");
ImGui::TableNextColumn();
text_memory(lang.builtin.decls[i]);
ImGui::Text("%zu", lang.builtin.decls[i]);
}
end_table_node();
}

if (begin_table_node((void*)&lang.fn_passes, "Callbacks", 3, kCallbackTableFlags))
if (begin_table_node((void*)&lang, "Callbacks", 3, kCallbackTableFlags))
{
ImGui::TableSetupColumn("Info", ImGuiTableColumnFlags_WidthFixed, ttwidth);
ImGui::TableSetupColumn("Name");
Expand Down Expand Up @@ -276,11 +277,12 @@ void LanguageInfoPanel::draw_content()
ImGui::SetItemTooltip("This language does not provide a scanner");
}

if (begin_table_node((void*)&lang.fn_passes, "Passes", 2, kCallbackTableFlags))
if (begin_table_node((void*)&lang.fn_passes, "Passes", 3, kCallbackTableFlags))
{
ImGui::TableSetupColumn("Index", ImGuiTableColumnFlags_WidthFixed, 30.f);
ImGui::TableSetupColumn("Name");
ImGui::TableSetupColumn("Address");
ImGui::TableHeadersRow();

for (size_t i = 0; i < ePassCount; i++)
{
Expand Down
50 changes: 0 additions & 50 deletions src/frontend/gui/src/panels/settings.cpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,12 @@
#include "editor/panels/settings.hpp"
#include "editor/utils.hpp"

#include "std/str.h"

using namespace ed;

static int gMemoryFormat = eIEC;
static char gThousandsSeparator = ',';

static constexpr const char *kMemoryFormatNames[eFormatCount] = {
"IEC (base 10)",
"SI (base 2)",
};

void SettingsPanel::draw_content()
{
ImGui::Text("Display");

ImGui::Combo("Memory Format", &gMemoryFormat, kMemoryFormatNames, eFormatCount);
}

SettingsPanel::SettingsPanel()
: IEditorPanel("Settings")
{ }

char ed::get_thousands_separator()
{
return gThousandsSeparator;
}

memory_format_t ed::get_memory_format()
{
return (memory_format_t)gMemoryFormat;
}

void ed::format_memory(uintmax_t value, char *buffer)
{
ed::memory_to_chars(value, buffer, get_memory_format());
}

size_t ed::format_large_number(intmax_t value, char *buffer, char seperator)
{
// format a string, inserting the thousands separator every 3 digits
size_t len = str_sprintf(buffer, 64, "%jd", value);
size_t offset = len % 3;

for (size_t i = 0; i < len; i++)
{
if (i > 0 && (i - offset) % 3 == 0)
{
buffer[i] = seperator;
offset--;
}
else
{
buffer[i] = buffer[i - offset];
}
}

return len + (len / 3);
}
Loading

0 comments on commit f0e2d9b

Please sign in to comment.