stuncloud
4/20/2016 - 3:01 AM

input()で入力履歴から値を選択できるようにする

input()で入力履歴から値を選択できるようにする

with InputHistory
    while TRUE
        .Start()
        s = input("履歴を表示するには↓キーを押す<#CR>キャンセルで削除モード", .Item(0))
        .Stop()
        if length(s) then
            .Add(s)
        else
            del = .List("選んだものを削除します")
            if length(del) then
                .Remove(del)
            else
                break
            endif
        endif
    wend
    .Save()
endwith
module InputHistory

    const delimiter = chr(11)
    dim history
    dim size = 10

    procedure InputHistory
        Init()
    fend

    procedure Init()
        Load()
        if length(history) = 0 then history = safearray(0, size - 1)
        if length(history) < size then resize(history, size - 1)
    fend
    
    function Item(index)
        result = history[index]
    fend
    
    procedure Load()
        history = split(readini("InputHistory", "history"), delimiter, FALSE)
    fend
    
    procedure Save()
        writeini("InputHistory", "history", join(history, delimiter, FALSE))
    fend
    
    procedure Start(key = VK_DOWN)
        sethotkey(key, 0, "Popup")
    fend
    
    procedure Stop(key = VK_DOWN)
        sethotkey(key)
    fend

    procedure Remove(indexes[])
        for i in indexes
            history[i] = EMPTY
        next
        history = split(join(history, delimiter), delimiter, TRUE)
        resize(history, size - 1)
    fend

    function List(msg = "履歴一覧")
        ret = slctbox(SLCT_LST or SLCT_NUM, 0, msg, history)
        if ret = -1 then
            result = safearray(0, -1)
        else
            result = split(ret, "<#TAB>", TRUE, TRUE)
        endif
    fend

    procedure Popup
        title = replace(GET_UWSC_NAME, ".exe", "")
        id = getid(title, "TUinpBox_uwsc")
        if getid(GET_ACTIVE_WIN) = id then
            Stop(HOTKEY_VK)
            TEdit = hndtoid(getctlhnd(id, "TEdit"))
            px = status(TEdit, ST_X)
            py = status(TEdit, ST_Y) + status(TEdit, ST_HEIGHT)
            if status(TEdit, ST_TITLE) = history[0] then
                menu = slice(history, 1)
            else
                menu = history
            endif
            i = popupmenu(menu, px, py)
            if i > -1 then
                sendstr(id, menu[i], 1, TRUE)
                Add(menu[i])
            else
                ctrlwin(id, ACTIVATE)
            endif
            Start(HOTKEY_VK)
        else
            kbd(HOTKEY_VK, CLICK)
        endif
    fend

    procedure Add(str)
        if contains(history, str) then
            tmp = safearray(0, resize(history))
            tmp[0] = str
            i = 1
            j = 0
            repeat
                if history[j] <> str then
                    tmp[i] = history[j]
                    i = i + 1
                endif
                j = j + 1
            until i = length(history)
            history = tmp
        else
            shiftarray(history, 1)
            history[0] = str
        endif
    fend

    function contains(array, value)
        for item in array
            result = item = value
            if result then break
        next
    fend

endmodule