update cli ui

This commit is contained in:
xdrm-brackets 2018-11-10 13:20:10 +01:00
parent 1705ff54d8
commit ea4dfef5e9
7 changed files with 50 additions and 2 deletions

View File

@ -83,12 +83,14 @@ func (r *Reader) Execute() error {
// 3. exec each instruction // 3. exec each instruction
for i, inst := range r.Content { for i, inst := range r.Content {
clifmt.AlignLimited(fmt.Sprintf("(%d) %s", i, clifmt.Color(33, inst.Raw())))
_, err := inst.Exec(*r.Context) _, err := inst.Exec(*r.Context)
if err != nil { if err != nil {
fmt.Printf("%d | %s\n", i, err) fmt.Printf("%s\n", clifmt.Color(31, err.Error()))
continue continue
} }
fmt.Printf("%d | %s\n", i, clifmt.Color(32, "done")) fmt.Printf("%s\n", clifmt.Color(32, "done"))
} }
return nil return nil

View File

@ -55,3 +55,31 @@ func Align(s string) {
fmt.Printf(" ") 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(" ")
}
}

View File

@ -7,6 +7,7 @@ import (
) )
type config struct { type config struct {
raw string
// File is the path to the file // File is the path to the file
File string File string
// Path is the configuration field path // Path is the configuration field path
@ -17,6 +18,8 @@ type config struct {
Format *cnf.ConfigurationFormat Format *cnf.ConfigurationFormat
} }
func (d *config) Raw() string { return d.raw }
func (d *config) Build(_args string) error { func (d *config) Build(_args string) error {
// 1. extract action (sub command) // 1. extract action (sub command)
@ -42,6 +45,7 @@ func (d *config) Build(_args string) error {
// add value // add value
d.Value = value d.Value = value
d.raw = _args
return nil return nil
} }

View File

@ -8,6 +8,8 @@ import (
// T is the instruction common interface // T is the instruction common interface
type T interface { type T interface {
// Raw input line
Raw() string
// Build the instruction with input arguments // Build the instruction with input arguments
Build(string) error Build(string) error
// Exec the given instruction // Exec the given instruction

View File

@ -6,11 +6,15 @@ import (
) )
type delete struct { type delete struct {
raw string
Packages []string Packages []string
} }
func (d *delete) Raw() string { return d.raw }
func (d *delete) Build(_args string) error { func (d *delete) Build(_args string) error {
d.Packages = strings.Split(_args, " ") d.Packages = strings.Split(_args, " ")
d.raw = _args
return nil return nil
} }

View File

@ -6,11 +6,15 @@ import (
) )
type install struct { type install struct {
raw string
Packages []string Packages []string
} }
func (d *install) Raw() string { return d.raw }
func (d *install) Build(_args string) error { func (d *install) Build(_args string) error {
d.Packages = strings.Split(_args, " ") d.Packages = strings.Split(_args, " ")
d.raw = _args
return nil return nil
} }

View File

@ -8,10 +8,13 @@ import (
var actions = []string{"enable", "disable", "start", "stop", "reload", "restart"} var actions = []string{"enable", "disable", "start", "stop", "reload", "restart"}
type service struct { type service struct {
raw string
Action string Action string
Services []string Services []string
} }
func (d *service) Raw() string { return d.raw }
func (d *service) Build(_args string) error { func (d *service) Build(_args string) error {
// 1. extract action (sub command) // 1. extract action (sub command)
@ -38,6 +41,7 @@ func (d *service) Build(_args string) error {
// 4. Store services // 4. Store services
d.Services = split[1:] d.Services = split[1:]
d.raw = _args
return nil return nil
} }