OpenCores
URL https://opencores.org/ocsvn/riscv_vhdl/riscv_vhdl/trunk

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [debugger/] [src/] [libdbg64g/] [services/] [console/] [autocompleter.cpp] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 sergeykhbr
/*
2
 *  Copyright 2018 Sergey Khabarov, sergeykhbr@gmail.com
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
6
 *  You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *  Unless required by applicable law or agreed to in writing, software
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *  See the License for the specific language governing permissions and
14
 *  limitations under the License.
15 3 sergeykhbr
 */
16
 
17
#include <string.h>
18
#include "autocompleter.h"
19
#include "coreservices/ithread.h"
20
 
21
namespace debugger {
22
 
23
/** Class registration in the Core */
24
REGISTER_CLASS(AutoCompleter)
25
 
26
AutoCompleter::AutoCompleter(const char *name)
27
    : IService(name) {
28
    registerInterface(static_cast<IAutoComplete *>(this));
29
    registerAttribute("History", &history_);
30
    registerAttribute("HistorySize", &history_size_);
31 4 sergeykhbr
    registerAttribute("SocInfo", &socInfo_);
32 3 sergeykhbr
 
33
    socInfo_.make_string("");
34
    history_.make_list(0);
35
    history_size_.make_int64(4);
36
    history_idx_ = 0;
37
 
38
    cmdLine_ = "";
39
    carretPos_ = 0;
40
}
41
 
42
AutoCompleter::~AutoCompleter() {
43
}
44
 
45
void AutoCompleter::postinitService() {
46
    history_idx_ = history_.size();
47
}
48
 
49
bool AutoCompleter::processKey(uint32_t qt_key,
50
                                AttributeType *cmd,
51
                                AttributeType *cursor) {
52
    bool add_symbol = false;
53
    bool isNewLine = false;
54
    bool set_history_end = true;
55
    if (!cursor->is_list() || cursor->size() != 2) {
56
        cursor->make_list(2);
57
        (*cursor)[0u].make_int64(0);
58
        (*cursor)[1].make_int64(0);
59
    }
60
    switch (qt_key) {
61
    case KB_Up:
62
        set_history_end = false;
63
        if (history_idx_ == history_.size()) {
64
            unfinshedLine_ = cmdLine_;
65
        }
66
        if (history_idx_ > 0) {
67
            history_idx_--;
68
        }
69
        cmdLine_ = std::string(history_[history_idx_].to_string());
70
        break;
71
    case KB_Down:
72
        set_history_end = false;
73
        if (history_idx_ == (history_.size() - 1)) {
74
            history_idx_++;
75
            cmdLine_ = unfinshedLine_;
76
        } else if (history_idx_ < (history_.size() - 1)) {
77
            history_idx_++;
78
            cmdLine_ = std::string(history_[history_idx_].to_string());
79
        }
80
        break;
81
    case KB_Left:
82
        if (carretPos_ < cmdLine_.size()) {
83
            carretPos_++;
84
        }
85
        break;
86
    case KB_Right:
87
        if (carretPos_ > 0) {
88
            carretPos_--;
89
        }
90
        break;
91
    case KB_Backspace:// 1. Backspace button:
92
        if (cmdLine_.size() && carretPos_ == 0) {
93
            cmdLine_.erase(cmdLine_.size() - 1);
94
        } else if (cmdLine_.size() && carretPos_ < cmdLine_.size()) {
95
            std::string t1 = cmdLine_.substr(0, cmdLine_.size() - carretPos_ - 1);
96
            cmdLine_ = t1 + cmdLine_.substr(cmdLine_.size() - carretPos_, carretPos_);
97
        }
98
        break;
99
    case KB_Return:// 2. Enter button:
100
        isNewLine = true;
101
        break;
102
    case 0:
103
        break;
104
    case KB_Shift:
105
    case KB_Control:
106
    case KB_Alt:
107
        break;
108
    case KB_Dot:
109
        qt_key = '.';
110
        add_symbol = true;
111
        break;
112
    default:
113
        add_symbol = true;
114
    }
115
 
116
    if (add_symbol) {
117
        if (carretPos_ == 0) {
118
            cmdLine_ += static_cast<uint8_t>(qt_key);
119
        } else if (carretPos_ == cmdLine_.size()) {
120
            std::string t1;
121
            t1 += static_cast<uint8_t>(qt_key);
122
            cmdLine_ = t1 + cmdLine_;
123
        } else {
124
            std::string t1 = cmdLine_.substr(0, cmdLine_.size() - carretPos_);
125
            t1 += static_cast<uint8_t>(qt_key);
126
            cmdLine_ = t1 + cmdLine_.substr(cmdLine_.size() - carretPos_, carretPos_);
127
        }
128
    }
129
 
130
    cmd->make_string(cmdLine_.c_str());
131
 
132
    if (isNewLine) {
133
        if (cmdLine_.size()) {
134
            addToHistory(cmdLine_.c_str());
135
            cmdLine_.clear();
136
        }
137
        carretPos_ = 0;
138
    }
139
    (*cursor)[0u].make_int64(carretPos_);
140
    (*cursor)[1].make_int64(carretPos_);
141
 
142
    if (set_history_end) {
143
        history_idx_ = history_.size();
144
    }
145
    return isNewLine;
146
}
147
 
148
void AutoCompleter::addToHistory(const char *cmd) {
149
    unsigned found = history_.size();
150
    for (unsigned i = 0; i < history_.size(); i++) {
151
        if (strcmp(cmd, history_[i].to_string()) == 0) {
152
            found = i;
153
            break;
154
        }
155
    }
156
    if (found  ==  history_.size()) {
157
        AttributeType t1;
158
        t1.make_string(cmd);
159
        history_.add_to_list(&t1);
160
 
161
        unsigned min_size = static_cast<unsigned>(history_size_.to_int64());
162
        if (history_.size() >= 2*min_size) {
163
            history_.trim_list(0, min_size);
164
        }
165
    } else if (found < (history_.size() - 1)) {
166
        history_.swap_list_item(found, history_.size() - 1);
167
    }
168
    history_idx_ = history_.size();
169
}
170
 
171
}  // namespace debugger

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.