mirror of
https://github.com/wahyd4/forego.git
synced 2026-08-09 05:07:05 +10:00
Refactor colored writing into WriteLine() function
This serves two purposes. It protects with a Mutex where it was missing before, and makes it so that there is only one place to change code for the line writing, which will be a later pull request.
This commit is contained in:
@@ -40,20 +40,9 @@ func NewOutletFactory() (of *OutletFactory) {
|
||||
}
|
||||
|
||||
func (o *Outlet) Write(b []byte) (num int, err error) {
|
||||
mx.Lock()
|
||||
defer mx.Unlock()
|
||||
scanner := bufio.NewScanner(bytes.NewReader(b))
|
||||
for scanner.Scan() {
|
||||
formatter := fmt.Sprintf("%%-%ds | ", o.Factory.Padding)
|
||||
ct.ChangeColor(o.Color, true, ct.None, false)
|
||||
fmt.Printf(formatter, o.Name)
|
||||
if o.IsError {
|
||||
ct.ChangeColor(ct.Red, true, ct.None, true)
|
||||
} else {
|
||||
ct.ResetColor()
|
||||
}
|
||||
fmt.Println(scanner.Text())
|
||||
ct.ResetColor()
|
||||
o.Factory.WriteLine(o.Name, scanner.Text(), ct.White, ct.None, o.IsError)
|
||||
}
|
||||
num = len(b)
|
||||
return
|
||||
@@ -69,15 +58,27 @@ func (of *OutletFactory) CreateOutlet(name string, index int, isError bool) *Out
|
||||
}
|
||||
|
||||
func (of *OutletFactory) SystemOutput(str string) {
|
||||
ct.ChangeColor(ct.White, true, ct.None, false)
|
||||
formatter := fmt.Sprintf("%%-%ds | ", of.Padding)
|
||||
fmt.Printf(formatter, "forego")
|
||||
ct.ResetColor()
|
||||
fmt.Println(str)
|
||||
ct.ResetColor()
|
||||
of.WriteLine("forego", str, ct.White, ct.None, false)
|
||||
}
|
||||
|
||||
func (of *OutletFactory) ErrorOutput(str string) {
|
||||
fmt.Printf("ERROR: %s\n", str)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Write out a single coloured line
|
||||
func (of *OutletFactory) WriteLine(left, right string, leftC, rightC ct.Color, isError bool) {
|
||||
mx.Lock()
|
||||
defer mx.Unlock()
|
||||
|
||||
ct.ChangeColor(leftC, true, ct.None, false)
|
||||
formatter := fmt.Sprintf("%%-%ds | ", of.Padding)
|
||||
fmt.Printf(formatter, left)
|
||||
|
||||
if isError {
|
||||
ct.ChangeColor(ct.Red, true, ct.None, true)
|
||||
} else {
|
||||
ct.ResetColor()
|
||||
}
|
||||
fmt.Println(right)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user