/*
* =====================================================================================
*
* Filename: nana.c
*
* Description: nano structure
*
* Version: 1.0
* Created: 2012年10月20日 14时30分34秒
* Revision: none
* Compiler: gcc
*
* Author: mtawaken (gcs), mtawaken@gmail.com
* Company: NJU
*
* =====================================================================================
*/
#include<unistd.h>
#include<stdlib.h>
#include<curses.h>
#include<string.h>
#include<locale.h>
/*
* defines
*/
#define TOPWIN_H 2
#define BOTTOMWIN_H 3
#define EDITWIN_H (LINES-TOPWIN_H-BOTTOMWIN_H)
#define TMP_SIZE ((COLS+1)*3)//support only one line input at one time
typedef struct ScreenLine{
int lineNo;
int shift;
} ScreenLine;
typedef struct Func{
void (*func)();
int invokeKey;
struct Func* next;
} Func;
typedef struct Line{
char *content;
int contentFactor;
int lineNo;
int size;
int lines;
int lastCols;
struct Line *next;
struct Line *previous;
} Line;
typedef struct MCursor{
int x,y;
} MCursor;
/*
* methods
*/
int doInput();
void doOutput(int);
void functionInit();
Func* createFunction(int,void*,Func*);
void freeFunctionList(Func*);
void doExit();
void doDelete();
void cursorBackward();
void updateLine(Line*);
void reAllocLine(Line*);
void getPositionByYX(Line**,int*,int,int);
void cursorForward();
void cursorUp();
void cursorDown();
/*
* globals
*/
WINDOW *topScr,*bottomScr,*editScr;
char *tmp;
int *rawInput;
bool mExit;
ScreenLine screenLine;
Func *functionList;
Line *lineList;
bool newAppending;
bool needRefresh;
bool needAdjustCursor;
MCursor mCursor;
int main(int args,char * argv[]){
setlocale(LC_ALL,"");
initscr();
raw();
noecho();
start_color();
init_pair(1,COLOR_CYAN,COLOR_BLACK);
init_pair(2,COLOR_WHITE,COLOR_BLACK);
/*
* split windows
*/
topScr = newwin(TOPWIN_H,COLS,0,0);
editScr = newwin(EDITWIN_H,COLS,TOPWIN_H,0);
bottomScr = newwin(BOTTOMWIN_H,COLS,LINES-BOTTOMWIN_H,0);
leaveok(topScr,false);
leaveok(editScr,false);
leaveok(bottomScr,false);
keypad(topScr,TRUE);
keypad(editScr,TRUE);
keypad(bottomScr,TRUE);
scrollok(editScr,false);
scrollok(topScr,false);
scrollok(bottomScr,false);
wattrset(topScr,COLOR_PAIR(1));
wattrset(bottomScr,COLOR_PAIR(1));
wattrset(editScr,COLOR_PAIR(2));
attron(A_BOLD);
tmp = (char*)malloc(TMP_SIZE*sizeof(char));
memset(tmp,'-',TMP_SIZE);
tmp[COLS] = 0;
mvwprintw(topScr,TOPWIN_H-1,0,"%s",tmp);
mvwprintw(bottomScr,0,0,"%s",tmp);
mvwprintw(topScr,0,0,"%s","NANO");
mvwprintw(bottomScr,1,0,"%s","STATUS");
wrefresh(topScr);
wrefresh(bottomScr);
attroff(A_BOLD);
/*
* function initialization
*/
functionInit();
/*
* line initialization
*/
lineList = (Line*)malloc(sizeof(Line));
memset(lineList,0,sizeof(Line));
Line* tmpLine = (Line*)malloc(sizeof(Line));
memset(tmpLine,0,sizeof(Line));
reAllocLine(tmpLine);
tmpLine->lineNo = 1;
tmpLine->lines = 1;
tmpLine->previous = lineList;
lineList->next = tmpLine;
/*
* data initialization
*/
screenLine.lineNo = 1;
screenLine.shift = 0;
mExit = false;
rawInput = (int*)malloc(sizeof(int)*TMP_SIZE);
newAppending = true;
needRefresh = true;
needAdjustCursor = false;
/*
* main process
*/
move(TOPWIN_H+1,0);
//func-1,ctrl+x-1,character-1,chineses-3,byinputmethod-n
int size;
while(!mExit){
size = doInput();
doOutput(size);
}
wrefresh(editScr);
endwin();
return EXIT_SUCCESS;
}
void functionInit(){
functionList = (Func*)malloc(sizeof(Func));
memset(functionList,0,sizeof(Func));
Func *lastFunc;
lastFunc = createFunction(24,(void*)&doExit,functionList);
lastFunc = createFunction(263,(void*)&doDelete,lastFunc);
lastFunc = createFunction(260,(void*)&cursorBackward,lastFunc);
lastFunc = createFunction(261,(void*)&cursorForward,lastFunc);
lastFunc = createFunction(259,(void*)&cursorUp,lastFunc);
lastFunc = createFunction(258,(void*)&cursorDown,lastFunc);
}
Func* createFunction(int invokeKey,void* realFunction,Func *previous){
Func *currentFunc = (Func*)malloc(sizeof(Func));
memset(currentFunc,0,sizeof(Func));
currentFunc->func = realFunction;
currentFunc->invokeKey = invokeKey;
currentFunc->next = 0;
previous->next = currentFunc;
return currentFunc;
}
void freeFunctionList(Func*current){
if(!current)
return;
if(current->next){
freeFunctionList(current->next);
}
free(current);
}
void freeOneLine(Line* line){
if(line){
if(line->content){
free(line->content);
}
free(line);
}
}
void freeLines(){
Line*tempLine,*nextLine;
tempLine=lineList;
while(tempLine){
nextLine = tempLine->next;
freeOneLine(tempLine);
tempLine = nextLine;
}
}
void updateLineNoAfter(Line* line){
int lineNoUpdater = line->lineNo+1;
Line *tmpLine = line->next;
while(tmpLine){
tmpLine->lineNo = lineNoUpdater;
lineNoUpdater++;
tmpLine = tmpLine->next;
}
}
void jumpLineUnit(char *p,char** newP,int* jumpCol){
if(!*p){
*newP = p;
*jumpCol = 0;
return;
}
int mbLen,wcWidth;
mbLen = mblen(p,MB_CUR_MAX);
if(mbLen == 3){
//it's a chinese
*jumpCol = 2;
}else if(mbLen == 1){
if(*p == 9){
//it's a tab
*jumpCol = 8;
}else if(*p == 10){
*newP = 0;
}else{
wchar_t wChar;
mbtowc(&wChar,p,MB_CUR_MAX);
wcWidth = wcwidth(wChar);
if(wcWidth == 1){
//normal character
*jumpCol = 1;
}else{
//unprintable contorl letter
*jumpCol = 2;
}
}
}else{
*newP = 0;
}
*newP = p + mbLen;
}
void updateLine(Line* line){
char *p = line->content;
int colCount,jumpCol,size;
char *newP;
colCount = 0;
size = 0;
while(p&&*p){
jumpLineUnit(p,&newP,&jumpCol);
if(newP!=0){
colCount +=jumpCol;
size += (newP-p);
}
p = newP;
}
line->lines = (colCount/COLS)+1;
line->lastCols = colCount%COLS;
line->size = size;
}
void reAllocLine(Line* line){
int oldSize = (line->contentFactor) * TMP_SIZE;
line->contentFactor += 1;
int newSize = (line->contentFactor) * TMP_SIZE;
char *newContent = (char*)malloc(sizeof(char)*newSize);
memset(newContent,0,newSize);
char *p = line->content;
int index = 0;
while(index<oldSize && p[index]){
newContent[index] = p[index];
index++;
}
free(p);
line->content = newContent;
}
void jumpCols(int jumpCols,char*p,char**newP,char**beforeNewP){
int jumpedCols = 0;
int jumpUnit;
char* startP = p;
char* beforeP = p;
*newP=p;
while(startP&&*startP&&jumpedCols<jumpCols){
beforeP = *newP;
jumpLineUnit(startP,&*newP,&jumpUnit);
if(*newP!=0){
jumpedCols+=jumpUnit;
}
startP = *newP;
}
if(beforeNewP){
*beforeNewP = beforeP;
}
}
void getPosition(Line** plineToInsert,int *pshift,int* pcursorY,int* pcursorX){ int cursorY,cursorX;
getyx(editScr,cursorY,cursorX);
getPositionByYX(plineToInsert,pshift,cursorY,cursorX);
*pcursorX=cursorX;
*pcursorY=cursorY;
}
void getPositionByYX(Line** plineToInsert,int *pshift,int cursorY,int cursorX){
int scrLine,scrShift;
Line* lineToInsert;
int lineShift;
scrLine = screenLine.lineNo;
scrShift = screenLine.shift;
Line* line = lineList->next;
while(l
nana.c.tar.gz_nano_ncurses
版权申诉
43 浏览量
2022-09-14
20:26:07
上传
评论
收藏 6KB GZ 举报
alvarocfc
- 粉丝: 112
- 资源: 1万+
最新资源
- 1_Parameter Manual NC variable and interface signals.pdf
- 目标检测-垃圾桶满溢检测数据集-3000张图-+对应VOC-COCO-YOLO三种格式标签+数据集划分脚本
- 目标检测-垃圾桶满溢检测数据集-1000张图-+对应VOC-COCO-YOLO三种格式标签+数据集划分脚本
- (网络收集)2024年新课标Ⅰ卷数学卷带答案带解析带分值文字版.docx
- HCIP-Datacom-Advanced Routing & Switching Technology V1.0 培训材料
- SpringBoot+Vue尚庭公寓项目前端代码
- C51C51单片机实现的 抽奖机 设计与编程指南
- ikbc C87 机械键盘固件升级
- 词频统计PTA案例分析.zip
- 疫情背景下应急物资配送算法:matlab实现用改进后的多目标粒子群优化(MOPSO)算法解决带有风险矩阵的多辆车配送旅行商问题
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