Skip to content

Instantly share code, notes, and snippets.

@tangentstorm
Last active May 19, 2024 17:02
Show Gist options
  • Save tangentstorm/37a26ebf0dc438eae1e765675b73ecd0 to your computer and use it in GitHub Desktop.
Save tangentstorm/37a26ebf0dc438eae1e765675b73ecd0 to your computer and use it in GitHub Desktop.
itemlist contraption for decker
{deck}
version:1
card:0
size:[512,342]
name:"itemlist.deck"
{card:home}
{widgets}
itemlist1:{"type":"contraption","size":[400,161],"pos":[13,30],"def":"itemlist","widgets":{"label":{},"item_text":{},"items":{},"bnw":{},"bgo":{},"run_event":{},"slider1":{},"bpr":{},"bnx":{},"brm":{},"bup":{},"dn":{}}}
{contraption:itemlist}
size:[400,161]
resizable:1
margin:[8,35,145,7]
description:"a list of items"
image:"%%IMG2AZAAoQH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wH/Af8B/wGM"
script:"itemlist.0"
attributes:{"name":["label","run_event","list_width"],"label":["label","run event (on \"!\")","list width"],"type":["string","string","number"]}
{widgets}
label:{"type":"field","size":[260,20],"pos":[0,0],"locked":1,"script":"itemlist.1","font":"menu","show":"invert","border":0,"style":"plain","value":" label"}
item_text:{"type":"field","size":[320,140],"pos":[80,20],"script":"itemlist.2","border":1,"scrollbar":1,"style":"code","value":"this is item 0"}
items:{"type":"grid","size":[80,140],"pos":[0,20],"script":"itemlist.3","headers":0,"lines":0,"widths":[60,0],"format":"ss","value":{"value":["item 0","item 1","item 2"],"text":["this is item 0","this is item 1","this is item 2"]},"row":0}
bnw:{"type":"button","size":[20,20],"pos":[380,0],"script":"itemlist.4","text":"+"}
bgo:{"type":"button","size":[20,20],"pos":[260,0],"script":"itemlist.5","font":"body","text":"!"}
run_event:{"type":"field","size":[100,20],"pos":[0,-40],"script":"itemlist.1","show":"none","value":"run"}
slider1:{"type":"slider","size":[100,20],"pos":[120,-40],"script":"itemlist.6","show":"none","interval":[0,400],"value":80,"step":20,"style":"compact"}
bpr:{"type":"button","size":[20,20],"pos":[320,0],"script":"itemlist.7","font":"body","text":"<"}
bnx:{"type":"button","size":[20,20],"pos":[340,0],"script":"itemlist.8","font":"body","text":">"}
brm:{"type":"button","size":[20,20],"pos":[360,0],"script":"itemlist.9","text":"-"}
bup:{"type":"button","size":[20,20],"pos":[280,0],"script":"itemlist.10","font":"body","text":"up"}
dn:{"type":"button","size":[20,20],"pos":[300,0],"script":"itemlist.11","font":"body","text":"dn"}
{script:itemlist.0}
on get_label do label.text end
on set_label x do label.text: x end
on get_run_event do run_event.text end
on set_run_event x do run_event.text: x end
on run x do card.event[get_run_event[] x] end
on goto_row x do
items.row: max 0,min x,count items.value
item_text.value: items.value[items.row].text
end
on view do
slider1.interval: 0,card.size[0]
set_list_width[slider1.value]
end
on get_list_width do slider1.value end
on set_list_width x do
cw:card.size[0] lw: slider1.value: floor x
items.size: lw, items.size[1]
items.widths: (max 20,lw-20),0
item_text.pos: lw,item_text.pos[1]
item_text.size: (cw-lw),item_text.size[1]
end
on mv_up do
r:items.row t:items.value
a: select where index<r-1 from t
b: select where index=r-1 from t
c: select where index=r from t
d: select where index>r from t
items.value:a,c,b,d
goto_row[r-1]
end
on mv_dn do
r:items.row t:items.value
a: select where index<r from t
b: select where index=r from t
c: select where index=r+1 from t
d: select where index>r+1 from t
items.value:a,c,b,d
goto_row[r+1]
end
# so you can query the items:
on get_ls do on ls do items.value end end
# or the current row:
on get_current do on current do items.value[items.row] end end
{end}
{script:itemlist.1}
on change val do
end
{end}
{script:itemlist.2}
on change val do
t:items.value t.text[items.row]: val
items.value:t
end
{end}
{script:itemlist.3}
on click row do
goto_row[row]
end
on changecell v do
t:me.value t[me.colname][me.row]:v
me.value:t
end
{end}
{script:itemlist.4}
on click do
k: keys items.value
row: table k dict count k take ()
items.value: items.value, row
goto_row[count items.value] # -1 but decker knows
end
{end}
{script:itemlist.5}
on click do
run[item_text.text]
end
{end}
{script:itemlist.6}
on change val do
set_list_width[me.value]
end
{end}
{script:itemlist.7}
on click do
goto_row[items.row-1]
end
{end}
{script:itemlist.8}
on click do
goto_row[items.row+1]
end
{end}
{script:itemlist.9}
on click do
if alert["really delete this item?" "bool"]
items.value:select where !index=items.row from items.value
end
goto_row[items.row]
end
{end}
{script:itemlist.10}
on click do
mv_up[]
end
{end}
{script:itemlist.11}
on click do
mv_dn[]
end
{end}
@tangentstorm
Copy link
Author

image

https://itch.io/t/2690007/the-contraption-bazaar?after=20#post-9886267

This contraption lets you store a list of items. Basically it wraps a two-column grid, but the second column is hidden and replaced with a big text area.

The buttons at the top do the following:

! -> "run", passes the current text to a configurable event handler on the card (run_event attribute contains the event name to trigger). Using shift-enter with the editor focused will also trigger this event, either on the entire item text or the selection.
up/dn -> moves the current item up or down in the list of items
</> -> lets you navigate between the items in order. (you can click the names in the list to do this, but you can also hide the names by setting the list_width attribute to 0)

  • -> deletes the current item (shows a confirmation box first)
  • -> adds a new item
    There are also two helper functions you can use to get access to the underlying data:

.ls[] -> returns the entire items.value table
.current[] -> returns the current row from the table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment