- C24zhuchengyu's blog
terminal
- @ 2024-12-24 13:57:33
一些关于控制台的函数
当前版本:1.02
// terminal 1.02
#ifdef _WIN32
#include<windows.h>
#endif
#include<utility>
#include<stdio.h>
#include<string>
namespace terminal{
using namespace std;
#ifdef _WIN32
HANDLE hd;
#else
#include<termios.h>
#include<unistd.h>
#include<sys/ioctl.h>
struct termios orig_termios;
#endif
bool isKeyPress(int name){
#ifdef _WIN32
return (GetAsyncKeyState(name)&0x8000)?1:0;
#else
struct termios new_termios;
tcgetattr(STDIN_FILENO,&orig_termios);
new_termios=orig_termios;
new_termios.c_lflag&=~(ICANON|ECHO);
tcsetattr(STDIN_FILENO,TCSANOW,&new_termios);
fd_set setx;
FD_ZERO(&setx);
FD_SET(STDIN_FILENO, &setx);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
bool result=(select(STDIN_FILENO+1,&set,NULL,NULL,&timeout)>0);
tcsetattr(STDIN_FILENO,TCSANOW,&orig_termios);
return result;
#endif
}
void setTPos(int l,int c){
#ifdef _WIN32
COORD p;
p.X=c,p.Y=l;
hd=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hd,p);
#else
printf("\033[%d;%dH",l+1,c+1);
#endif
}
pair<int, int> getTPos() {
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO p;
GetConsoleScreenBufferInfo(hd, &p);
return {p.dwCursorPosition.X, p.dwCursorPosition.Y};
#else
// Linux/macOS implementation would require more complex handling
// This is a placeholder - actual implementation would need terminal-specific codes
return {0,0};
#endif
}
void setColor(int c1,int c2){
#ifdef _WIN32
WORD cl=((c1&0x0f)<<4)+(c2&0x0f);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),cl);
#else
const char*fg_colors[]={"30","34","32","36","31","35","33","37","90","94","92","96","91","95","93","97"};
const char* bg_colors[] = {"40","44","42","46","41","45","43","47","100","104","102","106","101","105","103","107"};
printf("\033[%s;%sm",fg_colors[c2&0x0f],bg_colors[c1&0x0f]);
#endif
}
void setTextColor(short r,short g,short b){
printf("\033[38;2;%hd;%hd;%hdm",r,g,b);
}
void setBGColor(short r,short g,short b){
printf("\033[48;2;%hd;%hd;%hdm",r,g,b);
}
void setWindowsSize(int l,int c){
#ifdef _WIN32
string str="mode con lines="+to_string(l)+" cols="+to_string(c);
system(str.c_str());
#else
printf("\033[8;%d;%dt",l,c);
#endif
}
pair<int,int>getMPos(){
#ifdef _WIN32
HANDLE hd=GetStdHandle(STD_OUTPUT_HANDLE);
HWND h=GetForegroundWindow();
CONSOLE_FONT_INFO fs;
pair<int,int>pos;
POINT pt;
GetCursorPos(&pt);
ScreenToClient(h,&pt);
GetCurrentConsoleFont(hd,FALSE,&fs);
pos.first=pt.x/fs.dwFontSize.X;
pos.second=pt.y/fs.dwFontSize.Y;
return pos;
#else
// Linux/macOS - mouse position not directly available
return {0,0};
#endif
}
void hideCursor(){
#ifdef _WIN32
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO ci;
GetConsoleCursorInfo(h,&ci);
ci.bVisible=false;
SetConsoleCursorInfo(h,&ci);
#else
printf("\033[?25l");
#endif
}
void showCursor(){
#ifdef _WIN32
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO ci;
GetConsoleCursorInfo(h,&ci);
ci.bVisible=1;
SetConsoleCursorInfo(h,&ci);
#else
printf("\033[?25h");
#endif
}
void flsChoose(){
#ifdef _WIN32
auto si=GetStdHandle(STD_INPUT_HANDLE);
DWORD md;
GetConsoleMode(si,&md);
md&=~ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(si,md);
#else
// Not applicable to Linux/macOS
#endif
}
}