computer programme fun Anonymous https://bbs.gikopoi.com/atom/thread/1725182612 2024-10-10T19:42:57+00:00 computer programme fun https://bbs.gikopoi.com/post/1725182612/1 2024-09-01T09:23:32+00:00 2024-09-01T09:23:32+00:00 the 'tree' command for my little tcl utilities collection now pretty<br>prints, but is still valid data. format is { path { subdirectories }}<br><br>example output:<br><br>mush {{<br> mush/people.tcl {}} {<br> mush/player.tcl {}} {<br> mush/input.tcl {}} {<br> mush/notions.tcl {}} {<br> mush/mush.tcl {}} {<br> mush/c {{<br> mush/c/raymath.h {}} {<br> mush/c/raylib.h {}} {<br> mush/c/raygui.h {}} {<br> mush/c/libraylib.a {}} {<br> mush/c/rl.c {}} {<br> mush/c/make.tcl {}} {<br> mush/c/rlgl.h {}} {<br> mush/c/rl.so {}}}} {<br> mush/graphics.tcl {}} {<br> mush/tool.tcl {}} {<br> mush/tools.txt {}} {<br> mush/proto.tcl {}} {<br> mush/event.tcl {}} {<br> mush/object.tcl {}}}<br> Reply to thread https://bbs.gikopoi.com/post/1725182612/2 2024-09-02T17:51:56+00:00 2024-09-02T17:51:56+00:00 WHERE'S THE SOURCE CODE, LEBOWSKI? Reply to thread https://bbs.gikopoi.com/post/1725182612/3 2024-09-02T21:20:58+00:00 2024-09-02T21:20:58+00:00 package require Tclx<br>package require tclreadline<br><br>proc rm { target } {<br> if {[ file exists $target ]} {<br> file delete -- $target<br> return<br> }<br> error "error removing \"$target\": no such file or directory"<br>}<br><br>proc rmr { path } {<br> xpt rm [ tree $path ]<br>}<br><br>proc mv { src dst } {<br> file rename -force -- $src $dst<br>}<br><br>proc cp { src dst } {<br> file copy -force -- $src $dst<br>}<br><br>proc mkdir { path } {<br> file mkdir $path<br>}<br><br>proc ls {{ path . }} {<br> if { ! [ catch { file link $path }]} {<br> error "open of directory \"${path}\" failed: not a directory"<br> }<br> vert [ readdir $path ]<br>}<br><br>proc lsn {{ path . }} {<br> vert [ enumerate [ ls $path ]] 2<br>}<br><br>proc cdn { n } {<br> cd [ sel $n ]<br>}<br><br>proc scroll { txt } {<br> set fd [ open "| less" w ]<br> puts $fd $txt<br> close $fd<br>}<br><br>proc vert { l { w 1 }} {<br> set out ""<br> set i 1<br> foreach item $l {<br> append out [ list $item ]<br> if { $i % $w == 0 } {<br> set sep_c "\n"<br> } else {<br> set sep_c " "<br> }<br> incr i<br> append out $sep_c<br> }<br> string trimright $out<br>}<br><br>proc prefix { prefix l } {<br> for { set i 0 } { $i < [ llength $l ]} { incr i } {<br> lset l $i "$prefix[ lindex $l $i ]"<br> }<br> return $l<br>}<br><br>proc enumerate { l } {<br> set out [ dict create ]<br> for { set i 0 } { $i < [ llength $l ]} { incr i } {<br> dict set out $i [ lindex $l $i ]<br> }<br> return $out<br>}<br><br>proc sel { n { path . }} {<br> dict get [ lsn $path ] $n<br>}<br><br>proc xp { cmd args_list { tail "" }} {<br> set out [ list ]<br> foreach item $args_list {<br> if { $tail eq "" } {<br> set retval [ uplevel 1 [ list {*}$cmd $item ]]<br> } else {<br> set retval [ uplevel 1 [ list {*}$cmd $item {*}$tail ]]<br> }<br> if { $retval ne "" } {<br> lappend out $retval<br> }<br> }<br> return $out<br>}<br><br>proc xpt { cmd tree { tail "" } { depth 1 }} {<br> set name [ lindex $tree 0 ]<br> set branches [ lindex $tree 1 ]<br> <br> if {[ llength $branches ] == 0 } {<br> if { $tail eq "" } {<br> return [ uplevel $depth [ list {*}$cmd $name ]]<br> } else {<br> return [ uplevel $depth [ list {*}$cmd $name {*}$tail ]]<br> }<br> }<br> <br> lappend branches [ list $name [ list ]]<br> set out [ list ]<br> foreach branch $branches {<br> set retval [ xpt $cmd $branch $tail [ expr { $depth + 1 }]]<br> if { $retval ne "" } {<br> lappend out $retval<br> }<br> }<br> return $out<br>}<br><br>proc tree {{ path . } { max_depth -1 } { depth 0 }} {<br> set path [ string trimright $path "/" ]<br> if {[ catch { ls $path } subdirs ]<br> || ( $max_depth >= 0 && $depth > $max_depth )} {<br> set subdirs {}<br> }<br> incr depth<br> set ugly [ xp tree [ prefix "${path}/" $subdirs ] "$max_depth $depth" ]<br> list $path [ prefix "\n" [ vert [ prefix [ string repeat " " $depth ] $ugly ]]]<br>}<br> Reply to thread https://bbs.gikopoi.com/post/1725182612/4 2024-09-02T21:22:50+00:00 2024-09-02T21:22:50+00:00 last line in tree got wrapped for some reason, tclreadline isn't actually<br>needed (leftover from old interactive thing) and i forgot to remove it Reply to thread https://bbs.gikopoi.com/post/1725182612/5 2024-10-10T19:42:57+00:00 2024-10-10T19:42:57+00:00 I feel like it's actually a pain & GNU+Linus propaganda tricked me to wasting years of my life.