git-like vim invocation

起因

忽然发现自己很喜欢,gitcommit 时候,如果你没有输入-m参数的的话,它对vim的那种调用. 你可以使用vim的各种功能,慢慢的输入所有的数据,并且可以通过文本的方式自由的提示.甚至可以用ascii画图.然后在vim 关闭之后(似乎不是这样,难道git 时在追踪文件是否被锁定?)输入的信息将会得到处理.

经过

大概的实现就是开启子进程,然后等待子进程关闭,这样.查了下Microsoft文档,很快找到了进程相关的api,然后就基本完成了.代码就是这个样子.

#pragma once

#include <windows.h>
#include <string>
#include <fstream>
#include <vector>

using std::vector;
using std::string;
using std::ofstream;
using std::ifstream;

namespace mine{
    class VUI {
        char s[30]="gvim temporary_file_name";//why 30 is needed? I think this should cause error.
        char *cmd=s+0;
        char *tmp=cmd+5;
        string buffer;
        const char left='(',right=')';
    public:
        vector<string> answer;
        void setQuestion(const string& s) {//use "(" and ")" to set the boundaries for answers
            buffer=s;//is copy needed?
            answer.clear();
        }
        bool ask() {//more return value checking is needed
            //write question to file
            ofstream fo(tmp);
            fo<<buffer;
            fo.close();
            //open gvim and wait until gvim is closed
            STARTUPINFO si;
            PROCESS_INFORMATION pi;
            ZeroMemory(&si,sizeof(si));
            si.cb=sizeof(si);
            ZeroMemory(&pi,sizeof(pi));
            if(!CreateProcess(NULL,
                cmd,
                NULL,NULL,false,0,NULL,NULL,&si,&pi))return false;
            WaitForSingleObject(pi.hProcess,INFINITE);

            CloseHandle(pi.hProcess);
            CloseHandle(pi.hThread);
            //read file and extract answers
            ifstream fi(tmp);
            fi.seekg(0,ifstream::end);
            int len=fi.tellg();
            fi.seekg(0,ifstream::beg);
            buffer.resize(len+1);
            fi.read(&buffer[0],len);
            fi.close();
            bool in=false;
            for(int i=0;i<len;++i){
                if(buffer[i]==left){
                    in=true;
                    answer.push_back("");
                }
                else if(buffer[i]==right)in=false;
                else if(in){
                    answer.back()+=buffer[i];
                }
            }
            remove(tmp);
            return true;
        }
    };
};

结果

封装成了一个类,把一些自动提取结果的代码加进去了,其它也没什么特别的.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值