File browser in script - Script Writing - CHDK Forum

File browser in script

  • 2 Replies
  • 4032 Views
File browser in script
« on: 10 / June / 2011, 15:58:48 »
Advertisements
I'm writting a script that needs to operate on user defined file. So I need to give to user simple file browser. Is there a possibility to use CHDK file browser in script?

I've started writting my own file browser in lua, but if I can just use this browser build in CHDK in script it would save a lot of my time.
if (2*b || !2*b) {
    cout<<question
}

Compile error: poor Yorick

*

Offline msl

  • *****
  • 1280
  • A720 IS, SX220 HS 1.01a
    • CHDK-DE links
Re: File browser in script
« Reply #1 on: 10 / June / 2011, 19:18:31 »
Is there a possibility to use CHDK file browser in script?

Unfortunately this is not possible. You must write your own file browser.

msl
CHDK-DE:  CHDK-DE links

Re: File browser in script
« Reply #2 on: 11 / June / 2011, 03:45:22 »
Ok, I wrote my own, it's on alpha stage and you are only able to browse directories, not to take a file, but it'll be done in next release. Here are current problems:

- when users moves to a parent directory (for example from A/CHDK/SCRIPTS to A/CHDK the path the browser gives is A/CHDK/SCRIPTS/.. which refers to the same place, but in dirty way. I have no idea, how to get parent directory by lua, possibly there is some simple command?

-root directory is not accesible now... I need to solve the first problem before this

I also implemented a few nice functions such as is_file(string) and is_dir(string) which returns true if string referst to file or directory respectively and false if not (based on os.stat), global variable pwd (which meanc working directory), cd(string) which changes pwd if string refers to a directory and ls(string) which returns table containing list of files in directory string. These functions are simpler to use than low-level lua functions;)

the code:
Code: [Select]
--[[
@title chdkacs
--]]

-- SET ALL THE THINGS WE WILL NEED
console_height=12
set_console_layout(1,1,40,console_height)
visible_files=console_height-2

-- INPUTS --
function get_input()
    wait_click(5000)
    input=nil
    if is_key("up") then
        input="up"
        end
    if is_key("down") then
        input="down"
        end
    if is_key("left") then
        input="left"
        end
    if is_key("right") then
        input="right"
        end
    if is_key("set") then
        input="set"
        end
    return input
    end


-- FILESYSTEM CMDS --
function init_env()
    pwd="A/CHDK"
    end

function is_file(file)
    stat=os.stat(file)
    out=stat["is_file"]
    return out
    end

function is_dir(dir)
    stat=os.stat(dir)
    if stat~=nil then
        out=stat["is_dir"]
        end
    if stat==nil then
        out=false
        end
    return out
    end

function cd(dir)
    if is_dir(dir) then
        pwd=dir
        end
    if not is_dir(dir) then
        return false
        end
    end
   
function ls(dir)
    if is_dir(dir) then
        out=os.listdir(dir, true)
        end
    return out
    end

function browser_draw(first,selected)
    set_console_autoredraw(0)
    last=first+visible_files
    print("--- SELECT FILE ---")
    print("["..pwd.."]")
    for i=first, first+visible_files-2 do
        if i~=selected and i<=ls_numof then
            print("  "..ls_dir[i])
            end
        if i==selected and i<=ls_numof then
            print(">>"..ls_dir[i])
            end
        if i>ls_numof then
            print("")
            end
        end
    console_redraw()
    set_console_autoredraw(1)
    end

function browser()
    exit_browser=false
    ls_dir=ls(pwd)
    ls_numof=table.getn(ls_dir)
    selected=2
    first=2
    repeat
--        print("-- "..pwd.." --")
        browser_draw(first,selected)
        input=get_input()
        if input=="up" then
            selected=selected-1
            first=first-1
            end
        if input=="down" then
            selected=selected+1
            end
        if selected<2 then
            selected=2
            end
        if first<2 then
            first=2
            end
       
        if selected>ls_numof then
            selected=ls_numof
            end
       
        if selected>visible_files then
            first=selected-visible_files+2
            end
       
        if input=="set" then
            if is_dir(pwd.."/"..ls_dir[selected]) then
                cd(pwd.."/"..ls_dir[selected])
                ls_dir=ls(pwd)
                ls_numof=table.getn(ls_dir)
                selected=2
                first=2
                sleep(1000)
                end
            end
        until browser_exit==true
    end


init_env()

browser()

And the attached file:
if (2*b || !2*b) {
    cout<<question
}

Compile error: poor Yorick

 

Related Topics


SimplePortal © 2008-2014, SimplePortal