注册 登录  
 加关注

网易博客网站关停、迁移的公告:

将从2018年11月30日00:00起正式停止网易博客运营
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

祝灾区人民早日度过难关

努力工作

 
 
 

日志

 
 

[原创]qterm-0.4.0添加彩色复制功能的补丁  

2008-01-03 00:37:08|  分类: IT传奇 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
    使用方法:
    下载qterm-0.4.0源代码。解压。下载补丁qterm-0.4.0-copy_colorful_article.patch,放在源代码同一个目录下。
    cd qterm-0.4.0/
    patch -p1< ../qterm-0.4.0-copy_colorful_article.patch
    ./configure
    make
    make install
    增加的功能:
    1.在右键菜单中"copy article"选项下方添加了"copy colorful article"功能。
    2.在工具栏的右起第七个按钮是"copy colorful article"功能按钮。
    3.此外,在qterm的python接口qterm模块中增加了copyColorfulArticle方法。测试代码:
import qterm
import sys, time

f=open("/home/luohandsome/prog/python/qterm/test.txt","w")
lp=long(sys.argv[0])
f.write(qterm.copyColorfulArticle(lp))
f.close()
    保存为.py文件,然后在qterm主菜单中选择script->run,当前文章就会被彩色保存到test.txt。
    存在问题:
    1.彩色存盘时,控制字符中的[会丢失。(以下把两个ASCII码为0x1b的不可打印字符表示为0x1b0x1b,使用时请替换回正确的字符)为所以保存后还需要用查找替换功能把"0x1b0x1b"替换成"0x1b0x1b["
如:
0x1b0x1b33;44;1m[好朋友列表] 0x1b0x1b0m
需要改成
0x1b0x1b[33;44;1m[好朋友列表] 0x1b0x1b[0m
    我猜测是由于qterm中的某个类的getAttrText()成员函数在返回字符串时过滤了控制字符中的[。我的印象中CTerm(会把两个0x1b变为一个)和FTerm(会把两个0x1b变为*字符)也有类似问题。可能新版本里已经更正了吧。
    2.有时候文章会重复下载两次。不知道原因。难道是超时后又重新下载?
    3.快捷键和按钮的图标都是用copy article的。不过这是小问题了呵呵。
    可能还有其它bug,欢迎大牛们fix他们。
    原理:
    在qterm中,某个类有两个成员函数getText()和getAttrText()用来读取一行文字,区别是前者返回的行没有控制字符,后者有。只要把copyArticle的代码复制一份,在add line时把相应的getText函数改为getAttrText就OK了。
    改动:
    1.在QTermFrame和QTermWindow中添加copyColorfulArticle方法。然后在工具栏和右键菜单初始化时把新的菜单项和函数关联起来。
    2.在QTermDAThread类中添加成员变量colorful用来记录是否要彩色存盘。然后在线程函数run中根据colorful的值来决定调用getText()还是getAttrText()
    3.qtermpython.cpp文件定义了qterm python模块的方法,即所谓的embedding python。在里面仿照copyArticle添加一个copyColorfulArtcle方法。
    估计彩色存盘的需求很少。之所以想到要添加这样的功能,是因为我做Linux World的通知(通常是彩色的)后需要群发邮件。由于为了看效果,我是直接在QTerm里改的(我比较土,至今不知道Linux下是否有asciiart相关的编辑软件。)。没有彩色存盘功能,我无法直接获得修改后的有彩色效果的文件,带来了很大不便。虽然现在保存后还要查找替换一下(希望能早日fix这个bug),毕竟是方便多了。
    可能还有其它bug,欢迎联系我或者fix他们。
    最后,感谢adoal,BSD,macaulish,pluskid等网友的指点!


附qterm-0.4.0-copy_colorful_article.patch(不知道怎么上传文件,只能直接贴补丁了):
diff -uNr qterm-0.4.0/qterm/qtermframe.cpp qterm-0.4.0-copy_colorful_article/qterm/qtermframe.cpp
--- qterm-0.4.0/qterm/qtermframe.cpp    2006-07-10 15:36:13.000000000 +0800
+++ qterm-0.4.0-copy_colorful_article/qterm/qtermframe.cpp    2008-01-02 23:23:11.000000000 +0800
@@ -806,6 +806,11 @@
     wndmgr->activeWindow()->copyArticle();
 }
 
+void QTermFrame::copyColorfulArticle( )
+{
+    wndmgr->activeWindow()->copyColorfulArticle();
+}
+
 void QTermFrame::autoCopy()
 {
     wndmgr->activeWindow()->m_bAutoCopy = !wndmgr->activeWindow()->m_bAutoCopy;
@@ -1369,6 +1374,8 @@
     // Spec (5)
     new QToolButton( QPixmap(pathPic+"pic/article.png"), tr("Copy Article"), QString::null,
             this, SLOT(copyArticle()), mdiconnectTools, "Copy Article" );
+    new QToolButton( QPixmap(pathPic+"pic/article.png"), tr("Copy Colorful Article"), QString::null,
+            this, SLOT(copyColorfulArticle()), mdiconnectTools, "Copy Colorful Article" );
     specAnti =
         new QToolButton( QPixmap(pathPic+"pic/anti-idle.png"), tr("Anti-Idle"), QString::null,
             this, SLOT(antiIdle()), mdiconnectTools, "Anti-Idle" );
@@ -1502,6 +1509,8 @@
     spec->insertItem( QPixmap(pathLib+"pic/article.png"),
                     tr("&Copy article"), this, SLOT(copyArticle()), Qt::Key_F9, ID_SPEC_ARTICLE );
     spec->setCheckable( true );
+    spec->insertItem( QPixmap(pathLib+"pic/article.png"),
+                    tr("&Copy Colorful article"), this, SLOT(copyColorfulArticle()), Qt::Key_F9, ID_SPEC_ARTICLE );
     spec->insertItem( QPixmap(pathLib+"pic/anti-idle.png"),
                     tr("Anti &idle"), this, SLOT(antiIdle()), 0, ID_SPEC_ANTI );
     spec->insertItem(QPixmap(pathLib+"pic/auto-reply.png"),
diff -uNr qterm-0.4.0/qterm/qtermframe.h qterm-0.4.0-copy_colorful_article/qterm/qtermframe.h
--- qterm-0.4.0/qterm/qtermframe.h    2006-07-10 15:36:13.000000000 +0800
+++ qterm-0.4.0-copy_colorful_article/qterm/qtermframe.h    2008-01-02 23:23:16.000000000 +0800
@@ -117,6 +117,7 @@
     void copyRect();
     void copyColor();
     void copyArticle();
+    void copyColorfulArticle();
      void autoCopy();
     void wordWrap();
     void noEsc();
diff -uNr qterm-0.4.0/qterm/qtermpython.cpp qterm-0.4.0-copy_colorful_article/qterm/qtermpython.cpp
--- qterm-0.4.0/qterm/qtermpython.cpp    2006-07-10 15:36:13.000000000 +0800
+++ qterm-0.4.0-copy_colorful_article/qterm/qtermpython.cpp    2008-01-02 23:22:39.000000000 +0800
@@ -158,6 +158,79 @@
     return py_text;
 }
 
+// copy current artcle for back compatible use only
+// for new coder please use getArticle
+static PyObject *qterm_copyColorfulArticle(PyObject *, PyObject *args)
+{
+    long lp;
+    if (!PyArg_ParseTuple(args, "l", &lp))
+        return NULL;
+    QTermWindow *pWin=(QTermWindow*)lp;
+
+    QStringList strList;
+    QCString cstrArticle;
+    while(1)
+    {
+        // check it there is duplicated string
+        // it starts from the end in the range of one screen height
+        // so this is a non-greedy match
+        QString strTemp = pWin->stripWhitespace(
+                pWin->m_pBuffer->screen(0)->getText());
+        int i=0;
+        int start=0;
+        for(QStringList::Iterator it=strList.fromLast();
+    it!=strList.begin(), i < pWin->m_pBuffer->line()-1; // not exceeeding the last screen
+            --it, i++)
+        {
+            if(*it!=strTemp)
+                continue;
+            QStringList::Iterator it2 = it;
+            bool dup=true;
+            // match more to see if its duplicated
+            for(int j=0; j<=i; j++, it2++)
+            {
+                QString str1 = pWin->stripWhitespace(
+                    pWin->m_pBuffer->screen(j)->getText());
+                if(*it2!=str1)
+                {
+                    dup = false;
+                    break;
+                }
+            }
+            if(dup)
+            {
+                // set the start point
+                start = i+1;
+                break;
+            }
+        }
+        // add new lines
+        for(i=start;i<pWin->m_pBuffer->line()-1;i++)
+            strList+=pWin->stripWhitespace(
+            pWin->m_pBuffer->screen(i)->getAttrText());
+
+        // the end of article
+        if( pWin->m_pBuffer->screen(
+        pWin->m_pBuffer->line()-1)->getText().find("%") == -1 )
+            break;
+        // continue
+        pWin->m_pTelnet->write(" ", 1);
+       
+        if(!pWin->m_wcWaiting.wait(10000))    // timeout
+            break;
+    }
+    #if defined(_OS_WIN32_) || defined(Q_OS_WIN32)
+    cstrArticle = strList.join("\r\n");
+    #else
+    cstrArticle = strList.join("\n");
+    #endif
+   
+    PyObject *py_text = PyString_FromString(cstrArticle);
+
+    Py_INCREF(py_text);
+    return py_text;
+}
+
 static PyObject *qterm_getArticle(PyObject *, PyObject *args)
 {
     long lp;
@@ -567,6 +640,9 @@
     {"copyArticle",        (PyCFunction)qterm_copyArticle,            METH_VARARGS,
             "copy current article (obsolete)"},
 
+    {"copyColorfulArticle",        (PyCFunction)qterm_copyColorfulArticle,            METH_VARARGS,
+            "copy current colorful article (obsolete)"},
+
     {"getText",        (PyCFunction)qterm_getText,            METH_VARARGS,
             "get text at line#"},
 
diff -uNr qterm-0.4.0/qterm/qtermwindow.cpp qterm-0.4.0-copy_colorful_article/qterm/qtermwindow.cpp
--- qterm-0.4.0/qterm/qtermwindow.cpp    2006-07-10 15:36:13.000000000 +0800
+++ qterm-0.4.0-copy_colorful_article/qterm/qtermwindow.cpp    2008-01-02 23:22:54.000000000 +0800
@@ -78,9 +78,10 @@
 extern QString getOpenFileName(const QString&, QWidget*);
 
 // script thread
-QTermDAThread::QTermDAThread(QTermWindow *win)
+QTermDAThread::QTermDAThread(QTermWindow *win, bool color)
 {
     pWin = win;
+    colorful = color;
 }
 
 QTermDAThread::~QTermDAThread()
@@ -127,8 +128,13 @@
         }
         // add new lines
         for(i=start;i<pWin->m_pBuffer->line()-1;i++)
-            strList+=pWin->stripWhitespace(
-            pWin->m_pBuffer->screen(i)->getText());
+            if(!colorful)
+                strList+=pWin->stripWhitespace(
+                pWin->m_pBuffer->screen(i)->getText());
+            else
+                strList+=pWin->stripWhitespace(
+                pWin->m_pBuffer->screen(i)->getAttrText());
+
 
         // the end of article
         if( pWin->m_pBuffer->screen(
@@ -306,6 +312,7 @@
     m_pMenu->insertItem( QPixmap(pathLib+"pic/copy.png"), tr("Copy"), this, SLOT(copy()), CTRL+Key_Insert );
     m_pMenu->insertItem( QPixmap(pathLib+"pic/paste.png"), tr("Paste"), this, SLOT(paste()), SHIFT+Key_Insert );
     m_pMenu->insertItem( QPixmap(pathLib+"pic/article.png"), tr("Copy Article"), this, SLOT(copyArticle()), Key_F9 );
+    m_pMenu->insertItem( QPixmap(pathLib+"pic/article.png"), tr("Copy Colorful Article"), this, SLOT(copyColorfulArticle()), Key_F9 );
     m_pMenu->insertSeparator();
     m_pMenu->insertItem( QPixmap(pathLib+"pic/fonts.png"), tr("Font"), this, SLOT(font()) );
     m_pMenu->insertItem( QPixmap(pathLib+"pic/color.png"), tr("Color"), this, SLOT(color()) );
@@ -1405,6 +1412,15 @@
     m_pDAThread->start();
        
 }
+void QTermWindow::copyColorfulArticle( )
+{
+    if(!m_bConnected)
+        return;
+   
+    m_pDAThread = new QTermDAThread(this,true);
+    m_pDAThread->start();
+       
+}
 void QTermWindow::font()
 {       
     bool ok;
diff -uNr qterm-0.4.0/qterm/qtermwindow.h qterm-0.4.0-copy_colorful_article/qterm/qtermwindow.h
--- qterm-0.4.0/qterm/qtermwindow.h    2006-07-10 15:36:12.000000000 +0800
+++ qterm-0.4.0-copy_colorful_article/qterm/qtermwindow.h    2008-01-02 23:23:00.000000000 +0800
@@ -40,13 +40,14 @@
 class QTermDAThread : public QThread
 {
 public:
-    QTermDAThread(QTermWindow*);
+    QTermDAThread(QTermWindow*, bool color = false);
     ~QTermDAThread();
 
     virtual void run();
     QCString cstrArticle;
 private:
     QTermWindow *pWin;
+    bool    colorful;
 };
 
 class QTermWindow: public QMainWindow
@@ -65,6 +66,7 @@
     void copy();
     void paste();
     void copyArticle();
+    void copyColorfulArticle();
     void font();
     void setting();
     void color();

  评论这张
 
阅读(122)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018