admin.go (view raw)
1//
2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
3//
4// Permission to use, copy, modify, and distribute this software for any
5// purpose with or without fee is hereby granted, provided that the above
6// copyright notice and this permission notice appear in all copies.
7//
8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16package main
17
18/*
19#include <termios.h>
20*/
21import "C"
22import (
23 "fmt"
24 "io/ioutil"
25 "log"
26 "os"
27)
28
29func adminscreen() {
30 log.SetOutput(ioutil.Discard)
31 stdout := os.Stdout
32 esc := "\x1b"
33 smcup := esc + "[?1049h"
34 rmcup := esc + "[?1049l"
35
36 hidecursor := func() {
37 }
38 showcursor := func() {
39 }
40 movecursor := func(x, y int) {
41 stdout.WriteString(fmt.Sprintf(esc+"[%d;%dH", x, y))
42 }
43 clearscreen := func() {
44 stdout.WriteString(esc + "[2J")
45 }
46
47 savedtio := new(C.struct_termios)
48 C.tcgetattr(1, savedtio)
49 restore := func() {
50 stdout.WriteString(rmcup)
51 showcursor()
52 C.tcsetattr(1, C.TCSAFLUSH, savedtio)
53 }
54 defer restore()
55
56 init := func() {
57 tio := new(C.struct_termios)
58 C.tcgetattr(1, tio)
59 tio.c_lflag = tio.c_lflag & ^C.uint(C.ECHO|C.ICANON)
60 C.tcsetattr(1, C.TCSADRAIN, tio)
61
62 hidecursor()
63 stdout.WriteString(smcup)
64 clearscreen()
65 movecursor(1, 1)
66 }
67
68 init()
69
70 for {
71 var buf [1]byte
72 os.Stdin.Read(buf[:])
73 c := buf[0]
74 switch c {
75 case 'q':
76 return
77 default:
78 os.Stdout.Write(buf[:])
79 }
80 }
81}