Coder Social home page Coder Social logo

hymkor / go-windows1x-virtualterminal Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 33 KB

Enables Windows10 and 11's native ESCAPE SEQUENCES and TERMINAL INPUTS

Home Page: https://pkg.go.dev/github.com/hymkor/go-windows1x-virtualterminal

License: MIT License

Go 100.00%
escape-sequence go golang terminal windows

go-windows1x-virtualterminal's Introduction

go-windows1x-virtualterminal

This package enables following features on Windows10 and Windows11:

  • Enabling ANSI Escape Sequences for stdout and stderr
  • Enabling the input of keyboard arrow keys in the form of codes such as ESC[A to ESC[D. from stdin

If the platform is not Windows, the original terminal often supports escape sequences, so each function exits silently without causing an error.

If the platform is Windows older than version 10, each function will return virtualterminal.ErrNotSupported because the command prompt does not support these features.

Enabling ANSI Escape Sequences

package main

import (
    "errors"
    "fmt"
    "os"

    "github.com/hymkor/go-windows1x-virtualterminal"
)

func mains() error {
    fmt.Println("Defaut-Mode: \x1B[32;1m!!!!\x1B[0m\n")
    closer, err := virtualterminal.EnableStdout()
    if err != nil {
        if errors.Is(err, virtualterminal.ErrNotSupported) {
            return fmt.Errorf("This machine can not use ANSI-ESCAPE-SEQUENCE: %w", err)
        }
        return err
    }
    fmt.Println("Ansi-Mode: \x1B[32;1m!!!!\x1B[0m\n")
    closer()
    fmt.Println("Default-Mode: \x1B[32;1m!!!!\x1B[0m\n")
    return nil
}

func main() {
    if err := mains(); err != nil {
        fmt.Fprintln(os.Stderr, err.Error())
        os.Exit(1)
    }
}

example.png

Read a key without Enter-key

package main

import (
    "fmt"
    "os"
    "strings"

    "github.com/hymkor/go-windows1x-virtualterminal"
    "github.com/hymkor/go-windows1x-virtualterminal/keyin"
)

func mains() error {
    // EnableStdin should be called once in one application.
    close1, err := virtualterminal.EnableStdin()
    if err != nil {
        return err
    }
    defer close1()

    // Raw switches the terminal to raw-mode.
    // It should be called once in one readline loop.
    close2, err := keyin.Raw()
    if err != nil {
        return err
    }
    defer close2()

    fmt.Print("Hit any key: ")

    ch, err := keyin.Get()
    if err != nil {
        return err
    }

    ch = strings.ReplaceAll(ch, "\x1B", "<ESC>")
    fmt.Println(ch)
    return nil
}

func main() {
    if err := mains(); err != nil {
        fmt.Fprintln(os.Stderr, err.Error())
        os.Exit(1)
    }
}

Enabling the input of keyboard arrow keys

package main

import (
    "fmt"
    "io"
    "os"

    "golang.org/x/term"

    "github.com/hymkor/go-windows1x-virtualterminal"
)

func readLine() (string, error) {
    oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
    if err != nil {
        return "", err
    }
    defer term.Restore(int(os.Stdin.Fd()), oldState)

    disableIn, err := virtualterminal.EnableStdin()
    if err != nil {
        return "", err
    }
    defer disableIn()

    disableOut, err := virtualterminal.EnableStdout()
    if err != nil {
        return "", err
    }
    defer disableOut()

    terminal := term.NewTerminal(&struct {
        io.Reader
        io.Writer
    }{os.Stdin, os.Stdout}, "> ")

    return terminal.ReadLine()
}

func main() {
    line, err := readLine()
    if err != nil {
        fmt.Fprintln(os.Stderr, err.Error())
        os.Exit(1)
    }
    fmt.Println("Line:", line)
}

go-windows1x-virtualterminal's People

Contributors

hymkor avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.