aboutsummaryrefslogtreecommitdiffstats
path: root/editor.cpp
blob: 08abdded5046837b7ad5803a340e409ca31a465c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "editor.h"
#include "utils.h"
#include <fstream>
#include <limits>
#include <iostream>

Editor::Editor(std::vector<std::string>* contents, const std::string& filename):
    m_current_line(0), m_filename(filename), m_stopped(false)  {

    m_contents = contents;
}

int Editor::run_editor() {
    std::vector<std::string>* c = m_contents;

    while (!m_stopped) {
        print_contents_on_line(m_current_line);
        
        char input = Utils::getch();
        switch (input) {
        case 'e':
            edit_command();
            break;
        case 'm':
            move_to_command();
            break;
        case 'w':
            write_command();
            break;
        case 'q':
            quit_command();
            break;
        default:
            break;
        }
    }
    return 0;
}

int Editor::print_contents_on_line(int line) const {
    int output_length = m_contents->size();
    bool truncated = false;
    if ((int) m_contents->size() > line + OUTPUT_LINE_MAX) {
        output_length = line + OUTPUT_LINE_MAX;
        truncated = true;
    }
    for (int i = line; i < std::min((int) m_contents->size(), line + OUTPUT_LINE_MAX); i++) {
        std::cout << i << " " << m_contents->at(i) << '\n';
    }
    if (truncated) {
        std::cout << "...\n";
    }
}

int Editor::edit_command() {
    std::cout << "Edit which line? ";
    int line;
    std::cin >> line; 
    if (std::cin.fail() || line < 0) {
        std::cout << "Not a valid line number!\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        return 1;
    }

    return edit_line(line);
}

int Editor::edit_line(int line) {

    if (line >= m_contents->size()) {
        m_contents->resize(line + 1, "");
    }

    std::cout << "> ";
    std::string input;

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    std::getline(std::cin, input);
    std::cin.clear();
    (*m_contents)[line] = input;

    return 0;
}

int Editor::move_to_command() {
    std::cout << "Move to which line? ";
    int line;
    std::cin >> line;
    if (std::cin.fail() || line < 0 || line >= m_contents->size()) {
        std::cout << "Not a valid line number!\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        return 1;
    }

    return move_to_line(line);
}

int Editor::move_to_line(int line) {
    m_current_line = line;
    return 0;
}

int Editor::write_command() {
    std::cout << "Write to file? [y/n] ";
    char answer = Utils::getch();

    if (answer == 'y') {
        return write();
    }

    return 0;
}

int Editor::write() {
    std::ofstream file;
    file.open(m_filename);
    if (file.fail()) {
        std::cout << "Can't open file " << m_filename << " for writing!";
        return 1;
    }

    for (int i = 0; i < m_contents->size(); i++) {
        file << m_contents->at(i) << '\n';
    }

    file.close();
    
    return 0;   
}

int Editor::quit_command() {
    std::cout << "Really quit? [y/n] ";
    char answer = Utils::getch();

    if (answer == 'y') {
        m_stopped = true;
        std::cout << "\n";
    }

    return 0;
}