News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
ascii art viewer
Author Message
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #1
RE: ascii art viewer

Some additions and some reorganization/cleanup.

You can now swap foreground and background colors (key "x").
User-set variables are all in one place, including hotkeys.
Changed default cd key to "o" because I kept pressing it instead of "c".
Scrollbars now hide (resize to zero, actually) in fullscreen.
Text display font is now set to courier (I forgot screen is not the default monospace font in Fltk).
Fl_Window:fullscreen() is the default method of going fullscreen now, although I left the kludge version in comments.

I'm considering an attempt at disabling direct keyboard interaction with the file list. I find myself initially using up and down arrows, and then when I go fullscreen I have to switch to the hotkeys.

Code:
#!/usr/bin/env murgaLua

--[[

simple text display directed primarily toward high-res ASCII art
mikshaw 2010

This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://sam.zoy.org/wtfpl/COPYING for more details.

--]]

-- preferences
asciidir="." -- start directory
--asciidir="/home/shared/image/ascii"
sep="/" -- directory name separator, "\" for Windows
ww,wh=720,486 -- main window width & height
lw=140 -- file list width
as=5 -- initial art text size
fs_key="f" -- fullscreen hotkey
big_key=">" -- art font bigger
lil_key="<" -- font smaller
next_key="." -- show next file
prev_key="," -- show previous file
cd_key="o" -- change directory
swap_key="x" -- swap fg and bg colors
-- OPTIONAL preferences, can be deleted or commented out
ts=11 -- list text size
Fl:set_boxtype(fltk.FL_UP_BOX,fltk.FL_THIN_UP_BOX)
Fl:set_boxtype(fltk.FL_DOWN_BOX,fltk.FL_THIN_DOWN_BOX)
Fl:scrollbar_size(10) -- scrollbar width
--Fl:set_font(fltk.FL_COURIER,"some font") -- ascii text font


function load_txt()
list:value(index)
artbuff:loadfile(asciidir..filetab[index])
win:redraw()
end

-- file navigation
function list_cb()
if list:value()>0 then
  index=list:value()
  load_txt()
end
end
function nav_dn()
if index<list:size() then
  index=index+1
  load_txt()
end
end
function nav_up()
if index>1 then
  index=index-1
  load_txt()
end
end

-- font size
function refresh_buff()
art:textsize(as)
tmp=artbuff:text()
artbuff:text(tmp)
win:redraw()
end
function fnt_sm()
if as>1 then
  as=as-1
  refresh_buff()
end
end
function fnt_lg()
if as<24 then
  as=as+1
  refresh_buff()
end
end

-- swap colors
function invert()
fg,bg=art:color(),art:textcolor()
art:textcolor(fg)
art:color(bg)
art:redraw()
end

--[[
-- fullscreen, kludged for dwm which doesn't like Fl_Window:fullscreen()
function tog_fs()
if fullscr==0 then
  list:hide()
  art:scrollbar_width(0)
  wx,wy,ww,wh=win:x(),win:y(),win:w(),win:h()
  win:resize(0,0,Fl:w(),Fl:h())
  art:resize(0,0,Fl:w(),Fl:h())
  fullscr=1
else
  list:show()
  art:scrollbar_width(Fl:scrollbar_size())
  win:resize(wx,wy,ww,wh)
  art:resize(lw,0,ww-lw,wh)
  fullscr=0
end
win:redraw()
end
--]]

-- normal fullscreen
function tog_fs()
if fullscr==0 then
  list:hide()
  wx,wy,ww,wh=win:x(),win:y(),win:w(),win:h()
  win:fullscreen()
  art:resize(0,0,Fl:w(),Fl:h())
  fullscr=1
else
  list:show()
  win:fullscreen_off(wx,wy,ww,wh)
  art:resize(mw,0,ww-mw,wh)
  fullscr=0
end
win:redraw()
end

-- change directory
function cd()
local newdir=fltk.fl_dir_chooser("pick a new directory",asciidir,0)
if newdir then
  asciidir=newdir
  get_files()
end
end

-- add files to list
function get_files()
list:clear_output()
filetab={}
list:clear()
if not string.find(asciidir,sep.."$") then asciidir=asciidir..sep end
for i in lfs.dir(asciidir) do
  if string.find(string.lower(i),"%.txt$") then table.insert(filetab,i) end
end
table.sort(filetab)
for i,v in pairs(filetab) do
  list:add(string.sub(v,1,-5))
end
if list:size()==0 then
  list:add("no text files found")
  list:set_output()
else
  list:value(1); list:do_callback()
end
end

-- interface
Fl:get_system_colors()
win=fltk:Fl_Double_Window(ww,wh,"ASCII Images")

-- files
list=fltk:Fl_Hold_Browser(0,0,lw,wh)
list:callback(list_cb)
if ts then list:textsize(ts) end

-- display area
art=fltk:Fl_Text_Display(lw,0,ww-lw,wh)
artbuff=fltk:Fl_Text_Buffer()
art:buffer(artbuff)
art:textfont(fltk.FL_COURIER)
art:textsize(as)

-- hotkeys
hk=fltk:Fl_Menu_Bar(0,0,0,0)
hk:add("fullscreen",string.byte(fs_key),tog_fs)
hk:add("big",string.byte(big_key),fnt_lg)
hk:add("little",string.byte(lil_key),fnt_sm)
hk:add("next",string.byte(next_key),nav_dn)
hk:add("prev",string.byte(prev_key),nav_up)
hk:add("change directory",string.byte(cd_key),cd)
hk:add("swap colors",string.byte(swap_key),invert)
hk:global()

index,fullscr=0,0
get_files()
win:resizable(art)
win:show()
Fl:run()

This post was last modified: 12-08-2010 04:05 AM by mikshaw.

12-08-2010 03:53 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Messages In This Thread
ascii art viewer - mikshaw - 12-05-2010, 06:02 AM
RE: ascii art viewer - jpjacobs - 12-05-2010, 06:42 AM
RE: ascii art viewer - mikshaw - 12-05-2010, 08:20 AM
RE: ascii art viewer - mikshaw - 12-05-2010, 09:43 AM
RE: ascii art viewer - mikshaw - 12-05-2010, 02:30 PM
RE: ascii art viewer - jpjacobs - 12-05-2010, 11:30 PM
RE: ascii art viewer - mikshaw - 12-06-2010, 01:25 AM
RE: ascii art viewer - mikshaw - 12-06-2010, 01:44 AM
RE: ascii art viewer - mikshaw - 12-08-2010 03:53 AM

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: