cons

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2023 License: MIT Imports: 2 Imported by: 0

README

Golang Windows Console Package

License

Project Overview

Console is a Golang package designed for the Windows operating system, aimed at simplifying interactions and management with the console.

Key Features

  • Buffer Management: Streamline console buffer management for enhanced control.
  • Cursor Manipulation: Fine-tune the position and appearance of the console cursor.
  • Console Administration: Simplify console mode and attribute management.
  • Console Information: Easily retrieve and configure console information.

Installation

go get https://github.com/Mandarinkocka/golang-windows-console

sample

package main

import (
	"fmt"
	"log"

	cons "github.com/mandarinkocka/golang-windows-console"
)

func main() {
	cons.SetTitle("My titel")

	if cp, _ := cons.GetOutputCP(); cp != cons.Utf8 {
		cons.SetOutputCP(cons.Utf8)
	}

	hStdout, err := cons.GetStdHandle(cons.StdOutputHandle)
	if err != nil {
		log.Fatalln("GetStdHandle: ", err)
	}

	if !cons.IsEnableMode(hStdout, cons.EnableVirtualTerminalProcessing) {
		if err := cons.EnableMode(hStdout, cons.EnableVirtualTerminalProcessing); err != nil {
			log.Fatalln("EnableMode: ", err)
		}
	}

	cons.SetCursorVisible(hStdout, false)
	defer cons.SetCursorVisible(hStdout, true)

	cons.ClearScreen(hStdout)

	cons.SetCursorPosition(hStdout, cons.Coord{X: 1, Y: 1})
	fmt.Println("Press any key to exit")

	hStdin, _ := cons.GetStdHandle(cons.StdInputHandle)
	if _, err := cons.GetKey(hStdin); err != nil {
		log.Fatal(err)
	}
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearScreen

func ClearScreen(hStdout Handle) error

ClearScreen clears the console screen buffer by filling it with spaces and setting a specified text attribute.

Parameters:

hStdout: The handle to the console screen buffer.

Returns:

error If the function succeeds, the value is nil

func EnableMode

func EnableMode(hStdout Handle, flag ...int) error

EnableMode enables one or more console screen buffer modes for the specified console screen buffer.

Parameters:

hStdout (Handle): The handle to the console screen buffer.
                  This handle must have GENERIC_READ access rights.
flag ([]int): Zero or more console screen buffer modes to enable.

Returns:

error: If the function succeeds, the value is nil

func FillAttribute

func FillAttribute(hStdout Handle, attribute uint16, length int, writeCoord Coord) (uint16, error)

FillAttribute writes characters with a specified attribute to the console screen buffer a specified number of times, starting from the specified coordinates.

Parameters:

hStdout: The handle to the console screen buffer.
attribute: The attribute to use when writing to the console screen buffer.
length: The number of character cells to write.
writeCoord: The character coordinates of the first cell where characters will be written.

Returns:

uint16: The actual number of attributes written to the console screen buffer.
error: If the function succeeds, the value is nil

func FillCharacter

func FillCharacter(hStdout Handle, character rune, length int, writeCoord Coord) (uint16, error)

FillCharacter writes a character to the console screen buffer a specified number of times, starting from the specified coordinates.

Parameters:

hStdout: The handle to the console screen buffer.
character: The character to be written to the console screen buffer.
length: The number of character cells to write the character to.
writeCoord: The character coordinates of the first cell where the character will be written.

Returns:

uint16: The actual number of character written to the console screen buffer.
error: If the function succeeds, the value is nil

func GetCP

func GetCP() (uint32, error)

GetCP retrieves the input code page used by the console associated with the calling process. The console uses its input code page to convert keyboard input into the corresponding character values.

Returns:

uint32: The return value is an uint representing the code page identifier.
error:  If the function succeeds, the value is nil

func GetKey

func GetKey(hStdin Handle) (uint16, error)

GetKey reads and returns a key press event from the console input buffer.

Parameters:

hStdin: The handle to the console input buffer.

Returns:

uint16: The Unicode character code of the key pressed.
error: If the function succeeds, the value is nil

func GetMode

func GetMode(hStdout Handle) (uint32, error)

GetMode retrieves the current input mode for the console input buffer or the current output mode for the console screen buffer associated with the specified handle.

Parameters:

hStdout: The handle to the console input buffer or console screen buffer.
         This handle must have GENERIC_READ access rights.

Returns:

uint32: The current mode of the buffer
error:  If the function succeeds, the value is nil

func GetOutputCP

func GetOutputCP() (uint32, error)

GetOutputCP retrieves the output code page used by the console associated with the calling process.

Returns:

wCodePage: If the function succeeds, it returns the code page identifier and nil error.
err: If the function succeeds, the value is nil

func GetScreenBufferInfo

func GetScreenBufferInfo(hStdout Handle, lpScreenBufferInfo *ScreenBufferInfo) error

GetScreenBufferInfo retrieves information about the specified console screen buffer.

Parameters:

hStdout: The handle to the console screen buffer.
lpScreenBufferInfo: A pointer to the ScreenBufferInfo structure that receives information about the console screen buffer.

Returns:

error: If the function succeeds, the value is nil

func IsEnableMode

func IsEnableMode(hStdout Handle, flag int) bool

IsEnableMode retrieves the state of a specific console mode flag.

Parameters:

hStdout: The handle to the console screen buffer.
flag: The mode flag to be checked.

Returns:

bool: If the function succeeds, the value is true

func ReadInput

func ReadInput(hStdin Handle, lpBuffer unsafe.Pointer, length uint32, lpNumberOfEventsRead *uint32) error

ReadInput reads console input records from the specified console input buffer.

Parameters:

hStdin: The handle to the console input buffer.
lpBuffer: A pointer to an array of INPUT_RECORD structures that receive the input buffer data.
length: The size of the array pointed to by lpBuffer.
lpNumberOfEventsRead: A pointer to a variable that receives the number of input records read.

Returns:

error: If the function succeeds, the value is nil

func ScrollScreenBuffer

func ScrollScreenBuffer(hStdout Handle, lpScrollRectangle, lpClipRectangle *SmallRect, destinationOrigin Coord, lpFill CharInfo) error

ScrollScreenBuffer scrolls the contents of the console screen buffer.

Parameters:

hStdout: The handle to the console output buffer.
lpScrollRectangle: A pointer to the SMALL_RECT structure that specifies the top-left and bottom-right coordinates
                  of the console screen buffer rectangle to be moved.
lpClipRectangle: A pointer to the SMALL_RECT structure that specifies the top-left and bottom-right coordinates
                 of the console screen buffer rectangle affected by the scroll. This pointer can be NULL.
destinationOrigin: Specifies the new location of the top-left corner of the contents of lpScrollRectangle (in characters).
lpFill: Specifies the character and color attributes to be used for filling any areas left empty in the intersection
        of lpScrollRectangle and lpClipRectangle due to the move.

Returns:

error: If the function succeeds, the value is nil

func SetCP

func SetCP(wCodePage uint32) error

SetCP sets the input code page used by the console associated with the calling process. The console uses its input code page to convert keyboard input into the corresponding character values.

Parameters:

wCodePageID: The identifier of the code page to set.

Returns:

error: If the function succeeds, the value is nil

func SetCursorInfo

func SetCursorInfo(hStdout Handle, lpCursorInfo *CursorInfo) error

SetCursorInfo updates the cursor size and visibility information for the specified console screen buffer.

Parameters:

hStdout (Handle): The handle to the console screen buffer.
                  This handle must have GENERIC_READ access rights.
lpCursorInfo (*CursorInfo): A pointer to a CursorInfo structure that provides
                            new specifications for the cursor of the console screen buffer.

Returns:

error: If the function succeeds, the value is nil

func SetCursorPosition

func SetCursorPosition(hStdout Handle, pos Coord) error

SetCursorPosition sets the cursor position within the specified console screen buffer.

Parameters:

hStdout: The handle to the console screen buffer. This handle must have GENERIC_READ access rights.
pos:     The new cursor position specified as a COORD structure, measured in character units.
         Coordinates are in terms of columns and rows of character cells in the screen buffer.
         The coordinates must be within the boundaries of the console screen buffer.

Returns:

error : If the function succeeds, the value is nil

func SetCursorVisible

func SetCursorVisible(hStdout Handle, bVisible bool) error

SetCursorVisible sets the visibility of the console cursor.

Parameters:

hStdout:  The handle to the console screen buffer.
          This handle must have GENERIC_READ access rights.
bVisible: Indicates whether the cursor should be visible (true) or hidden (false).

Returns:

error If the function succeeds, the value is nil

func SetMode

func SetMode(hStdout Handle, dwMode uint32) error

SetMode sets the input or output mode for the console input buffer or console screen buffer associated with the specified handle.

Parameters:

hStdout: The handle to the console input buffer or console screen buffer.
         This handle must have GENERIC_READ access rights.
dwMode:  The mode to be set for input or output.

Returns:

error: If the function succeeds, the value is nil

func SetOutputCP

func SetOutputCP(wCodePage uint32) error

SetOutputCP SetConsoleOutputCP sets the output code page used by the console associated with the calling process. The console uses its output code page to convert character values written by various output functions into the images displayed in the console window.

Parameters:

wCodePageID: The identifier of the code page to set.

Returns:

error: If the function succeeds, the value is nil

func SetTitle

func SetTitle(title string) error

SetTitle sets the title of the current console window.

Parameters:

title: A string to be displayed in the title bar of the console window.
       The total size must be less than 64K.

Returns:

error:  If the function succeeds, the value is nil

func WriteAttribute

func WriteAttribute(hStdout Handle, attribute uint16, length uint32, writeCoord Coord) (uint16, error)

WriteAttribute writes a specified attribute to the console screen buffer.

Parameters:

hStdout: The handle to the console output buffer.
attribute: The attribute to be written to the console screen buffer.
length: The number of screen buffer character cells to which the attribute should be copied.
writeCoord: The coordinates of the first character cell in the console screen buffer to receive the attribute.

Returns:

uint16: The actual number of attributes written to the console screen buffer.
error: If the function succeeds, the value is nil

func WriteCharacter

func WriteCharacter(hStdout Handle, character uint16, length uint32, writeCoord Coord) (uint16, error)

WriteCharacter writes a specified character to the console screen buffer.

Parameters:

hStdout: The handle to the console output buffer.
character: The character to be written to the console screen buffer.
length: The number of characters to be written.
writeCoord: The coordinates of the first character cell in the console screen buffer to receive the character.

Returns:

uint16: The actual number of characters written to the console screen buffer.
error: If the function succeeds, the value is nil

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL