Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teeny notepad (console based app) #321

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions teeny.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#include <iostream>
#include<conio.h>
#include "Teeny.h"
using namespace std;

//Skeleton of class LinkedText
class LinkedText{
struct Node{
char ch;
Node * next;
Node * prev;
} *head, *tail, *cursorPos;

int textSize;

public:
LinkedText(){
head=tail=cursorPos=NULL;
textSize=0;
}
//some suggested methods
//All these methods (except copying) are O(1) thanks to the linked list!
void addCharAtCursorPos(char input)//when char entered
{
if (textSize == 0)
{
head = new Node;
head->ch = input;
head->next = nullptr;
tail = head;
cursorPos = head;
textSize++;
head->prev = nullptr;
}
else if (textSize > 0)
{
Node*temp = new Node[1];
temp->ch = input;
if (cursorPos == tail)
{
Node*temp1 = tail;
tail = temp;
temp->next = nullptr;
temp->prev = temp1;
temp1->next = temp;
cursorPos = temp;
textSize++;
}
else if (cursorPos == head)
{
head->next = temp;
temp->next = nullptr;
temp->prev = head;
cursorPos = temp;
tail = temp;
textSize++;
}
else
{
temp->prev = cursorPos;
temp->next= cursorPos->next;
cursorPos = temp;
textSize++;
}
}

}
void delCharAtCursorPos()//when bksp pressed
{
if (cursorPos == head)
{
head = cursorPos->next;
textSize--;
if (textSize == 0)
head = tail = cursorPos = nullptr;
}
else if(cursorPos->next==nullptr)
{
Node*temp1= cursorPos->prev;
tail = temp1;
cursorPos = temp1;
temp1->next = nullptr;
textSize--;
}
else
{
Node*temp1 = cursorPos->prev;
temp1->next = cursorPos->next;
cursorPos = temp1;
textSize--;
}


}
void moveCursorRight();//right arrow
void moveCursorLeft();//left arrow
/*the following method returns the sub-list after cutting,
and also adjusts cursor position*/
Node * cutTextBetween(Node * cursorpos1, Node * cursorpos2);
/*the following method returns the duplicated sub-list after copying,
and also adjusts cursor position*/
Node * copyTextBetween(Node * cursorpos1, Node * cursorpos2);
void insertTextAtCursorPos(Node * subList);

/*Method returns the number of times key occurs
in LinkedText*/
int findInText(const string & key);

//You may add appropriate printing methods and other methods below
~LinkedText(){//add code
};//delete the list


/*The methods below will be used to keep
the screen cursor inside the text area
only.
*/
inline bool textToLeft(){
return (cursorPos!=NULL && cursorPos->prev!=NULL);
}


inline bool textToRight(){
return (cursorPos!=NULL);
}

};

int main()
{
GetConsoleWindowDims();
/*This starter code is capable of detecting the
left and right arrow keys; the backspace key;
the escape key (for quit); the F1, F2 and F3 keys;
and the regular text keys.
Written for CS-218 "Data Structures" BCS-3A, FAST-NU, Lahore.
*/
LinkedText text;
int key=-9;
while(key!=ESCAPE){
key=_getch();
//either an arrow or function key is pressed
if(SPECIAL_KEY(key)){
key=_getch();//get 2nd part of code from buffer
switch(key){
case ARROW_LEFT:
//if(text.textToLeft()){
moveScreenCursorLeft();
//text.moveCursorLeft();
//}
break;
case ARROW_RIGHT:
//if (text.textToRight()){
moveScreenCursorRight();
//text.moveCursorRight();
//}
break;
case F1://start or end text selection
cout<<"[F1]";
break;
case F2://cut option
cout<<"[F2]";
break;
case F3://copy option
cout<<"[F3]";
break;
case F5://for search
cout<<"[F5]";
break;
case INSERT://to insert text
cout<<"[INSERT]";
break;
};
}else if(key==BACKSPACE){
cout<<"[BKSP]";
}else{
cout<<char(key);
// text.addCharAtCursorPos(key);
// cin.ignore(1);
}
}
return 0;
}
90 changes: 90 additions & 0 deletions teeny.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#ifndef TEENY_H_INCLUDED
#define TEENY_H_INCLUDED
#include <windows.h>
#include <conio.h>

//some macros for readability
#define ESCAPE 27
#define ARROW_LEFT 75
#define ARROW_RIGHT 77
#define BACKSPACE 8
#define INSERT 82
#define F1 59
#define F2 60
#define F3 61
#define F5 63
#define SPECIAL_KEY(x) (x==0 || x==0xE0)

//Global variable for screen dimensions
COORD dims;

/*Note: the following method allows you to write a writer a character
anywhere on the screen. It moves the screen cursor to the same position as well.
This method can make it easier for you to color single characters during
printing. You can also choose not to use it and work with cout.*/
void gotoxy(int xpos, int ypos, int col, char ch)
{
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = xpos; scrn.Y = ypos;
SetConsoleCursorPosition(hOuput,scrn);
HANDLE screen=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(screen,col);
DWORD useless;
WriteConsole(screen,&ch,1,(LPDWORD)&useless,NULL);
SetConsoleTextAttribute(screen,15);
SMALL_RECT windowSize = {0, 0, 100, 100};
SetConsoleWindowInfo(screen, TRUE, &windowSize);
}

COORD GetConsoleWindowDims(){
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
SMALL_RECT srctWindow;
GetConsoleScreenBufferInfo(hStdout, &csbiInfo);
srctWindow = csbiInfo.srWindow;
dims.X=srctWindow.Right;
dims.Y=srctWindow.Bottom;
return dims;
}

COORD GetConsoleCursorPosition(HANDLE hConsoleOutput)
{
CONSOLE_SCREEN_BUFFER_INFO cbsi;
if (GetConsoleScreenBufferInfo(hConsoleOutput, &cbsi))
{
return cbsi.dwCursorPosition;
}
else
{
// The function failed. Call GetLastError() for details.
COORD invalid = { 0, 0 };
return invalid;
}
}

void moveScreenCursorLeft(){
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = GetConsoleCursorPosition(hOuput);
if(pos.X>0)
pos.X--;
else if(pos.Y>0){
pos.Y--;
pos.X=dims.X;
}
SetConsoleCursorPosition(hOuput,pos);
}

void moveScreenCursorRight(){
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = GetConsoleCursorPosition(hOuput);
if(pos.X<dims.X)
pos.X++;
else if(pos.Y<dims.Y){
pos.Y++;
pos.X=0;
}
SetConsoleCursorPosition(hOuput,pos);
}

#endif // TEENY_H_INCLUDED