6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-30 18:51:03 +00:00
prologic-saltyim/cmd/salty-chat/format.go

45 lines
865 B
Go

package main
import (
"bufio"
"fmt"
"log"
"os"
"github.com/spf13/cobra"
"go.mills.io/saltyim"
)
var formatCmd = &cobra.Command{
Use: "format [<message>]",
Short: "Format a message for display",
Long: `This command formats an unencrypted message for display purposes
for display purposes. If no message is provided as the first argument, then
the message is read from stadnard input.`,
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
format(args[:]...)
},
}
func init() {
rootCmd.AddCommand(formatCmd)
}
func format(args ...string) {
if len(args) == 1 && args[0] != "-" {
fmt.Println(saltyim.FormatMessage(args[0]))
os.Exit(0)
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fmt.Println(saltyim.FormatMessage(scanner.Text()))
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}