Skip to content

Commit

Permalink
internal: v1.2.0 (#5)
Browse files Browse the repository at this point in the history
The clog package has been added, to which
the Fatal* functions have been moved, and
the Print* and Panic* functions have been
added.
  • Loading branch information
i582 authored Apr 25, 2021
1 parent 3abaee5 commit df6b986
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 59 deletions.
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
install:
go install .

check:
@echo "running tests..."
@go test -count 1 -v ./...
Expand Down
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# cfmt

**cfmt** is a small library for **simple and convenient formatted stylized output** to the console, providing an interface that is exactly the same as the standard `fmt` library.
**cfmt** is a small library for **simple and convenient formatted stylized output** to the console, providing an interface that is exactly the same as the standard `fmt` and `log` libraries.

## Table of Contents

Expand All @@ -24,12 +24,22 @@
## Install

```
go get -u -v github.com/i582/cfmt
go get -v github.com/i582/cfmt
```

## Usage

To switch to `cfmt` anywhere in the code, it is enough to add one letter `c` to `fmt` and you get all the possibilities, the library is fully compatible with the standard library `fmt`.
To switch to `cfmt` anywhere in the code, it is enough to add one letter `c` to `fmt` (or to `log`), import the required part (`cfmt` or `clog`):

```go
import (
"github.com/i582/cfmt/cmd/cfmt"
// or
"github.com/i582/cfmt/cmd/clog"
)
```

and you get all the possibilities, the library is fully compatible with the standard libraries `fmt` and `log`

### Simple usage

Expand Down Expand Up @@ -109,7 +119,7 @@ cfmt.Println("{{%s}}::flag ", flag)

### HEX colors

If the standard colors are not enough for you, then you can use the colors in the `HEX` format (note, not all terminals support all colors fully!).
If the standard colors are not enough for you, then you can use the colors in the `HEX` format (*NOTE: not all terminals support all colors fully!*).

```go
cfmt.Println("This is a {{red color}}::#ff0000")
Expand Down Expand Up @@ -207,7 +217,7 @@ And colors in HEX format. See [HEX colors](#hex-colors) part.

The existing libraries for styling the output are very powerful and this library builds on one of them ([gookit/color](https://github.com/gookit/color)). However, they are not very useful for styling certain words or sentences, since you need to use `Sprintf` and put the styled ones in a format string, which greatly reduces readability if you need to style many elements.

I believe that the library will be useful primarily for formatting ready-made text, for reference or examples. However, in other cases it should be just as convenient.
I believe that the library will be useful primarily for formatting ready-made text, for reference, or examples. However, in other cases, it should be just as convenient.

The library aims to make formatted text look readable in code, even with complex formatting.

Expand Down
53 changes: 15 additions & 38 deletions main.go → cmd/cfmt/cfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// It will look like this:
//
// cfmt.RegisterStyle("code", func(s string) string {
// return cfmt.Sprintf("{{%s}}::red|underline", s)
// return cfmt.Sprintf("{{%s}}::red|underline", s)
// })
//
// The first argument is the name by which this style will be used,
Expand All @@ -22,70 +22,47 @@ func RegisterStyle(name string, fn func(string) string) {
internal.CustomMap[name] = fn
}

// Sprint is the same as fmt.
// Sprint is the same as fmt.Sprint.
func Sprint(a ...interface{}) string {
text := fmt.Sprint(a...)
return internal.ParseAndApply(text)
return internal.ParseAndApply(fmt.Sprint(a...))
}

// Fprint is the same as fmt.
// Fprint is the same as fmt.Fprint.
func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
text := Sprint(a...)
return fmt.Fprint(w, text)
return fmt.Fprint(w, Sprint(a...))
}

// Print is the same as fmt.
// Print is the same as fmt.Print.
func Print(a ...interface{}) (n int, err error) {
return Fprint(os.Stdout, a...)
}

// Sprintln is the same as fmt.
// Sprintln is the same as fmt.Sprintln.
func Sprintln(a ...interface{}) string {
return Sprint(a...) + "\n"
}

// Fprintln is the same as fmt.
// Fprintln is the same as fmt.Fprintln.
func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
text := Sprintln(a...)
return fmt.Fprint(w, text)
return fmt.Fprint(w, Sprintln(a...))
}

// Println is the same as fmt.
// Println is the same as fmt.Println.
func Println(a ...interface{}) (n int, err error) {
return Fprintln(os.Stdout, a...)
}

// Sprintf is the same as fmt.
// Sprintf is the same as fmt.Sprintf.
func Sprintf(format string, a ...interface{}) string {
text := fmt.Sprintf(format, a...)
return internal.ParseAndApply(text)
return Sprint(fmt.Sprintf(format, a...))
}

// Fprintf is the same as fmt.
// Fprintf is the same as fmt.Fprintf.
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
text := Sprintf(format, a...)
return fmt.Fprint(w, text)
return fmt.Fprint(w, Sprintf(format, a...))
}

// Printf is the same as fmt.
// Printf is the same as fmt.Printf.
func Printf(format string, a ...interface{}) (n int, err error) {
return Fprintf(os.Stdout, format, a...)
}

// Fatalf is the same as fmt.
func Fatalf(format string, a ...interface{}) {
_, _ = Printf(format, a...)
os.Exit(1)
}

// Fatal is the same as fmt.
func Fatal(a ...interface{}) {
_, _ = Print(a...)
os.Exit(1)
}

// Fatalln is the same as fmt.
func Fatalln(a ...interface{}) {
_, _ = Println(a...)
os.Exit(1)
}
52 changes: 52 additions & 0 deletions cmd/clog/clog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package clog

import (
"log"

"github.com/i582/cfmt/cmd/cfmt"
)

// Printf is the same as log.Printf.
func Printf(format string, a ...interface{}) {
log.Print(cfmt.Sprintf(format, a...))
}

// Print is the same as log.Print.
func Print(a ...interface{}) {
log.Print(cfmt.Sprint(a...))
}

// Println is the same as log.Println.
func Println(a ...interface{}) {
log.Println(cfmt.Sprint(a...))
}

// Fatalf is the same as log.Fatalf.
func Fatalf(format string, a ...interface{}) {
log.Fatal(cfmt.Sprintf(format, a...))
}

// Fatal is the same as log.Fatal.
func Fatal(a ...interface{}) {
log.Fatal(cfmt.Sprint(a...))
}

// Fatalln is the same as log.Fatalln.
func Fatalln(a ...interface{}) {
log.Fatalln(cfmt.Sprint(a...))
}

// Panicf is the same as log.Panicf.
func Panicf(format string, a ...interface{}) {
log.Panic(cfmt.Sprintf(format, a...))
}

// Panic is the same as log.Panic.
func Panic(a ...interface{}) {
log.Panic(cfmt.Sprint(a...))
}

// Panicln is the same as log.Panicln.
func Panicln(a ...interface{}) {
log.Panicln(cfmt.Sprint(a...))
}
50 changes: 49 additions & 1 deletion tests/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package tests

import (
"fmt"
"testing"

"github.com/i582/cfmt"
"github.com/i582/cfmt/cmd/cfmt"
"github.com/i582/cfmt/cmd/clog"
)

type TestStruct struct {
Expand Down Expand Up @@ -35,4 +37,50 @@ func TestParse(t *testing.T) {
cfmt.Printf("{{{hello}}}::red|underline\n")
cfmt.Printf("{{some test struct: %v}}::red|underline\n", TestStruct{"hello", 1})
cfmt.Println("{{hello}}::red{{world}}::green")

clog.Print(cfmt.Sprintln("{{some group}}::red sfafs"))
clog.Printf("{{hex %s}}::#ff00ff sfas\n", "color group")
clog.Println("{{hello}}::red{{world}}::green")
}

func TestParseFatal(t *testing.T) {
clog.Fatal("{{hello}}::red{{world}}::green")
}

func TestParseFatalf(t *testing.T) {
clog.Fatalf("{{hello}}::red{{world}}::green")
}

func TestParseFatalln(t *testing.T) {
clog.Fatalln("{{hello}}::red{{world}}::green")
}

func TestParsePanic(t *testing.T) {
defer func() {
if err := recover(); err != nil {
fmt.Println("Passed.")
}
}()

clog.Panic("{{hello}}::red{{world}}::green")
}

func TestParsePanicf(t *testing.T) {
defer func() {
if err := recover(); err != nil {
fmt.Println("Passed.")
}
}()

clog.Panicf("{{hello}}::red{{world}}::green")
}

func TestParsePanicln(t *testing.T) {
defer func() {
if err := recover(); err != nil {
fmt.Println("Passed.")
}
}()

clog.Panicln("{{hello}}::red{{world}}::green")
}
2 changes: 1 addition & 1 deletion tests/style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tests
import (
"testing"

"github.com/i582/cfmt"
"github.com/i582/cfmt/cmd/cfmt"
"github.com/i582/cfmt/internal"
)

Expand Down

0 comments on commit df6b986

Please sign in to comment.