File: ctrlunix.tcl

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (91 lines) | stat: -rw-r--r-- 2,257 bytes parent folder | download | duplicates (6)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- tcl -*-
# ### ### ### ######### ######### #########
## Terminal packages - ANSI - Control operations
## (Unix specific implementation).

## This was originally taken from page 11820 (Pure Tcl Console Editor)
## of the Tcler's Wiki, however page 14693 (Reading a single character
## ...) is the same in a more self-contained manner.

# ### ### ### ######### ######### #########
## Requirements

namespace eval ::term::ansi::ctrl::unix {}

# ### ### ### ######### ######### #########
## Make command easily available

proc ::term::ansi::ctrl::unix::import {{ns ctrl} args} {
    if {![llength $args]} {set args *}
    set args ::term::ansi::ctrl::unix::[join $args " ::term::ansi::ctrl::unix::"]
    uplevel 1 [list namespace eval ${ns} [linsert $args 0 namespace import]]
    return
}

# ### ### ### ######### ######### #########
## API

# We use the <@stdin because stty works out what terminal to work with
# using standard input on some platforms. On others it prefers
# /dev/tty instead, but putting in the redirection makes the code more
# portable

proc ::term::ansi::ctrl::unix::raw {} {
    variable stty
    exec $stty raw -echo <@stdin
    return
}

proc ::term::ansi::ctrl::unix::cooked {} {
    variable stty
    exec $stty -raw echo <@stdin
    return
}

proc ::term::ansi::ctrl::unix::columns {} {
    variable tput
    return [exec $tput cols <@stdin]
}

proc ::term::ansi::ctrl::unix::rows {} {
    variable tput
    return [exec $tput lines <@stdin]
}

# ### ### ### ######### ######### #########
## Package setup

proc ::term::ansi::ctrl::unix::INIT {} {
    variable tput [auto_execok tput]
    variable stty [auto_execok stty]

    if {($stty eq "/usr/ucb/stty") &&
	($::tcl_platform(os) eq "SunOS")} {
	set stty /usr/bin/stty
    }

    if {($tput eq "") || ($stty eq "")} {
	return -code error \
		"The external requirements for the \
		use of this package (tput, stty in \
		\$PATH) are not met."
    }
    return
}

namespace eval ::term::ansi::ctrl::unix {
    variable tput {}
    variable stty {}

    namespace export columns rows raw cooked
}

::term::ansi::ctrl::unix::INIT

# ### ### ### ######### ######### #########
## Ready

package provide term::ansi::ctrl::unix 0.1.1

##
# ### ### ### ######### ######### #########