Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

chore: remove dead xterminal code #298

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 0 additions & 71 deletions internal/x/xterminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,14 @@
package xterminal

import (
"context"
"os"
"os/signal"

"golang.org/x/crypto/ssh/terminal"
"golang.org/x/sys/unix"
)

// State differs per-platform.
type State struct {
s *terminal.State
}

// MakeRaw sets the terminal to raw.
func MakeRaw(fd uintptr) (*State, error) {
previousState, err := terminal.MakeRaw(int(fd))
if err != nil {
return nil, err
}
return &State{s: previousState}, nil
}

// MakeOutputRaw does nothing on non-Windows platforms.
func MakeOutputRaw(fd uintptr) (*State, error) { return nil, nil }

Expand All @@ -36,60 +22,3 @@ func Restore(fd uintptr, state *State) error {

return terminal.Restore(int(fd), state.s)
}

// ColorEnabled returns true on Linux if handle is a terminal.
func ColorEnabled(fd uintptr) (bool, error) {
return terminal.IsTerminal(int(fd)), nil
}

// ResizeEvent describes the new terminal dimensions following a resize.
type ResizeEvent struct {
Height uint16
Width uint16
}

// ResizeEvents sends terminal resize events.
func ResizeEvents(ctx context.Context, termFD uintptr) chan ResizeEvent {
// Use a buffered chan to avoid blocking when we emit the initial resize event.
// We send the event right away while the main routine might not be ready just yet.
events := make(chan ResizeEvent, 1)

go func() {
sigChan := make(chan os.Signal, 16) // Arbitrary large buffer size to allow for "continuous" resizing without blocking.
defer close(sigChan)

// Terminal resize event are notified using the SIGWINCH signal, start watching for it.
signal.Notify(sigChan, unix.SIGWINCH)
defer signal.Stop(sigChan)

// Emit an initial signal event to make sure the server receives our current window size.
select {
case <-ctx.Done():
return
case sigChan <- unix.SIGWINCH:
}

for {
select {
case <-ctx.Done():
return
case <-sigChan:
width, height, err := terminal.GetSize(int(termFD))
if err != nil {
return
}
event := ResizeEvent{
Height: uint16(height),
Width: uint16(width),
}
select {
case <-ctx.Done():
return
case events <- event:
}
}
}
}()

return events
}
82 changes: 0 additions & 82 deletions internal/x/xterminal/terminal_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
package xterminal

import (
"context"
"time"

"golang.org/x/crypto/ssh/terminal"
"golang.org/x/sys/windows"
)

Expand Down Expand Up @@ -36,16 +32,6 @@ func makeRaw(handle windows.Handle, input bool) (uint32, error) {
return prevState, nil
}

// MakeRaw sets an input terminal to raw and enables VT100 processing.
func MakeRaw(handle uintptr) (*State, error) {
prevState, err := makeRaw(windows.Handle(handle), true)
if err != nil {
return nil, err
}

return &State{mode: prevState}, nil
}

// MakeOutputRaw sets an output terminal to raw and enables VT100 processing.
func MakeOutputRaw(handle uintptr) (*State, error) {
prevState, err := makeRaw(windows.Handle(handle), false)
Expand All @@ -60,71 +46,3 @@ func MakeOutputRaw(handle uintptr) (*State, error) {
func Restore(handle uintptr, state *State) error {
return windows.SetConsoleMode(windows.Handle(handle), state.mode)
}

// ColorEnabled returns true if VT100 processing is enabled on the output
// console.
func ColorEnabled(handle uintptr) (bool, error) {
var state uint32
if err := windows.GetConsoleMode(windows.Handle(handle), &state); err != nil {
return false, err
}

return state&windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0, nil
}

// ResizeEvent represent the new size of the terminal.
type ResizeEvent struct {
Height uint16
Width uint16
}

func (s ResizeEvent) equal(s2 *ResizeEvent) bool {
if s2 == nil {
return false
}
return s.Height == s2.Height && s.Width == s2.Width
}

// ResizeEvents sends terminal resize events when the dimensions change.
// Windows does not have a unix.SIGWINCH equivalent, so we poll the terminal size
// at a fixed interval
func ResizeEvents(ctx context.Context, termFD uintptr) chan ResizeEvent {
// Use a buffered chan to avoid blocking if the main is not ready yet when we send the initial resize event.
events := make(chan ResizeEvent, 1)

go func() {
defer close(events)

// On windows, as we don't have a signal to know the size changed, we
// use a ticker and emit then event if the current size differs from last time we checked.
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()

var lastEvent *ResizeEvent
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
width, height, err := terminal.GetSize(int(windows.Handle(termFD)))
if err != nil {
return
}
event := ResizeEvent{
Height: uint16(height),
Width: uint16(width),
}
if !event.equal(lastEvent) {
select {
case <-ctx.Done():
return
case events <- event:
}
}
lastEvent = &event
}
}
}()

return events
}