2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

今使ってるpcdコマンド

Posted at

どういう経緯で書いてるか

以前、別アカウントでたまにゴミ知識を書いていたのだが、
前々職を辞める時に色々あって、端末の関係でアカウントにアクセス不能になってしまった。

こういう時に困るのが、過去の自分が書いた今は間違っている知識の類である。
何度かQiitaの運営に、アカウント削除なり、記事の削除なり、二段階認証の解除なりを問い合わせしたのが梨の礫の状態なので、ここで訂正する次第である。

問題の記事

何が問題なのか

訂正版

必要なソフト

コード

nyagos.d/pcd.lua
nyagos.alias.pcd = function (arg)
    local command
    local path
    if arg[1] then
        command = arg[1]
    end
    if arg[2] then
        path = arg[2]
    end
    if command == "add" or command == "a" then
        share.pathchanger(path)
    elseif command == "list" or command == "l" then
        share.pecoLi()
    elseif command == "edit" or command == "e" then
        local home = nyagos.getenv('HOME')
        nyagos.exec("subl "..home.."/.pcd_list")
    elseif command == "help" or command == "h" then
        print("pcd is bookmark script use peco and fzy.\n")
        print("   add : 'Add current directory favorite list.'\n")
        print("   add [path or directory name] : 'You can add the specified absolute path or directory to your favorites list. :)'\n")
        print("   list : 'Call favorite list use peco.'\n")
        print("   help : 'this.'")
    elseif command == nil or command == "" then
        share.pcdOrigin()
    else
        print("pcd: "..command.." is not a pcd command. Plz See 'pcd help'")
    end
end


-- .pcd_listファイルを探す関数
share.pathchanger = function (path)
    local current = path
    local cpath = share.cpathAddSlash(share.slashChanger(nyagos.getwd()))
    if current == nil then
        current = nyagos.getwd()
    end
    current = share.cpathAddSlash(share.slashChanger(current))
    if string.match(current, "("..cpath..")") then
        share.existsDirectory(current)
    else
        current = cpath..current
        share.existsDirectory(current)
    end
end

-- ディレクトリの存在チェック
share.existsDirectory = function (current)
    local f = io.open(current.."/.test", "w+")
    if f then
        io.output(f):write("test")
        share.addFolder(current)
        io.close(f)
        os.remove(current.."/.test")
    else
        print("'"..current.."' no such directory. ")
    end
end

-- .pcd_listファイルがなければ生成し、リストに追加する関数
share.addFolder = function (current)
    local home = nyagos.getenv('HOME')
    local f = io.open(home.."/.pcd_list", "r+")
    -- ファイルの存在チェック
    if f then
        -- 既にお気に入りに登録されているか調べる
        if string.find(f:read("*a"), current.."\n") then
            print("'"..current.."' is already exists in '"..home.."\\.pcd_list' .")
        else
            io.output(f):write(current.."\n")
            print("'"..current.."' add '"..home.."\\.pcd_list' .")
        end
        io.close(f)
    else
        f = io.open(home.."/.pcd_list", "w+")
        if io.output(f):write(current.."\n") then
            print("Success! pcd create '"..home.."\\.pcd_list' .")
            print("'"..current.."' add '"..home.."\\.pcd_list' .")
        else
            print("Fail! pcd cant create '.pcd_list' file")
        end
        io.close(f)
    end
end

-- favlistでpecoる関数
share.pecoLi = function ()
    nyagos.eval('setx PCD_CONFIG %HOME%\\.pcd_list')
    local dir = nyagos.eval('type "%PCD_CONFIG%" | peco')
    if (dir ~= nil) then
        nyagos.exec("cd " .. '"' .. share.chomp(dir) .. '"')
    end
end

-- \→/に変換する
share.slashChanger = function (str)
    return string.gsub(str, "\\", "/")
end

-- 末尾にスラッシュがあるか調べる
share.cpathAddSlash = function (str)
    if string.find(str, "/", "-1") == nil then
        str = str.."/"
    end
    return str
end

share.pcdOrigin = function ()
    local line = nyagos.eval("ls -la")
    local complst = share.split(line, "[\r\n]")
    local directories = ""
    for i, e in ipairs(complst) do
        if (e:match('.+/$')) then
            directories = directories .. '\n' .. e:gsub(".-%s+", "", 4)
        end
    end
    if (directories == "") then
        nyagos.exec("ls -oa")
    else
        local dir = nyagos.eval("echo " .. directories ..  " | fzy")
        if (dir ~= nil) then
            nyagos.exec("cd " .. '"' .. share.chomp(dir) .. '"')
        end
    end
end

share.split = function (str, pat)
    local t = {}
    local fpat = "(.-)" .. pat
    local last_end = 1
    local s, e, cap = str:find(fpat, 1)
    while s do
        if s ~= 1 or cap ~= "" then
            table.insert(t,cap)
        end
        last_end = e+1
        s, e, cap = str:find(fpat, last_end)
    end
    if last_end <= #str then
        cap = str:sub(last_end)
        table.insert(t, cap)
    end
    return t
end

share.chomp = function (src)
    return string.gsub(src, "[\r\n]+$", "")
end
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?