giga

Terminal text editor
git clone git://git.dimitrijedobrota.com/giga.git
Log | Files | Refs | README | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

commit 0dc45513c84776586408c8170e18c29defe65036
parent ac8e3cc68f65d47139eccd484340aa5d2e4096fe
author Dimitrije Dobrota < mail@dimitrijedobrota.com >
date Sun, 2 Mar 2025 09:47:16 +0100

Proper horizontal scrolling, code cleanup

Diffstat:
M CMakeLists.txt | ++ --
M source/main.cpp | ++++++++++++++++++++++++++++ ------------------------------

2 files changed, 30 insertions(+), 32 deletions(-)


diff --git a/ CMakeLists.txt b/ CMakeLists.txt

@@ -4,7 +4,7 @@ include(cmake/prelude.cmake)


project(
giga
VERSION 0.1.4
VERSION 0.1.5
DESCRIPTION "Terminal text editor"
HOMEPAGE_URL "https://git.dimitrijedobrota.com/giga.git"
LANGUAGES CXX

@@ -13,7 +13,7 @@ project(

include(cmake/project-is-top-level.cmake)
include(cmake/variables.cmake)

find_package(display 0.2 CONFIG REQUIRED)
find_package(display 0.3 CONFIG REQUIRED)
find_package(alec 0.1.13 CONFIG REQUIRED)

# ---- Declare library ----

diff --git a/ source/main.cpp b/ source/main.cpp

@@ -56,17 +56,26 @@ public:

{
line_reset();

const auto cursor = cursor_clamp();
const auto linenum_width = get_linenum_width();
const std::size_t available_width = wth().value() - linenum_width - 1;

auto cursor = m_cursor;
auto line = m_line;

const auto linenum_width = get_linenum_width();
const std::size_t available_width = wth().value() - linenum_width;
for (std::size_t i = 0; i < hgt().value(); i++, line.y++) {
if (line.y.value() >= m_file.size()) {
cursor_clamp_width(cursor.x);

const auto overshoot = cursor.x / (available_width / 2);
if (overshoot > 1) {
line.x += (overshoot - 1) * (available_width / 2);
cursor.x -= (overshoot - 1) * (available_width / 2);
}

for (std::size_t i = 0; i < hgt(); i++, line.y++) {
if (line.y >= m_file.size()) {
break;
}

if (i == cursor.y.value()) {
if (i == cursor.y) {
std::cout << alec::background_v<alec::Color::BLACK>;
}

@@ -74,7 +83,7 @@ public:

get_linenum(line.y.value(), linenum_width) + " "
+ m_file.substr(line.y.value(), line.x.value(), available_width));

if (i == cursor.y.value()) {
if (i == cursor.y) {
std::cout << alec::background_v<alec::Color::DEFAULT>;
}
}

@@ -96,9 +105,9 @@ public:

}

if (evnt.key() == 'j') {
if (m_cursor.y.value() + 1 < hgt().value()) {
if (m_cursor.y + 1 < hgt().value()) {
m_cursor.y++;
} else if (m_line.y.value() + hgt().value() < m_file.size()) {
} else if (m_line.y + hgt() < m_file.size()) {
m_line.y++;
}

@@ -108,9 +117,9 @@ public:

}

if (evnt.key() == 'k') {
if (m_cursor.y.value() > 0) {
if (m_cursor.y > 0) {
m_cursor.y--;
} else if (m_line.y.value() > 0) {
} else if (m_line.y > 0) {
m_line.y--;
}

@@ -122,17 +131,13 @@ public:

if (evnt.key() == 'l') {
using display::xpos_t;

m_cursor = cursor_clamp();
cursor_clamp_width(m_cursor.x);

const auto& line = m_file[(m_line.y + m_cursor.y).value()];
const auto size = sub_lim(xpos_t(line.size()), m_line.x, xpos_t(0));

const std::size_t width = wth().value() - 4;
const auto limit = std::min(size.value(), xpos_t(width).value());
if (m_cursor.x.value() + 1 < limit) {
if (m_cursor.x.value() + 1 < size) {
m_cursor.x++;
} else if (m_cursor.x.value() + 1 >= width) {
m_line.x.value() += width / 2U;
}

evnt.type() = event::Type::NONE;

@@ -143,13 +148,10 @@ public:

if (evnt.key() == 'h') {
using display::xpos_t;

m_cursor = cursor_clamp();
cursor_clamp_width(m_cursor.x);

const std::size_t width = wth().value() - 4;
if (m_cursor.x.value() > 0) {
if (m_cursor.x > 0) {
m_cursor.x--;
} else if (m_line.x.value() > 0) {
m_line.x = sub_lim(m_line.x, xpos_t(width / 2), xpos_t(0));
}

evnt.type() = event::Type::NONE;

@@ -177,22 +179,18 @@ private:

return digits;
}

display::pos_t cursor_clamp() const
void cursor_clamp_width(display::xpos_t& xpos) const
{
using display::clamp_high, display::xpos_t;

auto cursor = m_cursor;

const auto& line = m_file[(m_line.y + m_cursor.y).value()];
const auto size = sub_lim(xpos_t(line.size()), m_line.x, xpos_t(0));

if (size.value() > 0) {
cursor.x = clamp_high(m_cursor.x, size - 1);
if (size == 0) {
xpos = xpos_t(0);
} else {
cursor.x.value() = 0;
xpos = clamp_high(xpos, size - 1);
}

return cursor;
}

File m_file;