Merge branch 'dev' into 'master'

Sprint - Unit Tests sind cool

See merge request link/projekte/ws19/vkvm-new/terminal!2
This commit is contained in:
Julian Hinxlage 2020-01-09 06:52:59 +00:00
commit 6e85e1cb17
11 changed files with 637 additions and 7 deletions

32
.ci/clang-tidy.sh Normal file
View File

@ -0,0 +1,32 @@
#!/bin/sh
echo "Doing clang-tidy..."
bool=false
# explicitly set IFS to contain only a line feed
IFS='
'
filelist="$(find . -not \( -path './*build*' -prune \) -not \( -path './include' -prune \) -not \( -path './lib' -prune \) -type f ! -name "$(printf "*\n*")")"
for file in $filelist; do
if echo "$file" | grep -q -E ".*(\.cpp|\.h|\.hpp)$" ; then
#Extra check missing dependencies due to clang-tidy doesn't toggle exit code.
clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' "$file" -- -I. -std=c++14 2>&1)"
for tidy_line in $clang_tidy_lib_check; do
echo "$tidy_line" | grep -q -v -E "^Error while processing*"
if [ $? -eq 1 ]; then
bool=true
fi
echo "$tidy_line" | grep -q -v -E ".* error: .*"
if [ $? -eq 1 ]; then
bool=true
fi
echo "$tidy_line"
done
fi
done
if $bool; then
exit 1
else
echo "No clang-tidy errors found."
fi
exit 0

2
.clang-tidy Normal file
View File

@ -0,0 +1,2 @@
Checks: '*,-llvm-header-guard,-fuchsia*,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init,-google-readability-namespace-comments,-llvm-namespace-comment,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg,-modernize-use-trailing-return-type,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers'
WarningsAsErrors: 'true'

77
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,77 @@
---
image: samueldebruyn/debian-git:latest
stages:
- style
- test
- build
clang_tidy:
image: joethei/clang_tidy
stage: style
tags:
- docker-ci
script:
- mkdir current
- ls -d .[!.]* | grep -v current | xargs mv -t current
- git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.repo.digitech.hs-emden-leer.de/link/projekte/ws19/vkvm-new/library.git
- mkdir library/build
- cd library/build
- cmake ..
- make
- cd ../../current/.ci
- sh clang-tidy.sh
make_test:
stage: test
tags:
- docker-ci
script:
- apt-get update
- apt-get install -y clang make cmake clang-tidy
- mkdir current
- ls | grep -v current | xargs mv -t current
- git clone https://github.com/catchorg/Catch2.git
- cd Catch2
- cmake -Bbuild -H. -DBUILD_TESTING=OFF
- cmake --build build/ --target install
- cd ..
- git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.repo.digitech.hs-emden-leer.de/link/projekte/ws19/vkvm-new/library.git
- mkdir library/build
- cd library/build
- cmake ..
- make
- cd ../../current
- mkdir build
- cd build
- cmake ..
- make
- make test
cmake_build:
stage: build
tags:
- docker-ci
script:
- apt-get update
- apt-get install -y clang make cmake clang-tidy
- mkdir current
- ls | grep -v current | xargs mv -t current
- git clone https://github.com/catchorg/Catch2.git
- cd Catch2
- cmake -Bbuild -H. -DBUILD_TESTING=OFF
- cmake --build build/ --target install
- cd ..
- git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.repo.digitech.hs-emden-leer.de/link/projekte/ws19/vkvm-new/library.git
- mkdir library/build
- cd library/build
- cmake ..
- make
- cd ../../current
- mkdir build
- cd build
- cmake ..
- make

View File

