summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIru Cai <mytbk920423@gmail.com>2014-04-20 22:09:17 +0800
committerIru Cai <mytbk920423@gmail.com>2014-04-20 22:09:17 +0800
commitb15900a363953591d48366379512795a9422222a (patch)
treeaa5248a9926adbef0cedd7d7dabaf9b157d13f65
parent88c98119b6e8d6d30731cd0d3e7fb3f9af5557cb (diff)
downloadfqterm-b15900a363953591d48366379512795a9422222a.tar.xz
Add my qz script -- for fun!
2 functions are added to the script engine.
-rw-r--r--.gitignore4
-rw-r--r--res/script/README7
-rw-r--r--res/script/functions.js6
-rw-r--r--res/script/libpost.js69
-rw-r--r--res/script/qz.js36
-rw-r--r--res/script/testz.js76
-rw-r--r--src/fqterm/fqterm_scriptengine.cpp15
-rw-r--r--src/fqterm/fqterm_scriptengine.h4
8 files changed, 215 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 0def275..4757e06 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,3 +19,7 @@
*.exe
*.out
*.app
+
+# Patch and reject
+*.rej
+*.patch
diff --git a/res/script/README b/res/script/README
index c7dacbc..8e273fc 100644
--- a/res/script/README
+++ b/res/script/README
@@ -6,6 +6,13 @@ Any file to be written to or read from should be in the format of UTF-8.
Usage:
+//UI functions
+msgBox(msg)
+ no return value
+
+yesnoBox(msg)
+ return boolean value
+
sessionID=long(sys.argv[0])
formatError(sessionID)
diff --git a/res/script/functions.js b/res/script/functions.js
new file mode 100644
index 0000000..2461774
--- /dev/null
+++ b/res/script/functions.js
@@ -0,0 +1,6 @@
+
+rand=function(n){ //generate a random number b'w 0-(n-1)
+ var t=Math.random();
+ return Math.floor(t*n);
+}
+
diff --git a/res/script/libpost.js b/res/script/libpost.js
new file mode 100644
index 0000000..fee7dea
--- /dev/null
+++ b/res/script/libpost.js
@@ -0,0 +1,69 @@
+fqterm.importFile("utils.js");
+var timeout=100;
+
+postID=function(){
+ var thisLine = fqterm.getText(fqterm.caretY());
+ var reg = new RegExp('[0-9]{1,}');
+ return reg.exec(thisLine);
+}
+
+lastPost=function(){
+ fqterm.sendString("$"); //last post
+ sleep(timeout);
+ return postID();
+}
+
+isBoard=function(){
+ var secondLine = fqterm.getAttrText(1);
+ var charString = "Ctrl-P";
+ var myReg = new RegExp(charString);
+ return myReg.exec(secondLine)==charString;
+}
+
+postAction=function(){
+ if (isBoard()){
+ fqterm.sendParsedString("^p");
+ }else{
+ fqterm.sendString("a");
+ }
+}
+
+postEmpty=function(title){
+ // postAction() is now deprecated
+ if (isBoard()){
+ fqterm.sendParsedString("^p");
+ fqterm.sendString(title+"\n0\n\n"); // no qmd
+ }else{
+ fqterm.sendString("a"+title+"\n");
+ }
+ sleep(timeout);
+ fqterm.sendParsedString("^W\n");
+ sleep(timeout);
+}
+
+pastefile=function(filename){
+ var content = fqterm.readFile(filename);
+ for (var j = 0; j < content.length; ++j)
+ {
+ if (content[j] != '\033')
+ fqterm.sendString(content[j]);
+ else
+ fqterm.sendParsedString("^[^[");
+ }
+ fqterm.sendParsedString("^W\n");
+ sleep(timeout);
+}
+
+copyline=function(){
+ var line=fqterm.caretY();
+ var filename = "/tmp/test.txt";
+ fqterm.writeFile(filename, "");
+ for (var i=line-1;i<=line+1;++i){
+ var origline=fqterm.getAttrText(i);
+ var reg = new RegExp('\x1b\x1b','g');
+ var ANSIStr = origline.replace(reg,'\x1b[');
+ fqterm.appendFile(filename, ANSIStr);
+ fqterm.appendFile(filename, "\n");
+ }
+ return filename;
+}
diff --git a/res/script/qz.js b/res/script/qz.js
new file mode 100644
index 0000000..9a98bab
--- /dev/null
+++ b/res/script/qz.js
@@ -0,0 +1,36 @@
+fqterm.importFile("libpost.js");
+fqterm.importFile("testz.js");
+
+qz=function(){
+ articleID = lastPost();
+ ++articleID;
+ var IDStr=isz(articleID);
+ if (IDStr==-1){
+ var cont = true;
+ try
+ {
+ if (fqterm.yesnoBox("似乎不是整哦,继续抢吗?")==false){
+ cont = false;
+ }
+ }
+ catch(err)
+ {
+ }
+ if (cont){
+ IDStr = articleID;
+ }else{
+ return;
+ }
+ }
+ postEmpty(IDStr);
+ if (isBoard()){
+ fqterm.sendString("j=_"); //focus to the new post
+ sleep(500);
+ myFile=copyline();
+ fqterm.sendString("E"); //Edit the post
+ pastefile(myFile);
+ }
+}
+
+qz();
+
diff --git a/res/script/testz.js b/res/script/testz.js
new file mode 100644
index 0000000..ec7610b
--- /dev/null
+++ b/res/script/testz.js
@@ -0,0 +1,76 @@
+fqterm.importFile("functions.js");
+
+isexpt=function(num){
+ if (num==1){
+ return "1";
+ }
+ for (var i=2;i*i<=num;++i){
+ var p=i;
+ for (j=1;p<=num;++j){
+ if (p==num){
+ var strs = new Array();
+ strs[0] = i+"^"+j;
+ strs[1] = "(expt "+i+" "+j+")";
+ strs[2] = i+"**"+j;
+ return strs[rand(3)];
+ }else{
+ p *= i;
+ }
+ }
+ }
+ return -1;
+}
+
+ismod100=function(num){
+ var strs = new Array();
+ strs[0] = num;
+ if (num%1000==0){
+ strs[1] = "000";
+ return strs[rand(2)];
+ }else if (num%100==0){
+ strs[1] = "00";
+ return strs[rand(2)];
+ }else{
+ return -1;
+ }
+}
+
+isseq = function(num){
+ if (num<100){
+ return -1;
+ }
+ var digits = new Array();
+ var tmp = num;
+ var n = 0;
+ for (; tmp>0; n++){
+ digits[n] = tmp%10;
+ tmp = (tmp-digits[n])/10;
+ }
+ for (var i=1; i<n-1; ++i){
+ if (digits[i]*2==digits[i-1]+digits[i+1]){
+ continue;
+ }else{
+ return -1;
+ }
+ }
+ return num;
+}
+
+isz=function(num){
+ var str;
+ str = ismod100(num);
+ if (str!=-1){
+ return str;
+ }
+ str = isexpt(num);
+ if (str!=-1){
+ return str;
+ }
+ str = isseq(num);
+ if (str!=-1){
+ return str;
+ }
+ return -1;
+}
+
+
diff --git a/src/fqterm/fqterm_scriptengine.cpp b/src/fqterm/fqterm_scriptengine.cpp
index efab413..c49f44c 100644
--- a/src/fqterm/fqterm_scriptengine.cpp
+++ b/src/fqterm/fqterm_scriptengine.cpp
@@ -35,6 +35,7 @@
#include <QString>
#include <QDir>
#include <QMessageBox>
+#include <QFileDialog>
#include <QTime>
#include <QTimer>
#include <QFile>
@@ -132,6 +133,18 @@ void FQTermScriptEngine::msgBox( const QString& msg ) {
QMessageBox::Close);
}
+bool FQTermScriptEngine::yesnoBox( const QString& msg ){
+ return QMessageBox::question(window_, tr("FQTerm"),
+ msg,
+ QMessageBox::Yes|QMessageBox::No,
+ QMessageBox::No)==QMessageBox::Yes;
+}
+
+QString FQTermScriptEngine::FileDialog() {
+ return QFileDialog::getOpenFileName(
+ NULL, "Select a file", QDir::currentPath(), "*");
+}
+
int FQTermScriptEngine::caretX() {
return buffer_->getCaretColumn();
}
@@ -485,4 +498,4 @@ bool FQTermScriptEngine::isAutoReply() {
}
} // namespace FQTerm
-#include "fqterm_scriptengine.moc" \ No newline at end of file
+#include "fqterm_scriptengine.moc"
diff --git a/src/fqterm/fqterm_scriptengine.h b/src/fqterm/fqterm_scriptengine.h
index 70ca5cd..25af6e1 100644
--- a/src/fqterm/fqterm_scriptengine.h
+++ b/src/fqterm/fqterm_scriptengine.h
@@ -52,6 +52,7 @@ public:
public slots: //script apis
//ui functions.
void msgBox(const QString& msg);
+ bool yesnoBox(const QString& msg);
//bbs ui functions
int caretX();
@@ -73,6 +74,7 @@ public slots: //script apis
bool isConnected();
void disconnect();
void reconnect();
+ QString FileDialog();
QString getBBSCodec();
QString getAddress();
int getPort();
@@ -144,4 +146,4 @@ private:
}//namespace FQTerm
-#endif //FQTERM_SCRIPTENGINE_H \ No newline at end of file
+#endif //FQTERM_SCRIPTENGINE_H