2024-03-20 12:36:55 +01:00
|
|
|
/*
|
|
|
|
|
Copyright © 2024 Thomas von Dein
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-03-19 18:12:02 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2025-09-17 20:48:53 +02:00
|
|
|
"log"
|
2024-03-20 12:36:55 +01:00
|
|
|
"log/slog"
|
2024-03-19 18:12:02 +01:00
|
|
|
"sort"
|
|
|
|
|
)
|
|
|
|
|
|
2024-03-20 12:36:55 +01:00
|
|
|
// Output a list of hardcoded FN code templates
|
|
|
|
|
func ListTemplates(conf *Config, output io.Writer) {
|
|
|
|
|
slog.Debug("Listing configured templates", "templates", conf.Templates)
|
|
|
|
|
|
2024-03-19 18:12:02 +01:00
|
|
|
names := []string{}
|
|
|
|
|
|
2024-03-20 12:36:55 +01:00
|
|
|
for name := range conf.Templates {
|
2024-03-19 18:12:02 +01:00
|
|
|
names = append(names, name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sort.Strings(names)
|
|
|
|
|
|
|
|
|
|
for _, name := range names {
|
2025-09-17 20:48:53 +02:00
|
|
|
_, err := fmt.Fprintln(output, name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("failed to print to output: %w", err)
|
|
|
|
|
}
|
2024-03-19 18:12:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-20 12:36:55 +01:00
|
|
|
// Columnar output
|
|
|
|
|
func PrintColumns(conf *Config, names []string, output io.Writer) error {
|
2024-03-19 18:12:02 +01:00
|
|
|
count := len(names)
|
|
|
|
|
|
|
|
|
|
// no need for the hassle to calculate columns
|
2024-03-20 12:36:55 +01:00
|
|
|
if count <= conf.Columns {
|
2024-03-19 18:12:02 +01:00
|
|
|
for _, name := range names {
|
|
|
|
|
fmt.Fprintln(output, name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get a transposed list of columns
|
2024-03-20 12:36:55 +01:00
|
|
|
padlist, max := Getcolumns(names, conf.Columns)
|
2024-03-19 18:12:02 +01:00
|
|
|
|
|
|
|
|
// make sure there's enough spacing between the columns
|
|
|
|
|
format := fmt.Sprintf("%%-%ds", max+1)
|
|
|
|
|
|
|
|
|
|
for _, row := range padlist {
|
|
|
|
|
for _, word := range row {
|
2025-09-17 20:48:53 +02:00
|
|
|
_, err := fmt.Fprintf(output, format, word)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("failed to print to output: %w", err)
|
|
|
|
|
}
|
2024-03-19 18:12:02 +01:00
|
|
|
}
|
|
|
|
|
fmt.Fprintln(output)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Getcolumns(names []string, columns int) ([][]string, int) {
|
|
|
|
|
words := len(names)
|
|
|
|
|
max := 0
|
|
|
|
|
|
|
|
|
|
// we'll have a list of $columns columns
|
|
|
|
|
padlist := make([][]string, columns)
|
|
|
|
|
|
|
|
|
|
// initialize'em
|
|
|
|
|
for col := 0; col < columns; col++ {
|
|
|
|
|
padlist[col] = []string{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fill from input
|
|
|
|
|
for idx := 0; idx < words; idx += columns {
|
|
|
|
|
for col := 0; col < columns; col++ {
|
|
|
|
|
if idx+col >= words {
|
|
|
|
|
padlist[col] = append(padlist[col], "")
|
|
|
|
|
} else {
|
|
|
|
|
padlist[col] = append(padlist[col], names[idx+col])
|
|
|
|
|
length := len(names[idx+col])
|
|
|
|
|
if length > max {
|
|
|
|
|
max = length
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// turn columns to rows
|
|
|
|
|
return Transpose(padlist), max
|
|
|
|
|
}
|