File: misc.tcl

package info (click to toggle)
mmucl 1.5.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 648 kB
  • ctags: 303
  • sloc: tcl: 4,977; sh: 111; makefile: 97
file content (75 lines) | stat: -rw-r--r-- 1,191 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
proc lassign {vars vals} {
    uplevel 1 [list foreach $vars $vals break]
}

proc lmatch_prefix {list str} {
    set matches [list]

    foreach el $list {
	if {[string first $str $el] == 0} {
	    lappend matches $el
	}
    }

    return $matches
}

proc lmatch {list glob} {
    if {$glob eq "*"} {
	return $list
    }

    set matches [list]

    foreach el $list {
	if {[string match $glob $el]} {
	    lappend matches $el
	}
    }

    return $matches
}

# remove an element from a list

proc lremove {listvar el} {
    upvar 1 $listvar list
    
    set i [lsearch -exact $list $el]

    if {$i != -1} {
	set list [lreplace $list $i $i]
    }
}

proc ltrimleft {list n} {
    set result [list]

    foreach el $list {
	lappend result [string range $el $n end]
    }

    return $result
}

# turn secs into human readable time description

proc duration {secs} {
    set res ""

    foreach div {86400 3600 60} mod {0 24 60} name {day hr min} {
        set n [expr {$secs / $div}]

        if {$mod > 0} {
	    set n [expr {$n % $mod}]
	}

        if {$n > 1} {
            append res $n " " $name s
	} elseif {$n == 1} {
            append res $n " " $name
	}
    }

    return $res
}