messengerutils

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2023 License: MIT Imports: 6 Imported by: 1

README

Messenger Utils

Lint Code Base

This Go package provides a simple utility for printing informational and error messages to the console. It is designed to allow for easy management of logging verbosity and formatting of output. Additionally, it includes a simple event system that allows multiple listeners to subscribe and receive notifications when an event is emitted.

Installation

To install this package, simply run:

go get github.com/benni347/messengerutils

Usage

First, import the package:

import (
"github.com/benni347/messengerutils"
)

Create a MessengerUtils instance with the desired verbosity:

messenger := &messengerutils.MessengerUtils{
Verbose: true,
}

Use PrintInfo to print informational messages when the verbose flag is set to true:

messenger.PrintInfo("This is an informational message.")

Output:

INFO: This is an informational message.

Use PrintError to print error messages:

err := errors.New("This is an error message.")
messengerutils.PrintError("An error occurred", err)

Output:

ERROR: An error occurred: This is an error message.

Create an Event instance:

event := &messengerutils.Event{}

Subscribe to the event with a listener function:

event.Subscribe(func(data interface{}) {
    fmt.Printf("Received data: %v\n", data)
})

Emit an event with data:

event.Emit("Some data")

Output:

Received data: Some data

API

MessengerUtils struct
    Verbose: A boolean flag to determine if the PrintInfo method should output messages. If set to true, messages will be printed; otherwise, they will be silenced.
Methods
    PrintError(message string, err error): Formats and prints an error message to the console with a bold "ERROR:" label.
Event struct
    listeners: An array of listener functions to be called when the event is emitted.
    lock: A mutex used for ensuring thread-safety when modifying the listeners array or emitting events.
Event Methods
    Subscribe(listener func(interface{})): Adds a listener function to the Event object.
    Emit(data interface{}): Triggers the event, notifying all subscribed listeners with the provided data.

License

This package is released under the MIT License. See the LICENSE file for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintError

func PrintError(message string, err error)

PrintError formats and prints an error message to standard output. @param {string} message - A string representing the error message to display. @param {Error} err - An error object providing more information about the error. The function formats the error message with the provided message and error object, then prints it to the console with a bold "ERROR:" label using ANSI escape codes. @returns {void}

func PrintToDo added in v0.0.13

func PrintToDo(message ...interface{})

PrintToDo formats and prints a to-do message to standard output.

@param {interface{}} message - An array of any type that forms the to-do message to display.

The function checks each element in the message. If it is a time.Time type,
it is formatted according to RFC3339. All elements are then converted into
their string representations, concatenated into a single string,
then formatted and printed the string to the console with a bold "TODO:" label
using ANSI escape codes.

@returns {void}

func ValidateEmailRegex added in v0.3.0

func ValidateEmailRegex(email string) bool

ValidateEmailRegex checks if the provided email string matches the regex for a valid email address. @param {string} email - The email address to validate. @returns {bool} - True if the email is valid, false otherwise.

Types

type Event added in v0.0.8

type Event struct {
	// contains filtered or unexported fields
}

Event is a simple event system that allows multiple listeners to subscribe and receive notifications when an event is emitted. @typedef {Object} Event @property {Array<Function>} listeners - An array of listener functions to be called when the event is emitted. @property {sync.Mutex} lock - A mutex used for ensuring thread-safety when modifying the listeners array or emitting events.

func (*Event) Emit added in v0.0.8

func (e *Event) Emit(data interface{})

Emit triggers the event, notifying all subscribed listeners with the provided data. @method @memberof Event @param {interface{}} data - The data to be passed to each listener when the event is emitted. @returns {void}

func (*Event) Subscribe added in v0.0.8

func (e *Event) Subscribe(listener func(interface{}))

Subscribe adds a listener function to the Event object. @method @memberof Event @param {Function} listener - A function to be called when the event is emitted. It takes a single argument, which is the data passed when emitting the event. @returns {void}

type MessengerUtils

type MessengerUtils struct {
	Verbose bool
}

func (*MessengerUtils) PrintInfo

func (m *MessengerUtils) PrintInfo(message ...interface{})

PrintInfo prints the provided message to the console with the prefix "INFO" in bold, if the MessengerUtils's verbose flag is set to true. The function accepts a variadic string parameter and concatenates all strings before printing. @param {string} message - One or more strings to print as the message. @returns {void}

Jump to

Keyboard shortcuts

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