写下简单的sublime text插件

sublime text 是一个跨平台的文本编辑工具,用了下还是比较方便。
安装后,除了默认的语法支持外,如果想找下额外的功能,可以通过安装插件的方法来做。
快捷键 Ctrl+Shift+p 敲入install 可以看到已发布的插件列表。根据自己情况选择需要的插件。
但是有的时候,找不到自己想要的插件,那么只有自己编写了。
自己编写插件必备条件:
1、会基本python脚本语言
2、了解sublime text插件的api,这个通过官方文档查看
http://www.sublimetext.com/docs/3/
3、了解创建插件的过程,可以通过这个地址了解:
https://code.tutsplus.com/tutorials/how-to-create-a-sublime-text-2-plugin--net-22685

我写了个一个简单的sftp上传的插件:

pscp.exe 可以到网上下一个:
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

目录结构:
example/example.py
example/bin/pscp.exe

example.py代码如下

import sublime
import sublime_plugin
import os
import sys
import re

bin_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'bin')
has_bin = os.path.exists(bin_folder)
pscp_exe = os.path.join(bin_folder, 'pscp.exe')
has_psftp = os.path.exists(pscp_exe)
remote_ip = '1.1.1.1' # replace your ip
remote_ssh_password = 'you_password' # replace your you_password
pscp_exe_cmd = "\"" + pscp_exe + "\" -pw \"" + remote_ssh_password + "\" \"%s\" root@" + remote_ip + ":\"%s\" "
base_path = "D:\\svn\\project1\\" # repalce you base  path
remote_path = "/data/project1/"   # repalce you remote path

def get_relative_path(full_path,base_path):
    full_path = re.sub(r"\\", "/", full_path)
    base_path = re.sub(r"\\", "/", base_path)
    if (re.match(base_path, full_path, re.I)):
        relative_path = re.sub(base_path, "", full_path, re.I)
        return relative_path
    else:
        return ""

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        if os.name == 'nt' and (not has_bin or not has_psftp):
            sublime.error_message("sftp plugin is invalid")
        else:
            file_path = self.view.file_name()
            relative_path = get_relative_path(file_path,base_path)
            remote_file_path = remote_path + relative_path
            if (relative_path == ""):
                sublime.error_message("can not sftp to server, due to file not in base_path")
            else:
                cmd = pscp_exe_cmd % (file_path,remote_file_path)
                ret = os.popen(cmd)
                output = ret.read()
                if re.search(r'100%',output):
                    msg = relative_path+" upload success!"
                else:
                    msg = relative_path+" upload failed"
                sublime.message_dialog(msg)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容