Skip to content

Commit

Permalink
removed Buffer resize calls inside Editor + added Windows::resize to …
Browse files Browse the repository at this point in the history
…finally get a fully working vertical split support
  • Loading branch information
teju85 committed Aug 7, 2019
1 parent 2737703 commit 97eabad
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/core/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
namespace teditor {

Buffer::Buffer(const std::string& name):
screenStart(), screenDim(), lines(), startLine(0), modified(false),
readOnly(false), buffName(name), fileName(), dirName(), regions(),
mode(Mode::createMode("text")), locs(), undoStack(), redoStack() {
lines(), startLine(0), modified(false), readOnly(false), buffName(name),
fileName(), dirName(), regions(), mode(Mode::createMode("text")), locs(),
undoStack(), redoStack() {
addLine();
dirName = getpwd();
locs.push_back(Pos2di(0, 0));
Expand Down
1 change: 0 additions & 1 deletion src/core/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ class Buffer {
typedef std::stack<OpData> OpStack;


Pos2d<int> screenStart, screenDim;
std::vector<Line> lines;
int startLine;
bool modified, readOnly;
Expand Down
22 changes: 6 additions & 16 deletions src/core/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,13 @@ void Editor::runCmd(const std::string& cmd) {
}

void Editor::createScratchBuff(bool switchToIt) {
auto* buf = buffs.push_back("*scratch");
bufResize(buf);
buffs.push_back("*scratch");
if(switchToIt) setCurrBuff((int)buffs.size() - 1);
}

void Editor::createReadOnlyBuff(const std::string& name,
const std::string& contents, bool switchToIt) {
auto* buf = buffs.push_back(name);
bufResize(buf);
buf->insert(contents);
buf->makeReadOnly();
if(switchToIt) setCurrBuff((int)buffs.size() - 1);
Expand All @@ -112,7 +110,6 @@ void Editor::loadFiles() {

void Editor::load(const std::string& file, int line) {
auto* buf = new Buffer;
bufResize(buf);
buf->load(file, line);
buffs.push_back(buf);
setCurrBuff((int)buffs.size() - 1);
Expand Down Expand Up @@ -272,7 +269,7 @@ void Editor::resize() {
const auto& defaultbg = getColor("defaultbg");
backbuff.clear(defaultfg, defaultbg);
frontbuff.clear(defaultfg, defaultbg);
for(auto itr : buffs) bufResize(itr);
bufResize();
clearScreen();
}

Expand All @@ -286,7 +283,6 @@ Buffer& Editor::getMessagesBuff() {
Buffer* buf = getBuff("*messages");
if(buf != nullptr) return *buf;
auto* buf1 = new Buffer("*messages");
bufResize(buf1);
buffs.push_back(buf1);
return *buf1;
}
Expand Down Expand Up @@ -368,7 +364,7 @@ std::string Editor::prompt(const std::string& msg, KeyCmdMap* kcMap,
if(kcMap == nullptr) kcMap = &(cmBar->getKeyCmdMap());
if(choices != nullptr) cmBar->setChoices(choices);
cmBar->setMinLoc((int)msg.size());
bufResize(&getBuff());
bufResize();
quitPromptLoop = cancelPromptLoop = false;
cmBar->insert(msg.c_str());
if(!defVal.empty()) cmBar->insert(defVal.c_str());
Expand Down Expand Up @@ -408,7 +404,7 @@ std::string Editor::prompt(const std::string& msg, KeyCmdMap* kcMap,
cmBar->clearChoices();
}
unselectCmBar();
bufResize(&getBuff());
bufResize();
return ret;
}

Expand All @@ -417,15 +413,9 @@ void Editor::draw() {
windows.draw(*this, cmdMsgBarActive);
}

void Editor::bufResize(Buffer* buf) {
auto& term = Terminal::getInstance();
void Editor::bufResize() {
int ht = cmBarHeight();
Pos2di sz(term.width(), term.height());
sz.y -= ht;
///@todo: add support for multiple windows
getWindow().resize({0, 0}, sz);
getCmBarWindow().resize({0, sz.y}, {sz.x, ht});
DEBUG("bufResize: buff-x,y=%d,%d ht=%d\n", sz.x, sz.y, ht);
windows.resize(ht);
}

void Editor::render() {
Expand Down
2 changes: 1 addition & 1 deletion src/core/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Editor {
void writef(const char* fmt, ...);
void draw();
void loadFiles();
void bufResize(Buffer* mlb);
void bufResize();
Buffer* getBuff(const std::string& name);
const AttrColor& getColor(const std::string& name) const;
int cmBarHeight() const;
Expand Down
24 changes: 24 additions & 0 deletions src/core/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "editor.h"
#include "window.h"
#include "logger.h"
#include "terminal.h"

namespace teditor {

Expand Down Expand Up @@ -52,6 +53,29 @@ Windows::~Windows() {
borders.clear();
}

void Windows::resize(int cmBarHt) {
auto& term = Terminal::getInstance();
Pos2di sz(term.width(), term.height());
sz.y -= cmBarHt;
DEBUG("Windows::resize: buff-x,y=%d,%d ht=%d\n", sz.x, sz.y, cmBarHt);
// cmbar
wins[0]->resize({0, sz.y}, {sz.x, cmBarHt});
// rest of the windows
screenDim = sz;
wins[1]->resize({0, 0}, sz);
borders.clear();
if(wins.size() == 2) return;
///@todo: update this to support arbitrary splits!
// -1 for the border
int lWidth = (sz.x - 1) / 2;
int rWidth = sz.x - 1 - lWidth;
auto start = wins[1]->start();
wins[1]->resize(start, {lWidth, sz.y});
wins[2]->resize({start.x+lWidth+1, start.y}, {rWidth, sz.y});
Border b = {start.y, start.y+sz.y, start.x + lWidth};
borders.push_back(b);
}

void Windows::incrementCurrWin() {
++currWin;
if(currWin >= (int)wins.size()) currWin = 1;
Expand Down
1 change: 1 addition & 0 deletions src/core/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class Windows {
*/
bool splitVertically();
void clearAll();
void resize(int cmBarHt);

private:
std::vector<Window*> wins;
Expand Down

0 comments on commit 97eabad

Please sign in to comment.