Skip to content
This repository has been archived by the owner on Nov 25, 2021. It is now read-only.
/ term-tricks Public archive

Cool library for cool terminal stuff

License

Notifications You must be signed in to change notification settings

mercxry/term-tricks

Repository files navigation

Term(inal) Tricks

asciicast

Cool library for cool terminal stuff, it's a mixed library, more towards a personal use, but don't fear pushing merge requests!

Supports only Linux and MacOS

Table of Content

Installation

go get -u github.com/mercxry/term-tricks

Features

  • Create and Kill an Alternate Buffer
  • Hide and Show the Terminal Cursor
  • Some Commands Bindings for Golang (I.E: clear)
  • More coming soon!

Example

// Copyright 2019, Matteo Martellini
//
// Licensed under ISC, you can obtain a copy of the license
// inside the "LICENSE" file, on the root directory.

package main

import (
	"fmt"
	"time"

	termtricks "github.com/mercxry/term-tricks"
)

func main() {
	// Hijacks SIGTERM (ctrl+c)
	termtricks.GracefulShutdown(safeExit)

	// Create an alternate buffer and hides the cursor
	termtricks.CreateAltBuffer()
	termtricks.HideCursor()

	// Actions on application exit
	defer func() {
		// Kills the alternate buffer and shows the cursor
		termtricks.KillAltBuffer()
		termtricks.ShowCursor()
	}()

	// Alternate buffer valid for 10 iterations (10s)
	for i := 5; i >= 1; i-- {
		// Reset cursor position to x=1, y=1
		termtricks.ResetCursorPos()

		fmt.Println("Hello World!")
		fmt.Println()

		// Handle the '1 second' typo
		if i == 1 {
			fmt.Printf("This message expires in... %d second\n", i)
		} else {
			fmt.Printf("This message expires in... %d seconds\n", i)
		}

		// Waits a second before going to the next loop iteration
		time.Sleep(time.Second)

		// Clears the terminal (in this case the alternate buffer screen)
		termtricks.Clear()
	}
}

// Actions to do for a clean exit
func safeExit() {
	// Kills the alternate buffer and shows the cursor
	termtricks.KillAltBuffer()
	termtricks.ShowCursor()
}

License

ISC

About

Cool library for cool terminal stuff

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages