Skip to content

Commit 8f072d3

Browse files
committed
refactor: combine standard output and error
1 parent c130705 commit 8f072d3

6 files changed

Lines changed: 47 additions & 50 deletions

File tree

highlighter_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,4 @@ func TestGetHighlightedContent(t *testing.T) {
3939
if result != expected {
4040
t.Errorf("expected highlighted content %d, received %d", expected, result)
4141
}
42-
4342
}

parser.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ func NewRule(target, dependencies string, commands []string, lineNumber int) *Ru
1919

2020
// ParsedContent contains the content of a Makefile with its parsed rules
2121
type ParsedContent struct {
22-
filePath string
22+
FilePath string
2323
includeSpecialTargets bool
24-
content []string
25-
rules []Rule
24+
Content []string
25+
Rules []Rule
2626
}
2727

2828
// NewParsedContent constructs ParsedContent and stages the Makefile for parsing
2929
func NewParsedContent(filePath string, content []string) *ParsedContent {
30-
return &ParsedContent{filePath: filePath, includeSpecialTargets: false, content: content, rules: []Rule{}}
30+
return &ParsedContent{FilePath: filePath, includeSpecialTargets: false, Content: content, Rules: []Rule{}}
3131
}
3232

3333
// SetIncludeSpecialTargets sets the option to include special targets to the given boolean value
@@ -41,7 +41,7 @@ func (parsedContent *ParsedContent) Parse() {
4141
inTarget := false
4242
multilineCommentRegexp := regexp.MustCompile(`^.*#.*\\$`)
4343
ruleRegexp := regexp.MustCompile(`^([^:\s]+)\s*:\s*([^=].*)?$`)
44-
for lineNumber, line := range parsedContent.content {
44+
for lineNumber, line := range parsedContent.Content {
4545
// Handle multiline comments
4646
if inMultilineComment {
4747
inTarget = false
@@ -59,8 +59,8 @@ func (parsedContent *ParsedContent) Parse() {
5959
// Handle rule commands
6060
if inTarget && (len(line) == 0 || line[0] == '\t') {
6161
// Current line is a command
62-
ruleIndex := len(parsedContent.rules) - 1
63-
parsedContent.rules[ruleIndex].commands = append(parsedContent.rules[ruleIndex].commands, strings.TrimSpace(line))
62+
ruleIndex := len(parsedContent.Rules) - 1
63+
parsedContent.Rules[ruleIndex].commands = append(parsedContent.Rules[ruleIndex].commands, strings.TrimSpace(line))
6464
continue
6565
} else if inTarget {
6666
inTarget = false
@@ -70,7 +70,7 @@ func (parsedContent *ParsedContent) Parse() {
7070
if ruleSubmatch != nil && !(!parsedContent.includeSpecialTargets && isSpecialTarget(ruleSubmatch[1])) {
7171
// Match has been found
7272
newRule := NewRule(ruleSubmatch[1], ruleSubmatch[2], []string{}, lineNumber)
73-
parsedContent.rules = append(parsedContent.rules, *newRule)
73+
parsedContent.Rules = append(parsedContent.Rules, *newRule)
7474
inTarget = true
7575
}
7676
}

parser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func assertRulesEqual(t *testing.T, result, expected []Rule) {
3636
func assertParsed(t *testing.T, fileContent []string, expected []Rule) {
3737
content := NewParsedContent("fileName", fileContent)
3838
content.Parse()
39-
assertRulesEqual(t, content.rules, expected)
39+
assertRulesEqual(t, content.Rules, expected)
4040
}
4141

4242
func TestParserNoDependencies(t *testing.T) {

render.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"bufio"
54
"fmt"
65
"log"
76
"os/exec"
@@ -17,21 +16,21 @@ func Render(content *ParsedContent) {
1716
log.Fatalf("failed to initialize termui: %v", err)
1817
}
1918

20-
content.content = replaceTabs(content.content)
19+
content.Content = replaceTabs(content.Content)
2120
termWidth, termHeight := ui.TerminalDimensions()
2221

23-
target := NewTarget(0, len(content.rules), content.rules)
24-
target.Rows = getTargets(content.rules)
22+
target := NewTarget(0, len(content.Rules), content.Rules)
23+
target.Rows = getTargets(content.Rules)
2524

2625
highlighter := NewHighlighter("vim")
2726

2827
dependencyWidget := widgets.NewParagraph()
2928
dependencyWidget.Title = "Dependencies"
30-
dependencyWidget.Text = getDependency(content.rules, target.Index)
29+
dependencyWidget.Text = getDependency(content.Rules, target.Index)
3130

3231
contentWidget := widgets.NewParagraph()
33-
contentWidget.Title = content.filePath
34-
contentWidget.Text = getContent(content.content, highlighter, content.rules, termHeight, target.Index)
32+
contentWidget.Title = content.FilePath
33+
contentWidget.Text = getContent(content.Content, highlighter, content.Rules, termHeight, target.Index)
3534

3635
grid := ui.NewGrid()
3736
grid.SetRect(0, 0, termWidth, termHeight)
@@ -114,25 +113,14 @@ func Render(content *ParsedContent) {
114113
ui.Clear()
115114
}
116115
target.Index = target.SelectedRow
117-
dependencyWidget.Text = getDependency(content.rules, target.Index)
118-
contentWidget.Text = getContent(content.content, highlighter, content.rules, termHeight, target.Index)
116+
dependencyWidget.Text = getDependency(content.Rules, target.Index)
117+
contentWidget.Text = getContent(content.Content, highlighter, content.Rules, termHeight, target.Index)
119118
ui.Render(grid)
120119
}
121-
122120
ui.Close()
123-
targetName := target.GetName()
124-
if run && targetName != "" {
125-
cmd := exec.Command("make", "-f"+content.filePath, targetName)
126-
stdout, _ := cmd.StdoutPipe()
127-
Check(cmd.Start)
128-
129-
scanner := bufio.NewScanner(stdout)
130-
scanner.Split(bufio.ScanLines)
131-
for scanner.Scan() {
132-
m := scanner.Text()
133-
fmt.Println(m)
134-
}
135-
Check(cmd.Wait)
121+
122+
if run {
123+
runTarget(target.GetName(), content.FilePath)
136124
}
137125
}
138126

@@ -184,3 +172,13 @@ func replaceTabs(content []string) []string {
184172
func isLetter(s string) bool {
185173
return s[0] != '<' && s[len(s)-1] != '>'
186174
}
175+
176+
func runTarget(target, filePath string) {
177+
if target != "" {
178+
// Compose command
179+
cmd := exec.Command("make", "-f"+filePath, target)
180+
// CombinedOutput will return both standard output and standard error
181+
stdoutStderr, _ := cmd.CombinedOutput()
182+
fmt.Print(string(stdoutStderr))
183+
}
184+
}

search_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@ import (
55
)
66

77
func TestSearchGetContentLongContent(t *testing.T) {
8-
var maximum int = 3
9-
var content string = "abcdef"
8+
maximum := 3
9+
content := "abcdef"
1010

1111
search := NewSearch()
1212
search.SetActive(true)
1313
search.AppendStringToContent(content)
1414

15-
var expected string = "def"
16-
var result string = search.GetContent(maximum)
15+
expected := "def"
16+
result := search.GetContent(maximum)
1717
if result != expected {
1818
t.Errorf("got %s, expected %s", result, expected)
1919
}
2020
}
2121

2222
func TestSearchGetContentShortContent(t *testing.T) {
23-
var maximum int = 3
24-
var content string = "abc"
23+
maximum := 3
24+
content := "abc"
2525

2626
search := NewSearch()
2727
search.SetActive(true)
2828
search.AppendStringToContent(content)
2929

30-
var expected string = "abc"
31-
var result string = search.GetContent(maximum)
30+
expected := "abc"
31+
result := search.GetContent(maximum)
3232
if result != expected {
3333
t.Errorf("got %s, expected %s", result, expected)
3434
}
3535
}
3636

3737
func TestSearchActive(t *testing.T) {
38-
var content string = "foo"
38+
content := "foo"
3939

4040
search := NewSearch()
4141
search.SetActive(false)
@@ -47,13 +47,13 @@ func TestSearchActive(t *testing.T) {
4747
}
4848

4949
func TestSearchPop(t *testing.T) {
50-
var content string = "bar"
50+
content := "bar"
5151

5252
search := NewSearch()
5353
search.SetActive(true)
5454
search.AppendStringToContent(content)
5555

56-
var expected string = "ba"
56+
expected := "ba"
5757
search.Pop()
5858
if search.content != expected {
5959
t.Errorf("got %s, expected %s", search.content, expected)

target_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ func getRules(length int) []Rule {
1616
}
1717

1818
func TestTargetFindRealTarget(t *testing.T) {
19-
var numberOfRules int = 3
19+
numberOfRules := 3
2020
target := NewTarget(0, numberOfRules, getRules(numberOfRules))
2121

22-
var expected int = 2
23-
var result int = target.FindTarget("target2")
22+
expected := 2
23+
result := target.FindTarget("target2")
2424
if result != expected {
2525
t.Errorf("got index %d, expected index %d", result, expected)
2626
}
2727
}
2828

2929
func TestTargetFindFakeTarget(t *testing.T) {
30-
var numberOfRules int = 3
30+
numberOfRules := 3
3131
target := NewTarget(0, numberOfRules, getRules(numberOfRules))
3232

33-
var expected int = -1
34-
var result int = target.FindTarget("fake")
33+
expected := -1
34+
result := target.FindTarget("fake")
3535
if result != expected {
3636
t.Errorf("got index %d, expected index %d", result, expected)
3737
}

0 commit comments

Comments
 (0)