From ea4dfef5e98ece089e7fe113ff0fa3350b330c4e Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 10 Nov 2018 13:20:10 +0100 Subject: [PATCH] update cli ui --- internal/buildfile/reader.go | 6 ++++-- internal/clifmt/symbols.go | 28 ++++++++++++++++++++++++++++ internal/instruction/cnf.go | 4 ++++ internal/instruction/common.go | 2 ++ internal/instruction/del.go | 4 ++++ internal/instruction/ins.go | 4 ++++ internal/instruction/ser.go | 4 ++++ 7 files changed, 50 insertions(+), 2 deletions(-) diff --git a/internal/buildfile/reader.go b/internal/buildfile/reader.go index eab7840..a5ec95b 100644 --- a/internal/buildfile/reader.go +++ b/internal/buildfile/reader.go @@ -83,12 +83,14 @@ func (r *Reader) Execute() error { // 3. exec each instruction for i, inst := range r.Content { + clifmt.AlignLimited(fmt.Sprintf("(%d) %s", i, clifmt.Color(33, inst.Raw()))) + _, err := inst.Exec(*r.Context) if err != nil { - fmt.Printf("%d | %s\n", i, err) + fmt.Printf("%s\n", clifmt.Color(31, err.Error())) continue } - fmt.Printf("%d | %s\n", i, clifmt.Color(32, "done")) + fmt.Printf("%s\n", clifmt.Color(32, "done")) } return nil diff --git a/internal/clifmt/symbols.go b/internal/clifmt/symbols.go index d9e1619..7e8c8fe 100644 --- a/internal/clifmt/symbols.go +++ b/internal/clifmt/symbols.go @@ -55,3 +55,31 @@ func Align(s string) { fmt.Printf(" ") } } + +// AlignLimited prints strings with space padding to align line ends (fixed width) +// also crops the content to add '...' at the end if too long (must have a space +// at the end) +func AlignLimited(s string) { + + // format string + if len(s) > alignOffset-1 { + s = fmt.Sprintf("%s... ", s[:alignOffset-4]) + } + + // 1. print string + fmt.Printf("%s", s) + + // 2. get actual size + size := len(s) + + // 3. remove \033[XYm format characters + size -= (len(strings.Split(s, "\033")) - 0) * 6 + + // 3. add 1 char for each \033[0m + size += len(strings.Split(s, "\033[0m")) - 1 + + // 4. print trailing spaces + for i := size; i < alignOffset; i++ { + fmt.Printf(" ") + } +} diff --git a/internal/instruction/cnf.go b/internal/instruction/cnf.go index 8ed9665..cdcc236 100644 --- a/internal/instruction/cnf.go +++ b/internal/instruction/cnf.go @@ -7,6 +7,7 @@ import ( ) type config struct { + raw string // File is the path to the file File string // Path is the configuration field path @@ -17,6 +18,8 @@ type config struct { Format *cnf.ConfigurationFormat } +func (d *config) Raw() string { return d.raw } + func (d *config) Build(_args string) error { // 1. extract action (sub command) @@ -42,6 +45,7 @@ func (d *config) Build(_args string) error { // add value d.Value = value + d.raw = _args return nil } diff --git a/internal/instruction/common.go b/internal/instruction/common.go index ed09ab6..486c47c 100644 --- a/internal/instruction/common.go +++ b/internal/instruction/common.go @@ -8,6 +8,8 @@ import ( // T is the instruction common interface type T interface { + // Raw input line + Raw() string // Build the instruction with input arguments Build(string) error // Exec the given instruction diff --git a/internal/instruction/del.go b/internal/instruction/del.go index ad7f1d7..646d578 100644 --- a/internal/instruction/del.go +++ b/internal/instruction/del.go @@ -6,11 +6,15 @@ import ( ) type delete struct { + raw string Packages []string } +func (d *delete) Raw() string { return d.raw } + func (d *delete) Build(_args string) error { d.Packages = strings.Split(_args, " ") + d.raw = _args return nil } diff --git a/internal/instruction/ins.go b/internal/instruction/ins.go index 87919bb..368a71e 100644 --- a/internal/instruction/ins.go +++ b/internal/instruction/ins.go @@ -6,11 +6,15 @@ import ( ) type install struct { + raw string Packages []string } +func (d *install) Raw() string { return d.raw } + func (d *install) Build(_args string) error { d.Packages = strings.Split(_args, " ") + d.raw = _args return nil } diff --git a/internal/instruction/ser.go b/internal/instruction/ser.go index 6a1bb67..791a93c 100644 --- a/internal/instruction/ser.go +++ b/internal/instruction/ser.go @@ -8,10 +8,13 @@ import ( var actions = []string{"enable", "disable", "start", "stop", "reload", "restart"} type service struct { + raw string Action string Services []string } +func (d *service) Raw() string { return d.raw } + func (d *service) Build(_args string) error { // 1. extract action (sub command) @@ -38,6 +41,7 @@ func (d *service) Build(_args string) error { // 4. Store services d.Services = split[1:] + d.raw = _args return nil }