Compare commits
No commits in common. "dded22125cf2954e369376df508bc6a13c832f26" and "a9e9dfc85bf31d48caad25ca3d78ecfa48839913" have entirely different histories.
dded22125c
...
a9e9dfc85b
@ -1,189 +0,0 @@
|
||||
#include <ncurses.h>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "strings.h"
|
||||
using nlohmann::json;
|
||||
|
||||
void save_document(std::string& text, std::vector<std::string>& lines) {
|
||||
text = "";
|
||||
for (const std::string& line : lines) {
|
||||
text.append(line);
|
||||
text.append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void editor_hard(std::string& text) {
|
||||
uint16_t cursor_x = 0;
|
||||
uint16_t cursor_y = 0;
|
||||
std::vector<std::string> lines;
|
||||
char mode = 'n';
|
||||
uint16_t window_height, window_width;
|
||||
|
||||
mode = 'n';
|
||||
|
||||
noecho();
|
||||
curs_set(1);
|
||||
cbreak();
|
||||
keypad(stdscr, TRUE);
|
||||
getmaxyx(stdscr, window_height, window_width);
|
||||
|
||||
// Parse the multi-line string into individual lines
|
||||
lines.clear();
|
||||
std::stringstream ss(text);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(ss, line)) {
|
||||
lines.push_back(line);
|
||||
}
|
||||
|
||||
// Ensure at least one empty line exists
|
||||
if (lines.empty()) {
|
||||
lines.push_back("");
|
||||
}
|
||||
while (true) {
|
||||
clear();
|
||||
// Draw text lines
|
||||
for (size_t i = 0; i < window_height - 1 && i < lines.size(); i++) {
|
||||
mvprintw(i, 0, "%s", lines[i].c_str());
|
||||
}
|
||||
|
||||
// Draw status line
|
||||
std::string status = "Mode: " + std::string(1, mode);
|
||||
mvprintw(window_height - 1, 0, "%s", status.c_str());
|
||||
|
||||
move(cursor_y, cursor_x);
|
||||
refresh();
|
||||
{
|
||||
int ch = getch();
|
||||
switch (mode) {
|
||||
case 'n': // Normal mode
|
||||
switch (ch) {
|
||||
case 'h':
|
||||
if (cursor_x > 0) {
|
||||
cursor_x--;
|
||||
}
|
||||
break;
|
||||
case 'j':
|
||||
if (cursor_y < lines.size() - 1) {
|
||||
cursor_y++;
|
||||
if (cursor_x > lines[cursor_y].length()) {
|
||||
cursor_x = lines[cursor_y].length();
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'k':
|
||||
if (cursor_y > 0) {
|
||||
cursor_y--;
|
||||
if (cursor_x > lines[cursor_y].length()) {
|
||||
cursor_x = lines[cursor_y].length();
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case 'l':
|
||||
if (cursor_x < lines[cursor_y].length()) {
|
||||
cursor_x++;
|
||||
}
|
||||
break;
|
||||
case 'i':
|
||||
case 'a':
|
||||
mode = 'i';
|
||||
break;
|
||||
case 'x':
|
||||
if (cursor_x < lines[cursor_y].length()) {
|
||||
lines[cursor_y].erase(cursor_x, 1);
|
||||
}
|
||||
break;
|
||||
case ':': {
|
||||
// Enter command mode
|
||||
std::string command = "";
|
||||
int cmd_ch;
|
||||
// Show command prompt
|
||||
mvprintw(window_height - 1, 0, ":%s", command.c_str());
|
||||
clrtoeol();
|
||||
refresh();
|
||||
|
||||
while ((cmd_ch = getch()) != '\n' && cmd_ch != '\r') {
|
||||
if (cmd_ch == 27) { // ESC - cancel command
|
||||
break;
|
||||
} else if (cmd_ch == KEY_BACKSPACE || cmd_ch == 127) {
|
||||
if (!command.empty()) {
|
||||
command.pop_back();
|
||||
}
|
||||
} else if (cmd_ch >= 32 && cmd_ch <= 126) {
|
||||
command += static_cast<char>(cmd_ch);
|
||||
}
|
||||
|
||||
mvprintw(window_height - 1, 0, ":%s", command.c_str());
|
||||
clrtoeol();
|
||||
refresh();
|
||||
}
|
||||
|
||||
// Execute command
|
||||
if (cmd_ch != 27) { // If not cancelled
|
||||
switch (hash_djb2a(command)) {
|
||||
case "q"_sh:
|
||||
return;
|
||||
break;
|
||||
case "w"_sh:
|
||||
save_document(text, lines);
|
||||
break;
|
||||
|
||||
case "wq"_sh:
|
||||
case "x"_sh:
|
||||
save_document(text, lines);
|
||||
return;
|
||||
break;
|
||||
|
||||
case "q!"_sh:
|
||||
return;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} break;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'i': // Insert mode
|
||||
switch (ch) {
|
||||
case 27: // ESC
|
||||
mode = 'n';
|
||||
if (cursor_x > 0)
|
||||
cursor_x--;
|
||||
break;
|
||||
case KEY_BACKSPACE:
|
||||
case 127:
|
||||
if (mode == 'i' && cursor_x > 0) {
|
||||
lines[cursor_y].erase(cursor_x - 1, 1);
|
||||
cursor_x--;
|
||||
}
|
||||
break;
|
||||
case '\n':
|
||||
case '\r': {
|
||||
// Insert new line
|
||||
std::string new_line = lines[cursor_y].substr(cursor_x);
|
||||
lines[cursor_y] = lines[cursor_y].substr(0, cursor_x);
|
||||
lines.insert(lines.begin() + cursor_y + 1, new_line);
|
||||
cursor_y++;
|
||||
cursor_x = 0;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
if (ch >= 32 && ch <= 126) {
|
||||
if (mode == 'i') {
|
||||
lines[cursor_y].insert(cursor_x, 1, ch);
|
||||
cursor_x++;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
#ifndef PARADOCS_EDEH_H_
|
||||
#define PARADOCS_EDEH_H_
|
||||
#include <string>
|
||||
void editor_hard(std::string& text);
|
||||
#endif
|
@ -13,6 +13,73 @@
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include "memory.h"
|
||||
#include "strings.h"
|
||||
|
||||
/*
|
||||
size_t spawn_menu(uint16_t begin_y, uint16_t begin_x, const char** choices,
|
||||
size_t n_choices) {
|
||||
ITEM** sp_items;
|
||||
int c;
|
||||
MENU* sp_menu;
|
||||
WINDOW* sp_menu_win;
|
||||
int i;
|
||||
|
||||
sp_items = new ITEM*[n_choices];
|
||||
// Create items
|
||||
for (i = 0; i < n_choices; ++i) {
|
||||
sp_items[i] = new_item(choices[i], choices[i]);
|
||||
}
|
||||
|
||||
// Crate menu
|
||||
sp_menu = new_menu(sp_items);
|
||||
|
||||
// Create the window to be associated with the menu
|
||||
sp_menu_win = newwin(10, 40, begin_y, begin_x);
|
||||
keypad(sp_menu_win, TRUE);
|
||||
|
||||
// Set main window and sub window
|
||||
set_menu_win(sp_menu, sp_menu_win);
|
||||
set_menu_sub(sp_menu, derwin(sp_menu_win, 6, 38, begin_y - 1, begin_x - 3));
|
||||
|
||||
// Set menu mark to the string " * "
|
||||
set_menu_mark(sp_menu, " * ");
|
||||
|
||||
// Print a border around the main window and print a title
|
||||
box(sp_menu_win, 0, 0);
|
||||
print_in_middle(sp_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
|
||||
mvwaddch(sp_menu_win, 2, 0, ACS_LTEE);
|
||||
mvwhline(sp_menu_win, 2, 1, ACS_HLINE, 38);
|
||||
mvwaddch(sp_menu_win, 2, 39, ACS_RTEE);
|
||||
mvprintw(LINES - 2, 0, "F1 to exit");
|
||||
refresh();
|
||||
|
||||
// Post the menu
|
||||
post_menu(sp_menu);
|
||||
wrefresh(sp_menu_win);
|
||||
|
||||
while ((c = wgetch(sp_menu_win)) != 10) {
|
||||
switch (c) {
|
||||
case KEY_DOWN:
|
||||
menu_driver(sp_menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
case KEY_UP:
|
||||
menu_driver(sp_menu, REQ_UP_ITEM);
|
||||
break;
|
||||
}
|
||||
wrefresh(sp_menu_win);
|
||||
}
|
||||
size_t selected = item_index(current_item(sp_menu));
|
||||
|
||||
// Unpost and free all the memory taken up
|
||||
unpost_menu(sp_menu);
|
||||
free_menu(sp_menu);
|
||||
for (i = 0; i < n_choices; ++i)
|
||||
free_item(sp_items[i]);
|
||||
delete[] sp_items;
|
||||
|
||||
return selected;
|
||||
}
|
||||
*/
|
||||
|
||||
void print_in_middle(WINDOW* win, int starty, int startx, int width,
|
||||
const char* string, chtype color) {
|
||||
|
49
src/menu.cpp
49
src/menu.cpp
@ -17,26 +17,15 @@
|
||||
#include "const.h"
|
||||
#include "cups.h"
|
||||
#include "editor_easy.h"
|
||||
#include "editor_hard.h"
|
||||
#include "gameske_funkce.h"
|
||||
#include "memory.h"
|
||||
#include "signal.h"
|
||||
#include "strings.h"
|
||||
#include "types.h"
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
std::vector<allocation> main_menu_allocated;
|
||||
#define COMPLAINTS_DIR "complaints"
|
||||
|
||||
std::array<std::string, 2> get_name_date_from_item(ITEM* item) {
|
||||
std::stringstream ss(item_description(item));
|
||||
std::array<std::string, 2> name_date;
|
||||
name_date[0] = item_name(item);
|
||||
std::getline(ss, name_date[1], ' ');
|
||||
return name_date;
|
||||
}
|
||||
|
||||
void menu() {
|
||||
current_allocated = &main_menu_allocated;
|
||||
/* Initialize curses */
|
||||
@ -176,37 +165,6 @@ void menu() {
|
||||
|
||||
break;
|
||||
}
|
||||
case "eh"_sh: {
|
||||
std::array<std::string, 2> name_date =
|
||||
get_name_date_from_item(current_item(main_menu.menu));
|
||||
std::ifstream selected_file(COMPLAINTS_DIR "/" + name_date[0] +
|
||||
'_' + name_date[1]);
|
||||
if (!selected_file.is_open()) {
|
||||
std::clog << selected_file.rdstate() << "\n";
|
||||
std::cerr << RED "[ERROR] " RESET << loc_strings->invalid_input
|
||||
<< "\n"
|
||||
<< "File: "
|
||||
<< COMPLAINTS_DIR "/" + name_date[0] + '_' +
|
||||
name_date[1]
|
||||
<< "\n";
|
||||
exit(EINVAL);
|
||||
}
|
||||
|
||||
json selected_file_json = json::parse(selected_file);
|
||||
selected_file.close();
|
||||
std::string complaint_text =
|
||||
selected_file_json["complaint_text"].get<std::string>();
|
||||
editor_hard(complaint_text);
|
||||
curs_set(0);
|
||||
clear();
|
||||
selected_file_json["complaint_text"] = complaint_text;
|
||||
std::ofstream selected_file_out(COMPLAINTS_DIR "/" + name_date[0] +
|
||||
'_' + name_date[1]);
|
||||
selected_file_out << selected_file_json;
|
||||
selected_file_out.close();
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
print_in_middle(main_menu.win, 10, 0, 40,
|
||||
loc_strings->unknown_command, COLOR_PAIR(1));
|
||||
@ -214,8 +172,11 @@ void menu() {
|
||||
}
|
||||
break;
|
||||
case 'm': {
|
||||
std::array<std::string, 2> name_date =
|
||||
get_name_date_from_item(current_item(main_menu.menu));
|
||||
std::stringstream item_desc_ss(
|
||||
item_description(current_item(main_menu.menu)));
|
||||
std::string name_date[2];
|
||||
name_date[0] = item_name(current_item(main_menu.menu));
|
||||
std::getline(item_desc_ss, name_date[1], ' ');
|
||||
|
||||
{
|
||||
uint8_t n = 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user