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 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sergeykhbr
/**
2
 * @file
3
 * @copyright  Copyright 2016 GNSS Sensor Ltd. All right reserved.
4
 * @author     Sergey Khabarov - sergeykhbr@gmail.com
5
 * @brief      Auto Completer implementation.
6
 */
7
 
8
#include <string.h>
9
#include "autocompleter.h"
10
#include "coreservices/ithread.h"
11
 
12
namespace debugger {
13
 
14
/** Class registration in the Core */
15
REGISTER_CLASS(AutoCompleter)
16
 
17
AutoCompleter::AutoCompleter(const char *name)
18
    : IService(name) {
19
    registerInterface(static_cast<IAutoComplete *>(this));
20
    registerAttribute("SocInfo", &socInfo_);
21
    registerAttribute("History", &history_);
22
    registerAttribute("HistorySize", &history_size_);
23
 
24
    socInfo_.make_string("");
25
    history_.make_list(0);
26
    history_size_.make_int64(4);
27
    history_idx_ = 0;
28
 
29
    cmdLine_ = "";
30
    carretPos_ = 0;
31
}
32
 
33
AutoCompleter::~AutoCompleter() {
34
}
35
 
36
void AutoCompleter::postinitService() {
37
    info_ = static_cast<ISocInfo *>
38
            (RISCV_get_service_iface(socInfo_.to_string(), IFACE_SOC_INFO));
39
    history_idx_ = history_.size();
40
}
41
 
42
bool AutoCompleter::processKey(uint32_t qt_key,
43
                                AttributeType *cmd,
44
                                AttributeType *cursor) {
45
    bool add_symbol = false;
46
    bool isNewLine = false;
47
    bool set_history_end = true;
48
    if (!cursor->is_list() || cursor->size() != 2) {
49
        cursor->make_list(2);
50
        (*cursor)[0u].make_int64(0);
51
        (*cursor)[1].make_int64(0);
52
    }
53
    switch (qt_key) {
54
    case KB_Up:
55
        set_history_end = false;
56
        if (history_idx_ == history_.size()) {
57
            unfinshedLine_ = cmdLine_;
58
        }
59
        if (history_idx_ > 0) {
60
            history_idx_--;
61
        }
62
        cmdLine_ = std::string(history_[history_idx_].to_string());
63
        break;
64
    case KB_Down:
65
        set_history_end = false;
66
        if (history_idx_ == (history_.size() - 1)) {
67
            history_idx_++;
68
            cmdLine_ = unfinshedLine_;
69
        } else if (history_idx_ < (history_.size() - 1)) {
70
            history_idx_++;
71
            cmdLine_ = std::string(history_[history_idx_].to_string());
72
        }
73
        break;
74
    case KB_Left:
75
        if (carretPos_ < cmdLine_.size()) {
76
            carretPos_++;
77
        }
78
        break;
79
    case KB_Right:
80
        if (carretPos_ > 0) {
81
            carretPos_--;
82
        }
83
        break;
84
    case KB_Backspace:// 1. Backspace button:
85
        if (cmdLine_.size() && carretPos_ == 0) {
86
            cmdLine_.erase(cmdLine_.size() - 1);
87
        } else if (cmdLine_.size() && carretPos_ < cmdLine_.size()) {
88
            std::string t1 = cmdLine_.substr(0, cmdLine_.size() - carretPos_ - 1);
89
            cmdLine_ = t1 + cmdLine_.substr(cmdLine_.size() - carretPos_, carretPos_);
90
        }
91
        break;
92
    case KB_Return:// 2. Enter button:
93
        isNewLine = true;
94
        break;
95
    case 0:
96
        break;
97
    case KB_Shift:
98
    case KB_Control:
99
    case KB_Alt:
100
        break;
101
    case KB_Dot:
102
        qt_key = '.';
103
        add_symbol = true;
104
        break;
105
    default:
106
        add_symbol = true;
107
    }
108
 
109
    if (add_symbol) {
110
        if (carretPos_ == 0) {
111
            cmdLine_ += static_cast<uint8_t>(qt_key);
112
        } else if (carretPos_ == cmdLine_.size()) {
113
            std::string t1;
114
            t1 += static_cast<uint8_t>(qt_key);
115
            cmdLine_ = t1 + cmdLine_;
116
        } else {
117
            std::string t1 = cmdLine_.substr(0, cmdLine_.size() - carretPos_);
118
            t1 += static_cast<uint8_t>(qt_key);
119
            cmdLine_ = t1 + cmdLine_.substr(cmdLine_.size() - carretPos_, carretPos_);
120
        }
121
    }
122
 
123
    cmd->make_string(cmdLine_.c_str());
124
 
125
    if (isNewLine) {
126
        if (cmdLine_.size()) {
127
            addToHistory(cmdLine_.c_str());
128
            cmdLine_.clear();
129
        }
130
        carretPos_ = 0;
131
    }
132
    (*cursor)[0u].make_int64(carretPos_);
133
    (*cursor)[1].make_int64(carretPos_);
134
 
135
    if (set_history_end) {
136
        history_idx_ = history_.size();
137
    }
138
    return isNewLine;
139
}
140
 
141
void AutoCompleter::addToHistory(const char *cmd) {
142
    unsigned found = history_.size();
143
    for (unsigned i = 0; i < history_.size(); i++) {
144
        if (strcmp(cmd, history_[i].to_string()) == 0) {
145
            found = i;
146
            break;
147
        }
148
    }
149
    if (found  ==  history_.size()) {
150
        AttributeType t1;
151
        t1.make_string(cmd);
152
        history_.add_to_list(&t1);
153
 
154
        unsigned min_size = static_cast<unsigned>(history_size_.to_int64());
155
        if (history_.size() >= 2*min_size) {
156
            history_.trim_list(0, min_size);
157
        }
158
    } else if (found < (history_.size() - 1)) {
159
        history_.swap_list_item(found, history_.size() - 1);
160
    }
161
    history_idx_ = history_.size();
162
}
163
 
164
}  // namespace debugger

powered by: WebSVN 2.1.0

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