rubric/catalog/gobook.go
2026-04-13 17:25:34 +03:00

100 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package catalog
import (
"bufio"
"fmt"
"rubric/modules/gomodules"
"rubric/utils"
"strconv"
"strings"
)
var GoBook = Book{
Key: "go",
Aliases: []string{"g", "golang"},
Title: "Справочник по языку Go",
Description: "Установка, синтаксис, типы, функции, горутины, интерфейсы и многое другое",
Run: runGoBook,
}
func runGoBook(scanner *bufio.Scanner) {
for {
showGoMenu()
input, ok := ReadLine(scanner)
if !ok {
return
}
if input == "" {
continue
}
if IsQuit(input) {
fmt.Printf("\n %s%sДо свидания!%s\n\n",
utils.ColorBold, utils.ColorCyan, utils.ColorReset)
return
}
if IsBack(input) {
return
}
if n, err := strconv.Atoi(input); err == nil {
if n >= 1 && n <= len(gomodules.All) {
gomodules.All[n-1].Run()
if goWaitBack(scanner) {
return // b — выйти в главное меню
}
continue
}
fmt.Printf("\n %s%sНомер %d вне диапазона (1%d)%s\n",
utils.ColorBold, utils.ColorRed, n, len(gomodules.All), utils.ColorReset)
continue
}
if m, ok := gomodules.Find(input); ok {
m.Run()
if goWaitBack(scanner) {
return
}
continue
}
fmt.Printf("\n %s%sНе понял '%s'. Номер, ключ раздела, b — назад.%s\n",
utils.ColorBold, utils.ColorRed, input, utils.ColorReset)
}
}
// goWaitBack возвращает true если пользователь нажал b (выйти в главное меню)
func goWaitBack(scanner *bufio.Scanner) bool {
fmt.Printf("\n %s%s[Enter — назад в меню Go, b — главное меню]%s ",
utils.ColorDim, utils.ColorCyan, utils.ColorReset)
if scanner.Scan() {
return IsBack(strings.TrimSpace(scanner.Text()))
}
return false
}
func showGoMenu() {
utils.Header("Справочник Go")
fmt.Printf(" %s%sРазделы:%s\n\n",
utils.ColorBold, utils.ColorWhite, utils.ColorReset)
for i, m := range gomodules.All {
aliases := ""
if len(m.Aliases) > 0 {
aliases = fmt.Sprintf("%s [%s]%s",
utils.ColorDim, strings.Join(m.Aliases, ", "), utils.ColorReset)
}
fmt.Printf(" %s%s %2d.%s %s%-10s%s %s%-32s%s%s\n",
utils.ColorBold, utils.ColorCyan, i+1, utils.ColorReset,
utils.ColorYellow+utils.ColorBold, m.Key, utils.ColorReset,
utils.ColorWhite, m.Title, utils.ColorReset,
aliases,
)
fmt.Printf(" %s%s%s\n\n",
utils.ColorDim, m.Description, utils.ColorReset)
}
fmt.Printf(" %s%sРаздел (номер / ключ / b — главное меню / q — выход):%s ",
utils.ColorBold, utils.ColorGreen, utils.ColorReset)
}