#include "Status.h"
#include "Buffer.h"
#include "Conversion.h"
#include "Editor.h"
#include "File.h"
#include "Line.h"
#include <sstream>
#include <stdlib.h>
#include <termbox.h>
void Status::draw(Buffer& buf) {
int16_t fg = 232, bg = 220;
// Text aligned to the left
std::wostringstream left;
// Filename
auto wfilename = Convert::to_wide_string(buf.filename.c_str());
left << L" " << wfilename.get();
left << L" " << buf.force;
// Modified
if(buf.modified) {
left << L" [modified]";
}
auto left_str = left.str();
// Text aligned to the right
std::wostringstream right;
right << L" ";
// Character count
right << L"Col " << buf.get_visible_x() - 1;
right << L"/" << buf.current_line().width();
right << L" | ";
// Line count
right << L"Line " << buf.get_current_line_nb();
right << L"/" << buf.m_text.get_nb_lines();
right << L" ";
auto right_str = right.str();
int rwidth = right_str.size();
// Yellow background
for(int i = 0; i < tb_width(); i++) {
tb_change_cell(i, tb_height() - 1, ' ', fg, bg);
}
// Filename
Canvas::print(left_str.c_str(), 0, tb_height() - 1, fg, bg);
// Text
Canvas::print(right_str.c_str(), tb_width() - rwidth, tb_height() - 1, fg, bg);
// Print message if there is one
if(_msg != nullptr) {
// Set message colors
switch(_type) {
case StatusType::DEFAULT:
case StatusType::INFO:
break;
case StatusType::ERROR:
bg = 9;
fg = 15;
break;
case StatusType::SUCCESS:
bg = 2;
fg = 15;
break;
/*
case StatusType::PROMPT:
bg = 0;
fg = 234;
break;
*/
}
// Draw the message
for(int i = 0; i < tb_width() - rwidth; i++) {
tb_change_cell(i, tb_height() - 1, ' ', fg, bg);
}
Canvas::print(_msg, 1, tb_height() - 1, fg, bg);
}
if(prompt != nullptr) {
if(prompt->is_prompting()) {
prompt->draw(rwidth);
} else {
prompt = nullptr;
}
}
}
int Status::height() {
// Could be higher in the future
return 1;
}
void Status::handle_key(tb_event* ev) {
if(_msg == nullptr) return;
// if(_type == StatusType::PROMPT) return;
// Remove currently displayed msg
free(_msg);
_msg = nullptr;
}
void Status::set_msg(const wchar_t* msg, StatusType type) {
if(_msg != nullptr) free(_msg);
_msg = wcsdup(msg);
_type = type;
}