Skip to content

Commit

Permalink
Feat [Golang] [Package] Banner cli (#33)
Browse files Browse the repository at this point in the history
* Feat [Golang] [Package] Banner cli

- [+] feat(banner.go): add PrintBinaryBanner function to print a binary representation of a banner
- [+] feat(banner.go): add PrintAnimatedBanner function to print a simple animated banner by scrolling the message

* Docs [Golang] [Package] Banner cli

- [+] chore(banner.go): update package comment and file path comment
- [+] feat(banner.go): add comments to PrintBinaryBanner function
- [+] feat(banner.go): add comments to PrintAnimatedBanner function

* Feat [Golang] [Package] [Banner Cli] Typing Animated

- [+] feat(banner.go): add PrintTypingBanner function to print a message with a typing animation effect

* Refactor [Golang] [Package] Banner Cli

- [+] refactor(banner.go): rename package from "banner" to "bannercli"

* Feat [Golang] [Module] Main Command

- [+] feat(main.go): add typing banner for "ChatGPT Session Exporter" with 100ms delay
- [+] refactor(main.go): format error message before passing it to PrintTypingBanner

* Docs [Golang] [Package] Banner cl

- [+] chore(banner.go): rename package from "banner" to "bannercli"
- [+] docs(banner.go): add example usage of PrintTypingBanner function

* Docs [Golang] [Package] Banner CLI

- [+] chore(banner.go): add copyright notice to PrintBinaryBanner, PrintAnimatedBanner, and PrintTypingBanner functions

* Docs [Golang] [Package] [BannerCLI] Fix Typo

- [+] chore(banner.go): fix package import path in file header

* Docs [Golang] [Package] [BannerCLI] Add missing comment

- [+] chore(banner.go): add missing comment for PrintTypingBanner function
  • Loading branch information
H0llyW00dzZ authored Dec 11, 2023
1 parent 46fa46c commit ad5fa65
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
58 changes: 58 additions & 0 deletions bannercli/banner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Package bannercli provides functionality to print different styles of banners
// to the terminal. These styles include binary representation and simple
// animation effects to enhance the visual presentation of CLI applications.
// # bannercli/banner.go
// Example usage:
//
// bannercli.PrintTypingBanner("ChatGPT Session Exporter", 100*time.Millisecond)
//
// Copyright (c) 2023 H0llyW00dzZ
package bannercli

import (
"fmt"
"strings"
"time"
)

// PrintBinaryBanner prints a binary representation of a banner.
// Each character of the message is converted into its binary form.
// Spaces between words are widened to enhance readability.
//
// Copyright (c) 2023 H0llyW00dzZ
func PrintBinaryBanner(message string) {
banner := strings.ReplaceAll(message, " ", " ")
for _, char := range banner {
fmt.Printf(" %08b", char)
}
fmt.Println()
}

// PrintAnimatedBanner prints a simple animated banner by scrolling the message
// horizontally across the terminal. The animation repeats the number of times
// specified by the `repeat` parameter with a delay between each frame as
// specified by the `delay` parameter.
//
// Copyright (c) 2023 H0llyW00dzZ
func PrintAnimatedBanner(message string, repeat int, delay time.Duration) {
for r := 0; r < repeat; r++ {
for i := 0; i < len(message); i++ {
fmt.Print("\r" + strings.Repeat(" ", i) + message)
time.Sleep(delay)
}
}
fmt.Println()
}

// PrintTypingBanner prints the message with a typing animation effect.
//
// Each character appears sequentially with a delay, simulating a typing effect.
//
// Copyright (c) 2023 H0llyW00dzZ
func PrintTypingBanner(message string, delay time.Duration) {
for _, char := range message {
fmt.Printf("%c", char)
time.Sleep(delay)
}
fmt.Println()
}
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"strconv"
"strings"
"syscall"
"time"

"github.com/H0llyW00dzZ/ChatGPT-Next-Web-Session-Exporter/bannercli"
"github.com/H0llyW00dzZ/ChatGPT-Next-Web-Session-Exporter/exporter"
"github.com/H0llyW00dzZ/ChatGPT-Next-Web-Session-Exporter/filesystem"
"github.com/H0llyW00dzZ/ChatGPT-Next-Web-Session-Exporter/interactivity"
Expand Down Expand Up @@ -47,6 +49,7 @@ const (
// main initializes the application, setting up context for cancellation and
// starting the user interaction flow for data processing and exporting.
func main() {
bannercli.PrintTypingBanner("ChatGPT Session Exporter", 100*time.Millisecond)
// Prepare a cancellable context for handling graceful shutdown.
// This context will be passed down to functions that support cancellation.
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -110,10 +113,12 @@ func main() {
func handleInputError(err error) {
if err == context.Canceled || err == io.EOF {
// Handle a context cancellation or EOF, if applicable
fmt.Println("\n[GopherHelper] Exiting gracefully...\nReason: Operation canceled or end of input. Exiting program.")
bannercli.PrintTypingBanner("\nReason: Operation canceled or end of input. Exiting program.", 100*time.Millisecond)
os.Exit(0)
} else {
fmt.Printf("\n[GopherHelper] Error reading input: %s\n", err)
// Format the error message before passing it to PrintTypingBanner
errorMessage := fmt.Sprintf("\n[GopherHelper] Error reading input: %s\n", err)
bannercli.PrintTypingBanner(errorMessage, 100*time.Millisecond)
os.Exit(1)
}
}
Expand Down

0 comments on commit ad5fa65

Please sign in to comment.