@ -8,16 +8,21 @@ if ("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
endif()
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "bin" "doc" "CMakeFiles" "lib" "include")
set(CMAKE_C_COMPILER "clang")
set(CMAKE_CXX_COMPILER "clang++")
project(Terminal)
set(CMAKE_CXX_STANDARD 14)
# enable clang_tidy
set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=*")
set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;-checks=*;)
set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;)
file(GLOB_RECURSE SOURCES src/*.cpp)
file(GLOB_RECURSE HEADERS src/*.h)
file(GLOB_RECURSE HEADERS src/*.hpp)
file(GLOB_RECURSE TESTS test/*.cpp)
include_directories(src)
include_directories(test)
set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library")

View File

@ -1,5 +1,17 @@
# vKVM Terminal
Terminal calls the methods from library in order to get data from shared memory
and control the output content in GUI.
## Features
- If all the lines of the screen are occupied and a new line is added, all lines
will be moved up.
- Terminal can handle the backspace.
- If a line is too long for the screen, terminal will word wrap.
- A cursor can be simulated to insert text anywhere.
- Terminal can use the arrow keys to move the cursor.
- The input from terminal will be wise selected.
## Installation
Use the installation script provided in the Scripts repository

View File

@ -1,5 +1,143 @@
#include "../src/demo.h"
#include "../src/Terminal.hpp"
#include "internal.hpp"
#include "vkvm.hpp"
#include <iostream>
#include <unistd.h>
int main() {
return test();
vkvm::initialize(0);
Terminal terminal = Terminal();
terminal.init();
std::cout << terminal.getString() << std::endl;
vkvm::registerEvent(vkvm::KeyDown, [&terminal]() {
vkvm::KeyCode keycode = vkvm::getLastPressedKey();
std::cout << keycode << std::endl;
if((vkvm::KeyCode::ShiftLeft == keycode) ||(vkvm::KeyCode::ShiftRight == keycode)){
terminal.shiftpressed();
}
if((vkvm::KeyCode::Zero <= keycode) && (keycode <=vkvm::Nine)){
char ch = keycode - vkvm::KeyCode::Zero + '0';
std::cout << ch << std::endl;
terminal.setString(ch);
vkvm::setText(terminal.getString());
std::cout << terminal.getString() << std::endl;
std::cout<< vkvm::getText()<< std::endl;
vkvm::callEvent(vkvm::EventType::RenderText);
}
if((vkvm::KeyCode::A <= keycode) && (keycode <= vkvm::KeyCode::Z)){
int status = terminal.getstatus();
char ch;
if(status == 0){
ch = keycode - vkvm::KeyCode::A + 'a';
}
else{
ch = keycode - vkvm::KeyCode::A + 'A';
}
std::cout << ch << std::endl;
terminal.setString(ch);
vkvm::setText(terminal.getString());
std::cout << terminal.getString() << std::endl;
std::cout<< vkvm::getText()<< std::endl;
vkvm::callEvent(vkvm::EventType::RenderText);
}
if(vkvm::KeyCode::Space == keycode){
char ch = keycode - vkvm::KeyCode::Space + ' ';
std::cout << ch << std::endl;
terminal.setString(ch);
vkvm::setText(terminal.getString());
std::cout << terminal.getString() << std::endl;
std::cout<< vkvm::getText()<< std::endl;
vkvm::callEvent(vkvm::EventType::RenderText);
}
if(vkvm::KeyCode::Backspace == keycode){
char ch = keycode - vkvm::KeyCode::Backspace + ' ';
std::cout << ch << std::endl;
terminal.subString();
vkvm::setText(terminal.getString());
std::cout << terminal.getString() << std::endl;
std::cout<< vkvm::getText()<< std::endl;
vkvm::callEvent(vkvm::EventType::RenderText);
}
if(vkvm::KeyCode::Tab == keycode){
char ch = keycode - vkvm::KeyCode::Tab + ' ';
for(int i = 0; i < 3; i++) {
std::cout << ch << std::endl;
terminal.setString(ch);
}
vkvm::setText(terminal.getString());
std::cout << terminal.getString() << std::endl;
std::cout<< vkvm::getText()<< std::endl;
vkvm::callEvent(vkvm::EventType::RenderText);
}
if(vkvm::KeyCode::Enter == keycode){
char ch = '\n';
std::cout << ch << std::endl;
terminal.setString(ch);
vkvm::setText(terminal.getString());
std::cout << terminal.getString() << std::endl;
std::cout<< vkvm::getText()<< std::endl;
vkvm::callEvent(vkvm::EventType::RenderText);
}
if(vkvm::KeyCode::Arrow_Left == keycode){
terminal.moveleft();
std::cout << terminal.getString() << std::endl;
vkvm::setText(terminal.getString());
vkvm::callEvent(vkvm::EventType::RenderText);
}
if(vkvm::KeyCode::Arrow_Right == keycode){
terminal.moveright();
std::cout << terminal.getString() << std::endl;
vkvm::setText(terminal.getString());
vkvm::callEvent(vkvm::EventType::RenderText);
}
if(vkvm::KeyCode::Arrow_Down == keycode){
terminal.movedown();
std::cout << terminal.getString() << std::endl;
vkvm::setText(terminal.getString());
vkvm::callEvent(vkvm::EventType::RenderText);
}
if(vkvm::KeyCode::Arrow_Up == keycode){
terminal.moveup();
std::cout << terminal.getString() << std::endl;
vkvm::setText(terminal.getString());
vkvm::callEvent(vkvm::EventType::RenderText);
}
});
vkvm::registerEvent(vkvm::KeyUp, [&terminal]() {
vkvm::KeyCode keycode = vkvm::getLastPressedKey();
if(((vkvm::KeyCode::ShiftLeft) == keycode) ||((vkvm::KeyCode::ShiftRight) == keycode)){
terminal.shiftup();
}
// char ch = keycode - (vkvm::KeyCode::A & 0xfff) + 'A';
// std::cout << ch << std::endl;
// terminal.setString(ch);
// vkvm::setText(terminal.getString());
// std::cout<< vkvm::getText()<< std::endl;
// vkvm::callEvent(vkvm::EventType::RenderText);
});
while(1){
sleep(0.1);
}
}

340
src/Terminal.cpp Normal file
View File

@ -0,0 +1,340 @@
#include <iostream>
#include "Terminal.hpp"
std::string Terminal::getString() {
return s;
}
void Terminal::init(){
s = s + cursor;
}
void Terminal::setString(char c) {
// s.erase(s.end() - 1);
// s = s + c + cursor;
int i = s.find(cursor);
std::cout << i << std::endl;
std::cout<< s.length() -1 <<std::endl;
if(i < s.length() -1 ){
s= s + s[s.length() - 1];
int l = s.length() - 1;
for(; l > i + 1; l--){
s[l] = s[l-1];
}
s[i+1] = c;
}
else{
s = s + cursor;
s[s.length()-2] = c;
}
}
void Terminal::subString(){
// if(s.length() > 1)
// s.erase(s.end() - 1);
// s.erase(s.end() - 1);
// s = s + cursor;
if(s.length() > 1) {
int i = s.find(cursor);
s.erase(i - 1, 1);
}
}
void Terminal::shiftpressed() {
status = 1;
}
void Terminal::shiftup() {
status = 0;
}
int Terminal::getstatus() {
return status;
}
void Terminal::moveleft(){
int i = s.find(cursor);
if(i > 0){
char c = s[i-1];
s[i-1] = cursor;
s[i] = c;
}
}
void Terminal::moveright() {
int i = s.find(cursor);
if(i < (s.length() - 1)){
char c = s[i+1];
s[i+1] = cursor;
s[i] = c;
}
}
void Terminal::movedown() {
// std::string news="";
// int i = s.find(cursor);
// s.erase(i, 1);
//
// std::vector<std::vector<char>> vec(100,std::vector<char>(21,0));
// for(int n = 0; n < 100; n++){
// if(n*21 < s.length()) {
// for (int m = 0; m < 21; m++) {
// int sum = n * 21 + m;
// if (s[sum] != '\n') {
// vec[n][m] = s[sum];
// std::cout<<"vec before" + vec[n][m]<<std::endl;
// }
// else {
// vec[n][m] = s[sum];
// break;
// }
// }
// }
// else {
// break;
// }
// }
//
// int cursorrow = i / 21;
// int cursorcolumn =i % 21;
// std::cout<<"before" + std::to_string(cursorrow) + ' '+ std::to_string(cursorcolumn)<<std::endl;
// if(vec[cursorrow+1].size() >= cursorcolumn){
// cursorrow++;
// }
// else if(vec[cursorrow+1].size() >=1){
// cursorrow++;
// cursorcolumn = vec[cursorrow].size()-1;
// }
// std::cout<<"after" + std::to_string(cursorrow) + ' '+ std::to_string(cursorcolumn)<<std::endl;
//
// for(int n = 0; n < 100; n++){
// for(int m=0;m<21;m++){
// if((n==cursorrow) && (m==cursorcolumn)){
// news = news + cursor;
// }
// else if(vec[n][m] != '\n'){
// std::cout<<"vec after" + vec[n][m]<<std::endl;
// news = news + vec[n][m];
// }
// else{
// news = news + vec[n][m];
// break;
// }
// }
// }
//
// std::cout<< news<<std::endl;
int count =0;
for(int i = 0; i < s.length(); i++) {
if (s[i] == '\n') {
count = 0;
}
else {
if(s[i]!=cursor)
count++;
}
if(count == 22){
s = s + s[s.length() - 1];
int x = s.length() - 1;
for(; x > i; x--){
s[x] = s[x-1];
}
s[x] = '\n';
count = 0;
}
}
std::cout<<"plus enter:" + s <<std::endl;
int cursorposition = s.find(cursor);
int aftercursorenter1 = s.find('\n',cursorposition);
int beforcursorenter = cursorposition - 1;
while (beforcursorenter >= 0){
if (s[beforcursorenter] != '\n') {
beforcursorenter--;
} else {
break;
}
}
std::cout<<"cursorpositon:" + std::to_string(cursorposition)<<std::endl;
std::cout<<"aftercursorenter1:" + std::to_string(aftercursorenter1)<<std::endl;
std::cout<<"beforecursorenter:" + std::to_string(beforcursorenter)<<std::endl;
if(aftercursorenter1 != s.npos) {
int aftercursorenter2 = s.find('\n', aftercursorenter1 + 1);
std::cout<<"aftercursorenter2:" + std::to_string(aftercursorenter2)<<std::endl;
if(aftercursorenter2 != s.npos){
std::cout<<"enter the loop & aftercursorenter2 !=s.npos" <<std::endl;
if((aftercursorenter2 - aftercursorenter1) >= (cursorposition - beforcursorenter)) {
for(int newcursorposition = cursorposition;newcursorposition < (aftercursorenter1 + cursorposition -beforcursorenter -1);newcursorposition++){
s[newcursorposition] = s[newcursorposition + 1];
}
s[aftercursorenter1 + cursorposition - beforcursorenter -1] = cursor;
}
else{
for(int newcursorposition = cursorposition;newcursorposition < (aftercursorenter2); newcursorposition++){
s[newcursorposition] = s[newcursorposition + 1];
}
s[aftercursorenter2] = cursor;
}
}
else{
std::cout<<"enter the loop & aftercursorenter2 ==s.npos" <<std::endl;
std::cout<<"elsecursorpositon:" + std::to_string(cursorposition)<<std::endl;
std::cout<<"elseaftercursorenter1:" + std::to_string(aftercursorenter1)<<std::endl;
std::cout<<"elsebeforecursorenter:" + std::to_string(beforcursorenter)<<std::endl;
if((s.length()- 1 - aftercursorenter1) >= (cursorposition - beforcursorenter)) {
std::cout<<"enter the loop if" <<std::endl;
for(int newcursorposition=cursorposition; newcursorposition < (aftercursorenter1 + cursorposition -beforcursorenter -1); newcursorposition++){
s[newcursorposition] = s[newcursorposition + 1];
std::cout<<"forcursorpositon:" + std::to_string(cursorposition)<<std::endl;
}
s[aftercursorenter1 + cursorposition - beforcursorenter -1] = cursor;
std::cout<<"new cursorposition" + std::to_string(aftercursorenter1 + cursorposition - beforcursorenter -1) <<std::endl;
std::cout<<"delect enter:" + s <<std::endl;
}
else{
std::cout<<"enter the loop else" <<std::endl;
for(int newcursorposition = cursorposition;newcursorposition < (s.length() -1); newcursorposition++){
s[newcursorposition] = s[newcursorposition + 1];
}
std::cout<<"new cursorposition" + std::to_string(s.length() - 1) <<std::endl;
s[s.length() - 1] = cursor;
}
}
}
std::cout<<"after down:" + s <<std::endl;
count = 0;
for(int i = 0; i < s.length(); i++) {
if(s[i] != cursor) {
count++;
}
if((count ==22)){
for(int n=i; n < s.length();n++){
s[n] = s[n+1];
}
s = s.substr(0,s.length()-1);
count =1;
}
}
std::cout<<"delect enter:" + s <<std::endl;
}
void Terminal::moveup() {
int count =0;
for(int i = 0; i < s.length(); i++) {
if (s[i] == '\n') {
count = 0;
}
else {
if(s[i]!=cursor)
count++;
}
if(count == 22){
s = s + s[s.length() - 1];
int x = s.length() - 1;
for(; x > i; x--){
s[x] = s[x-1];
}
s[x] = '\n';
count = 0;
}
}
std::cout<<"plus enter:" + s <<std::endl;
int cursorposition = s.find(cursor);
int beforecursorenter1 = cursorposition - 1;
while (beforecursorenter1 >= 0){
if (s[beforecursorenter1] != '\n') {
beforecursorenter1--;
} else {
break;
}
}
std::cout<<"cursorpositon:" + std::to_string(cursorposition)<<std::endl;
std::cout<<"beforecursorenter1:" + std::to_string(beforecursorenter1)<<std::endl;
if(beforecursorenter1 != -1) {
int beforecursorenter2 = beforecursorenter1 - 1;
while (beforecursorenter2 >= 0){
if (s[beforecursorenter2] != '\n') {
beforecursorenter2--;
} else {
break;
}
}
std::cout<<"beforecursorenter2:" + std::to_string(beforecursorenter2)<<std::endl;
if(beforecursorenter2 != -1){
std::cout<<"enter the loop & beforecursorenter2 !=-1" <<std::endl;
if((beforecursorenter1 - beforecursorenter2) >= (cursorposition - beforecursorenter1)) {
for(int newcursorposition = cursorposition;newcursorposition > (beforecursorenter2 + cursorposition - beforecursorenter1 );newcursorposition--){
s[newcursorposition] = s[newcursorposition - 1];
}
s[beforecursorenter2 + cursorposition - beforecursorenter1] = cursor;
}
else{
for(int newcursorposition = cursorposition;newcursorposition > (beforecursorenter1); newcursorposition--){
s[newcursorposition] = s[newcursorposition -1];
}
s[beforecursorenter1] = cursor;
}
}
else{
std::cout<<"enter the loop & aftercursorenter2 ==s.npos" <<std::endl;
std::cout<<"elsecursorpositon:" + std::to_string(cursorposition)<<std::endl;
std::cout<<"elsebeforecursorenter1:" + std::to_string(beforecursorenter1)<<std::endl;
std::cout<<"elsebeforecursorenter2:" + std::to_string(beforecursorenter2)<<std::endl;
if((beforecursorenter1 + 1) >= (cursorposition - beforecursorenter1)) {
std::cout<<"enter the loop if" <<std::endl;
for(int newcursorposition=cursorposition; newcursorposition > (cursorposition -beforecursorenter1 -1); newcursorposition--){
s[newcursorposition] = s[newcursorposition - 1];
std::cout<<"forcursorpositon:" + std::to_string(cursorposition)<<std::endl;
}
s[cursorposition -beforecursorenter1 -1] = cursor;
std::cout<<"new cursorposition" + std::to_string(cursorposition -beforecursorenter1 -1) <<std::endl;
std::cout<<"delect enter:" + s <<std::endl;
}
else{
std::cout<<"enter the loop else" <<std::endl;
for(int newcursorposition = cursorposition;newcursorposition > (beforecursorenter1); newcursorposition--){
s[newcursorposition] = s[newcursorposition - 1];
}
std::cout<<"new cursorposition" + std::to_string(beforecursorenter1) <<std::endl;
s[beforecursorenter1] = cursor;
}
}
}
std::cout<<"after up:" + s <<std::endl;
count = 0;
for(int i = 0; i < s.length(); i++) {
if(s[i] != cursor) {
count++;
}
if((count ==22)){
for(int n=i; n < s.length();n++){
s[n] = s[n+1];
}
s = s.substr(0,s.length()-1);
count =1;
}
}
std::cout<<"delect enter:" + s <<std::endl;
}

24
src/Terminal.hpp Normal file
View File

@ -0,0 +1,24 @@
#include <string>
#ifndef TERMINAL_TERMINAL_HPP
#define TERMINAL_TERMINAL_HPP
class Terminal {
public:
void init();
std::string getString();
void setString(char c);
void subString();
void shiftpressed();
void shiftup();
int getstatus();
void moveleft();
void moveright();
void moveup();
void movedown();
private:
std::string s="";
int status = 0;
char cursor = -127;
};
#endif //TERMINAL_TERMINAL_HPP

View File

@ -1,4 +1,4 @@
#include "demo.h"
#include "demo.hpp"
int test() {
return 42;

View File

@ -1,5 +1,5 @@
#include <catch2/catch.hpp>
#include "../src/demo.h"
#include "demo.hpp"
TEST_CASE("Demo test") {
REQUIRE(test() == 42);