Cthulhu  0.2.10
Cthulhu compiler collection
sources.cpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 #include "stdafx.hpp"
3 
5 
6 using namespace ed;
7 
8 SourceView::SourceView(const fs::path& ospath)
9  : directory(ospath.parent_path().string())
10  , basename(ospath.filename().string())
11  , io(ctu::Io::file(ospath.string().c_str()))
12  , error(io.error())
13 {
14  if (error.success())
15  {
16  source = io.text();
17  build_line_offsets();
18  }
19 }
20 
21 void SourceView::build_line_offsets()
22 {
23  line_offsets.clear();
24  line_offsets.push_back(0);
25 
26  for (size_t i = 0; i < source.size(); i++)
27  {
28  if (source[i] == '\n')
29  {
30  line_offsets.push_back(i + 1);
31  }
32  }
33 }
34 
35 static constexpr ImGuiWindowFlags kSourceFlags
36  = ImGuiWindowFlags_HorizontalScrollbar;
37 static constexpr ImGuiChildFlags kChildFlags
38  = ImGuiChildFlags_AutoResizeY
39  | ImGuiChildFlags_AutoResizeX;
40 
42 {
43  if (ImGui::BeginChild(get_path(), ImVec2(0, 0), kChildFlags, kSourceFlags))
44  {
45  if (error.failed())
46  {
47  ImGui::Text("Failed to open file: %s", error.what());
48  }
49  else
50  {
51  ImGui::TextUnformatted(source.data(), source.data() + source.size());
52  }
53  ImGui::EndChild();
54  }
55 }
std::string_view text() const
Definition: io.cpp:59
bool success() const
Definition: io.cpp:24
const char * what() const
Definition: io.cpp:34
bool failed() const
Definition: io.cpp:29
const char * get_path() const
Definition: sources.hpp:37
void draw_content()
Definition: sources.cpp:41
SourceView(const fs::path &ospath)
Definition: sources.cpp:8
Definition: io.hpp:7
Definition: compile.hpp:13