Compare commits
No commits in common. "master" and "feature/progress" have entirely different histories.
master
...
feature/pr
11
.drone.yml
11
.drone.yml
|
@ -1,11 +0,0 @@
|
||||||
---
|
|
||||||
kind: pipeline
|
|
||||||
type: docker
|
|
||||||
name: default
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: test
|
|
||||||
image: golang:1.13
|
|
||||||
commands:
|
|
||||||
- go get ./...
|
|
||||||
- go test -v -race -cover -coverprofile ./coverage.out ./...
|
|
258
README.md
258
README.md
|
@ -3,188 +3,181 @@
|
||||||
[![Go version](https://img.shields.io/badge/go_version-1.11-blue.svg)](https://golang.org/doc/go1.11)
|
[![Go version](https://img.shields.io/badge/go_version-1.11-blue.svg)](https://golang.org/doc/go1.11)
|
||||||
[![Go Report Card](https://goreportcard.com/badge/git.xdrm.io/go/clifmt)](https://goreportcard.com/report/git.xdrm.io/go/clifmt)
|
[![Go Report Card](https://goreportcard.com/badge/git.xdrm.io/go/clifmt)](https://goreportcard.com/report/git.xdrm.io/go/clifmt)
|
||||||
[![Go doc](https://godoc.org/git.xdrm.io/go/clifmt?status.svg)](https://godoc.org/git.xdrm.io/go/clifmt)
|
[![Go doc](https://godoc.org/git.xdrm.io/go/clifmt?status.svg)](https://godoc.org/git.xdrm.io/go/clifmt)
|
||||||
[![Build Status](https://drone.xdrm.io/api/badges/go/clifmt/status.svg)](https://drone.xdrm.io/go/clifmt)
|
[![buddy branch](https://app.buddy.works/xdrmbracketsdev/clifmt/repository/branch/master/badge.svg?token=33f90a953be299fc439c760e2eab36c708f8ea1f87f1159dd77924589b364b2d "buddy branch")](https://app.buddy.works/xdrmbracketsdev/clifmt/repository/branch/master)
|
||||||
|
|
||||||
|
|
||||||
Simple utility written in `go` that extends the standard `fmt.Sprintf` and `fmt.Printf` functions. It allows you to set foreground/background color, **bold**, <u>underlined</u>, _italic_ text and [hyperlinks](some-url).
|
Simple utility written in `go` that extends the standard `fmt.Sprintf` and `fmt.Printf` functions. It allows you to set foreground/background color, **bold**, <u>underlined</u> and _italic_ text.
|
||||||
|
|
||||||
<!-- toc -->
|
<!-- toc -->
|
||||||
|
|
||||||
- [I. How to use](#i-how-to-use)
|
- [(1) Format](#1-format)
|
||||||
* [1) Requirements](#1-requirements)
|
* [[Colorization]](#colorization)
|
||||||
* [2) Installation](#2-installation)
|
+ [Base format](#base-format)
|
||||||
* [3) Usage](#3-usage)
|
* [Example](#example)
|
||||||
+ [a) As a library](#a-as-a-library)
|
+ [Foreground only](#foreground-only)
|
||||||
+ [b) As an executable](#b-as-an-executable)
|
* [Example](#example-1)
|
||||||
- [II. Format syntax](#ii-format-syntax)
|
+ [Background only](#background-only)
|
||||||
* [1) Text style](#1-text-style)
|
* [Example](#example-2)
|
||||||
- [III. Animations](#iii-animations)
|
* [[Markdown-like format]](#markdown-like-format)
|
||||||
- [IV. Screenshots](#iv-screenshots)
|
+ [Bold format](#bold-format)
|
||||||
|
* [Example](#example-3)
|
||||||
|
+ [Italic format](#italic-format)
|
||||||
|
* [Example](#example-4)
|
||||||
|
+ [Underline format](#underline-format)
|
||||||
|
* [Example](#example-5)
|
||||||
|
+ [Hyperlink format](#hyperlink-format)
|
||||||
|
* [Example](#example-6)
|
||||||
|
- [(2) Screenshot](#2-screenshot)
|
||||||
* [Colorizing format example :](#colorizing-format-example-)
|
* [Colorizing format example :](#colorizing-format-example-)
|
||||||
* [Markdown-like format example](#markdown-like-format-example)
|
* [Markdown-like format example](#markdown-like-format-example)
|
||||||
- [V. Incoming features](#v-incoming-features)
|
- [(3) Incoming features](#3-incoming-features)
|
||||||
|
|
||||||
<!-- tocstop -->
|
<!-- tocstop -->
|
||||||
|
|
||||||
----
|
----
|
||||||
## I. How to use
|
|
||||||
|
|
||||||
### 1) Requirements
|
## (1) Format
|
||||||
|
|
||||||
The package **clifmt** can be used as a `go` library or as an executable. In either case, you need :
|
|
||||||
|
|
||||||
- any recent linux system (_has not been tested over other OSes_)
|
|
||||||
- `go` installed (_has not been tested under version **1.11**_)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### 2) Installation
|
### [Colorization]
|
||||||
|
|
||||||
Simply launch the following command in your favorite terminal
|
#### Base format
|
||||||
|
|
||||||
```bash
|
|
||||||
$ go get -u git.xdrm.io/go/clifmt
|
|
||||||
```
|
|
||||||
|
|
||||||
> It will download the project sources into _`$GOPATH`/src/git.xdrm.io/go/clifmt_.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### 3) Usage
|
|
||||||
|
|
||||||
#### a) As a library
|
|
||||||
|
|
||||||
You must import the library into your program with
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "git.xdrm.io/go/clifmt"
|
${<target text>}(<fg>:<bg>)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
- `<target text>` is the text that will be colorized.
|
||||||
|
|
||||||
|
- `<fg>` is the name of the foreground color (*c.f. [color list](https://git.xdrm.io/go/clifmt/src/master/colors.go#L15)*), or an hex color (*e.g.`#00ffaa`, `#0fa`*).
|
||||||
|
|
||||||
|
- `<bg>` is the name of the background color with the same syntax as for the foreground.
|
||||||
|
|
||||||
|
|
||||||
Then, the following methods will be available
|
|
||||||
|
###### Example
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// Printf wraps the standard fmt.Printf() features but adds formatting capabilities
|
clifmt.Printf("normal text ${red text over black}(red:#000) normal text")
|
||||||
func Printf(fmt string, args ...interface{}) error
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> Note that it is not recommended to nest the different coloring formats.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### Foreground only
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// Sprintf acts as 'Printf' but returns the string instead of printing it
|
${<target text>}(<fg>)
|
||||||
func Sprintf(fmt string, args ...interface{}) (string, error)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
- `<target text>` is the text that will be colorized.
|
||||||
|
- `<fg>` is the name of the foreground color.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
###### Example
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// Printpf acts as 'Printf' but takes as arguments either standard fmt.Printf arguments, or channels that will update the output when provided with correct values.
|
clifmt.Printf("normal text ${red text}(red) normal text")
|
||||||
func Printpf(fmt string, args ...interface{}) (error)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### b) As an executable
|
#### Background only
|
||||||
|
|
||||||
You must run from your terminal
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ go get -u git.xdrm.io/go/clifmt/cmd/clifmt
|
|
||||||
```
|
|
||||||
The `clifmt` executable will be available in your $GOBIN directory.
|
|
||||||
|
|
||||||
> WARNING: The executable tool is a Work In Progress, it is not stable for now. You can use `clifmt --help` anyway to see details on the format, coloring, etc.
|
|
||||||
|
|
||||||
|
|
||||||
----
|
|
||||||
|
|
||||||
## II. Format syntax
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### 1) Text style
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
The format has been designed with the markdown syntax in mind, but has some differences due to implementation issues and for stability.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
The format is better described by the sample below than explanations :
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// markdown-like
|
${<target text>}(:<bg>)
|
||||||
Printf("some normal text")
|
|
||||||
Printf("**some bold text**")
|
|
||||||
Printf("*some italic text*")
|
|
||||||
Printf("_some underline text_")
|
|
||||||
Printf("[link label](http://link_url)")
|
|
||||||
|
|
||||||
// colors
|
|
||||||
Printf("${red text}(red)")
|
|
||||||
Printf("${red text over blue background}(red:blue)")
|
|
||||||
Printf("${blue background text}(:blue)")
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The code below will print the following result :
|
- `<target text>` is the text that will be colorized.
|
||||||
|
- `<bg>` is the name of the background color.
|
||||||
![definition example result](https://0x0.st/zrtE.png)
|
|
||||||
|
|
||||||
> Any syntax feature (_e.g. bold, color, hyperlink, ..._) can be included in any other. In the same way any syntax feature can be interlaced (_e.g. "startA startB stopA stopB"_) with each other.
|
|
||||||
|
|
||||||
> Note that there can be some issues with interlaced hyperlinks as it is supported as other syntax features. But it works perfectly when used alone.
|
|
||||||
|
|
||||||
> Color names (_e.g. **red**, **blue**_) can be replaced by their hexadecimal representation (_e.g. **#ff0000**, **#0000ff**_) or the short version (_e.g. **#f00**, **#00f**_).
|
|
||||||
|
|
||||||
----
|
|
||||||
|
|
||||||
## III. Animations
|
|
||||||
|
|
||||||
The **Printpf** method allows you to pass [channels](https://tour.golang.org/concurrency/2) among ordinary arguments. It allows you to animate the text you want to print each time you send data on a channel argument.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
The example below shows a simple progress bar using markdown-like syntax, colors and animations :
|
###### Example
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
clifmt.Printf("normal text ${text over red}(#ff0000) normal text")
|
||||||
|
|
||||||
import (
|
|
||||||
"git.xdrm.io/go/clifmt"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// (1) animated values
|
|
||||||
var (
|
|
||||||
status = make(chan interface{})
|
|
||||||
color = make(chan interface{})
|
|
||||||
progress = make(chan interface{})
|
|
||||||
)
|
|
||||||
|
|
||||||
// (2) print your animated values
|
|
||||||
go clifmt.Printpf("[${%s}(%s)] **%d**%%", status, color, progress)
|
|
||||||
|
|
||||||
// (3) animate values
|
|
||||||
status <- "download"
|
|
||||||
color <- "red"
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
progress <- i
|
|
||||||
time.Sleep(time.Millisecond * 200)
|
|
||||||
}
|
|
||||||
status <- "done"
|
|
||||||
color <- "green"
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The result is the following :
|
|
||||||
|
|
||||||
![animation result](https://cloud.xdrm.io/s/go_clifmt_anim_result/download)
|
|
||||||
|
|
||||||
|
|
||||||
|
### [Markdown-like format]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### Bold format
|
||||||
|
|
||||||
|
```go
|
||||||
|
**<target text>**
|
||||||
|
```
|
||||||
|
|
||||||
|
- `<target text>` is the text that will be bold.
|
||||||
|
|
||||||
|
###### Example
|
||||||
|
|
||||||
|
```go
|
||||||
|
clifmt.Printf("normal text **bold text** normal text")
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### Italic format
|
||||||
|
|
||||||
|
```go
|
||||||
|
*<target text>*
|
||||||
|
```
|
||||||
|
|
||||||
|
- `<target text>` is the text that will be italic.
|
||||||
|
|
||||||
|
###### Example
|
||||||
|
|
||||||
|
```go
|
||||||
|
clifmt.Printf("normal text *italic text* normal text")
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### Underline format
|
||||||
|
|
||||||
|
```go
|
||||||
|
_<target text>_
|
||||||
|
```
|
||||||
|
|
||||||
|
- `<target text>` is the text that will be underlined.
|
||||||
|
|
||||||
|
###### Example
|
||||||
|
|
||||||
|
```go
|
||||||
|
clifmt.Printf("normal text _underline text_ normal text")
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### Hyperlink format
|
||||||
|
|
||||||
|
```go
|
||||||
|
[<target text>](<target url>)
|
||||||
|
```
|
||||||
|
|
||||||
|
- `<target text>` is the text that will be displayed.
|
||||||
|
- `<target url>` is the url the hyperlink links to.
|
||||||
|
|
||||||
|
###### Example
|
||||||
|
|
||||||
|
```go
|
||||||
|
clifmt.Printf("normal text [hyper](link) normal text")
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
----
|
----
|
||||||
|
|
||||||
## IV. Screenshots
|
## (2) Screenshot
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -202,10 +195,9 @@ The result is the following :
|
||||||
|
|
||||||
----
|
----
|
||||||
|
|
||||||
## V. Incoming features
|
## (3) Incoming features
|
||||||
|
|
||||||
- [x] **markdown-like formatting** - bold, italic, underlined, (strike-through?)
|
- [x] **markdown-like formatting** - bold, italic, underlined, (strike-through?)
|
||||||
- [ ] **command-line executable** - align text from terminal
|
|
||||||
- [ ] **global alignment** - align text dynamically
|
- [ ] **global alignment** - align text dynamically
|
||||||
- [x] **progress-line** - redrawing format to show, for instance an animated progress bar on the same line
|
- [ ] **progress-line** - redrawing format to show, for instance an animated progress bar on the same line
|
||||||
|
|
||||||
|
|
|
@ -10,17 +10,14 @@ type export string
|
||||||
var Export = export("hyperlink")
|
var Export = export("hyperlink")
|
||||||
|
|
||||||
func (syn export) Regex() *regexp.Regexp {
|
func (syn export) Regex() *regexp.Regexp {
|
||||||
return regexp.MustCompile(`(?m)(^|[^\x1b])\[([^(?:\]()]+)\]\(([^\)]+)\)`)
|
return regexp.MustCompile(`(?m)\[([^\[]+)\]\(([^\)]+)\)`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (syn export) Transform(args ...string) (string, error) {
|
func (syn export) Transform(args ...string) (string, error) {
|
||||||
// no arg, empty -> ignore
|
// no arg, empty -> ignore
|
||||||
if len(args) < 2 || len(args[1]) < 1 {
|
if len(args) < 2 || len(args[0]) < 1 {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
linkstart := fmt.Sprintf("\x1b]8;;%s\x1b\\", args[2])
|
return fmt.Sprintf("\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\", args[1], args[0]), nil
|
||||||
linkend := fmt.Sprintf("\x1b]8;;\x1b\\")
|
|
||||||
|
|
||||||
return fmt.Sprintf("%s%s%s%s", args[0], linkstart, args[1], linkend), nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package clifmt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -57,12 +58,10 @@ func Printpf(format string, args ...interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// (4) rewind N lines (written previous time)
|
// (4) rewind N lines (written previous time)
|
||||||
if rows > 0 {
|
if rows >= 0 {
|
||||||
fmt.Printf("\x1b[%dF\x1b[K", rows)
|
fmt.Printf("\x1b[%dF\x1b[K", rows)
|
||||||
} else {
|
|
||||||
fmt.Printf("\r\x1b[K")
|
|
||||||
}
|
}
|
||||||
rows = strings.Count(str, "\n")
|
rows = int(math.Max(float64(strings.Count(str, "\n")), 1))
|
||||||
|
|
||||||
// (5) make each line rewrite previous line
|
// (5) make each line rewrite previous line
|
||||||
str = strings.Replace(str, "\n", "\n\x1b[K", 11)
|
str = strings.Replace(str, "\n", "\n\x1b[K", 11)
|
||||||
|
|
Loading…
Reference in New Issue