Skip to content

Commit

Permalink
refactor: combine standard output and error
Browse files Browse the repository at this point in the history
  • Loading branch information
claby2 committed Feb 7, 2021
1 parent c130705 commit 8f072d3
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 50 deletions.
1 change: 0 additions & 1 deletion highlighter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ func TestGetHighlightedContent(t *testing.T) {
if result != expected {
t.Errorf("expected highlighted content %d, received %d", expected, result)
}

}
16 changes: 8 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ func NewRule(target, dependencies string, commands []string, lineNumber int) *Ru

// ParsedContent contains the content of a Makefile with its parsed rules
type ParsedContent struct {
filePath string
FilePath string
includeSpecialTargets bool
content []string
rules []Rule
Content []string
Rules []Rule
}

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

// SetIncludeSpecialTargets sets the option to include special targets to the given boolean value
Expand All @@ -41,7 +41,7 @@ func (parsedContent *ParsedContent) Parse() {
inTarget := false
multilineCommentRegexp := regexp.MustCompile(`^.*#.*\\$`)
ruleRegexp := regexp.MustCompile(`^([^:\s]+)\s*:\s*([^=].*)?$`)
for lineNumber, line := range parsedContent.content {
for lineNumber, line := range parsedContent.Content {
// Handle multiline comments
if inMultilineComment {
inTarget = false
Expand All @@ -59,8 +59,8 @@ func (parsedContent *ParsedContent) Parse() {
// Handle rule commands
if inTarget && (len(line) == 0 || line[0] == '\t') {
// Current line is a command
ruleIndex := len(parsedContent.rules) - 1
parsedContent.rules[ruleIndex].commands = append(parsedContent.rules[ruleIndex].commands, strings.TrimSpace(line))
ruleIndex := len(parsedContent.Rules) - 1
parsedContent.Rules[ruleIndex].commands = append(parsedContent.Rules[ruleIndex].commands, strings.TrimSpace(line))
continue
} else if inTarget {
inTarget = false
Expand All @@ -70,7 +70,7 @@ func (parsedContent *ParsedContent) Parse() {
if ruleSubmatch != nil && !(!parsedContent.includeSpecialTargets && isSpecialTarget(ruleSubmatch[1])) {
// Match has been found
newRule := NewRule(ruleSubmatch[1], ruleSubmatch[2], []string{}, lineNumber)
parsedContent.rules = append(parsedContent.rules, *newRule)
parsedContent.Rules = append(parsedContent.Rules, *newRule)
inTarget = true
}
}
Expand Down
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func assertRulesEqual(t *testing.T, result, expected []Rule) {
func assertParsed(t *testing.T, fileContent []string, expected []Rule) {
content := NewParsedContent("fileName", fileContent)
content.Parse()
assertRulesEqual(t, content.rules, expected)
assertRulesEqual(t, content.Rules, expected)
}

func TestParserNoDependencies(t *testing.T) {
Expand Down
44 changes: 21 additions & 23 deletions render.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bufio"
"fmt"
"log"
"os/exec"
Expand All @@ -17,21 +16,21 @@ func Render(content *ParsedContent) {
log.Fatalf("failed to initialize termui: %v", err)
}

content.content = replaceTabs(content.content)
content.Content = replaceTabs(content.Content)
termWidth, termHeight := ui.TerminalDimensions()

target := NewTarget(0, len(content.rules), content.rules)
target.Rows = getTargets(content.rules)
target := NewTarget(0, len(content.Rules), content.Rules)
target.Rows = getTargets(content.Rules)

highlighter := NewHighlighter("vim")

dependencyWidget := widgets.NewParagraph()
dependencyWidget.Title = "Dependencies"
dependencyWidget.Text = getDependency(content.rules, target.Index)
dependencyWidget.Text = getDependency(content.Rules, target.Index)

contentWidget := widgets.NewParagraph()
contentWidget.Title = content.filePath
contentWidget.Text = getContent(content.content, highlighter, content.rules, termHeight, target.Index)
contentWidget.Title = content.FilePath
contentWidget.Text = getContent(content.Content, highlighter, content.Rules, termHeight, target.Index)

grid := ui.NewGrid()
grid.SetRect(0, 0, termWidth, termHeight)
Expand Down Expand Up @@ -114,25 +113,14 @@ func Render(content *ParsedContent) {
ui.Clear()
}
target.Index = target.SelectedRow
dependencyWidget.Text = getDependency(content.rules, target.Index)
contentWidget.Text = getContent(content.content, highlighter, content.rules, termHeight, target.Index)
dependencyWidget.Text = getDependency(content.Rules, target.Index)
contentWidget.Text = getContent(content.Content, highlighter, content.Rules, termHeight, target.Index)
ui.Render(grid)
}

ui.Close()
targetName := target.GetName()
if run && targetName != "" {
cmd := exec.Command("make", "-f"+content.filePath, targetName)
stdout, _ := cmd.StdoutPipe()
Check(cmd.Start)

scanner := bufio.NewScanner(stdout)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
m := scanner.Text()
fmt.Println(m)
}
Check(cmd.Wait)

if run {
runTarget(target.GetName(), content.FilePath)
}
}

Expand Down Expand Up @@ -184,3 +172,13 @@ func replaceTabs(content []string) []string {
func isLetter(s string) bool {
return s[0] != '<' && s[len(s)-1] != '>'
}

func runTarget(target, filePath string) {
if target != "" {
// Compose command
cmd := exec.Command("make", "-f"+filePath, target)
// CombinedOutput will return both standard output and standard error
stdoutStderr, _ := cmd.CombinedOutput()
fmt.Print(string(stdoutStderr))
}
}
22 changes: 11 additions & 11 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ import (
)

func TestSearchGetContentLongContent(t *testing.T) {
var maximum int = 3
var content string = "abcdef"
maximum := 3
content := "abcdef"

search := NewSearch()
search.SetActive(true)
search.AppendStringToContent(content)

var expected string = "def"
var result string = search.GetContent(maximum)
expected := "def"
result := search.GetContent(maximum)
if result != expected {
t.Errorf("got %s, expected %s", result, expected)
}
}

func TestSearchGetContentShortContent(t *testing.T) {
var maximum int = 3
var content string = "abc"
maximum := 3
content := "abc"

search := NewSearch()
search.SetActive(true)
search.AppendStringToContent(content)

var expected string = "abc"
var result string = search.GetContent(maximum)
expected := "abc"
result := search.GetContent(maximum)
if result != expected {
t.Errorf("got %s, expected %s", result, expected)
}
}

func TestSearchActive(t *testing.T) {
var content string = "foo"
content := "foo"

search := NewSearch()
search.SetActive(false)
Expand All @@ -47,13 +47,13 @@ func TestSearchActive(t *testing.T) {
}

func TestSearchPop(t *testing.T) {
var content string = "bar"
content := "bar"

search := NewSearch()
search.SetActive(true)
search.AppendStringToContent(content)

var expected string = "ba"
expected := "ba"
search.Pop()
if search.content != expected {
t.Errorf("got %s, expected %s", search.content, expected)
Expand Down
12 changes: 6 additions & 6 deletions target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ func getRules(length int) []Rule {
}

func TestTargetFindRealTarget(t *testing.T) {
var numberOfRules int = 3
numberOfRules := 3
target := NewTarget(0, numberOfRules, getRules(numberOfRules))

var expected int = 2
var result int = target.FindTarget("target2")
expected := 2
result := target.FindTarget("target2")
if result != expected {
t.Errorf("got index %d, expected index %d", result, expected)
}
}

func TestTargetFindFakeTarget(t *testing.T) {
var numberOfRules int = 3
numberOfRules := 3
target := NewTarget(0, numberOfRules, getRules(numberOfRules))

var expected int = -1
var result int = target.FindTarget("fake")
expected := -1
result := target.FindTarget("fake")
if result != expected {
t.Errorf("got index %d, expected index %d", result, expected)
}
Expand Down

0 comments on commit 8f072d3

Please sign in to comment.