gonsole

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2022 License: GPL-3.0 Imports: 9 Imported by: 2

README

Gonsole

Gonsole provides some simple operations which improve the ux inside the console

Prerequisites

This project makes use of go 1.18 which is currently in beta so make sure your project is set up properly

Install

Run go get github.com/MangioneAndrea/Gonsole to get the package

Usage

Console

The console is fairly simple to use.

  • The first parameter is the message to print
  • The second parameter is a flag to put before the message (optional)
  • The third parameter is a filter function. It allows to print the message conditionally (optional)
Colors

Colored console

package main

import "github.com/MangioneAndrea/gonsole"

func main() {
	gonsole.Error("File not found", "Error")
	gonsole.Log("Simple message", "Log")
	gonsole.Warn("Wrong password", "Warning")
	gonsole.Success("Action successful", "Success")
}

Conditions

Conditional console

package main

import (
	"github.com/MangioneAndrea/gonsole"
	"os"
)

// ShowIfNotEmpty user defined conditional for the console to show
var ShowIfNotEmpty gonsole.ShowIf = func(elem interface{}) bool {
	return elem != ""
}

func main() {
	// this file is in the same folder as the runnable
	data, err := os.ReadFile("./examples/console/condition/example.txt")
	gonsole.Error(err, "Error", gonsole.ShowIfNotNil)
	gonsole.Success(string(data), "Data", ShowIfNotEmpty)
	// This file does not exist
	data, err = os.ReadFile("./path/to/nowhere.go")
	gonsole.Error(err, "Error", gonsole.ShowIfNotNil)
	gonsole.Success(string(data), "Data", ShowIfNotEmpty)
}

CLI

The cli is a bit advanced and can be used mostly for small scripts. Each stage has a pointer as last parameter. The result is stored there

Conditional console

package main

import "github.com/MangioneAndrea/gonsole"

func main() {
	var name string
	var choco bool
	var color string
	var languages []string

	gonsole.Cli().
		Input("What's your name?", &name).
		Confirm("Do you like chocolate?", &choco).
		SelectOne("What's your favourite colour?", []string{"red", "blue", "yellow", "cyan"}, &color).
		SelectMany("What languages do you speak?", []string{"English", "German", "Italian", "French"}, &languages)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Default = Color("")
	Black   = Color("\033[1;30m%s\033[0m")
	Red     = Color("\033[1;31m%s\033[0m")
	Green   = Color("\033[1;32m%s\033[0m")
	Yellow  = Color("\033[1;33m%s\033[0m")
	Purple  = Color("\033[1;34m%s\033[0m")
	Magenta = Color("\033[1;35m%s\033[0m")
	Teal    = Color("\033[1;36m%s\033[0m")
	White   = Color("\033[1;37m%s\033[0m")
)
View Source
var ActiveQuestionColor tcell.Color = tcell.ColorForestGreen
View Source
var QuestionColor tcell.Color = tcell.ColorViolet
View Source
var SelectedLineColor tcell.Color = tcell.ColorCadetBlue

Functions

func Cli

func Cli() *cli

func Error

func Error(a interface{}, props ...interface{})

Error Print a text red

func Log

func Log(a interface{}, props ...interface{})

Log Print a text

func Print

func Print(a interface{}, desc interface{}, showIf ShowIf, color Color)

func Success

func Success(a interface{}, props ...interface{})

Success Print a text green

func Warn

func Warn(a interface{}, props ...interface{})

Warn Print a text yellow

Types

type Color

type Color string

type ShowIf

type ShowIf func(elem interface{}) bool
var ShowAlways ShowIf = func(elem interface{}) bool { return true }
var ShowIfNotNil ShowIf = func(elem interface{}) bool { return elem != nil }

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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