Add notice about snipping process, if it happened

This commit is contained in:
maride 2019-12-04 00:10:42 +01:00
parent e9ec8ad46c
commit 7dfc4cb514
3 changed files with 87 additions and 41 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"git.darknebu.la/maride/pancap/output"
"log"
"math/rand"
"time"
@ -14,6 +15,7 @@ func main() {
// register flags
registerFileFlags()
output.RegisterFlags()
flag.Parse()
// Open the given PCAP
@ -29,6 +31,9 @@ func main() {
// Mh, encountered some problems while analyzing file
log.Fatalf("Error occurred while analyzing: %s", analyzeErr.Error())
}
// Finalize output
output.Finalize()
}
// Prints a simple figlet-style ASCII art and a random quote

View File

@ -1,52 +1,24 @@
package output
import (
"fmt"
"flag"
"github.com/fatih/color"
)
const (
MaxContentLines = 50
var (
fullOutput *bool
)
// Prints a block of information with the given headline
// If content is empty, printing the headline is omitted.
// If the content is longer than MaxContentLines, content is cut.
func PrintBlock(headline string, content string) {
// Print a newline to add some space between blocks
fmt.Println("")
// Check if we need to print a headline
if len(content) > 0 {
// We have content, we can print the headline
headlineColor := color.New(color.FgRed, color.Bold)
headlineColor.Println(headline)
}
// Cut to MaxContentLines if required
cutCont := cutContent(content)
fmt.Print(cutCont)
func RegisterFlags() {
fullOutput = flag.Bool("full-output", false, "Show full output instead of limiting submodule output")
}
// Cut content after MaxContentLines lines
func cutContent(content string) string {
numNewlines := 0
// iterate over every character
for i, c := range content {
// check if character is newline
if c == '\n' {
// it is, count occurrence
numNewlines++
// Check if we already hit our limit yet
if numNewlines == MaxContentLines {
// Found nth newline, return content up to this position.
return content[:i+1] // +1 to include the last newline as well
}
}
// Called at the very end, before terminating pancap
func Finalize() {
// Check if we snipped, to add a notice how to show the whole block
if DidSnip {
// We snipped - inform user about this process
printer := color.New(color.Bold, color.BgBlack)
printer.Print("\nOutput is snipped at one or more positions. Add --full-output to avoid snipping.")
}
// We are done before reaching the cut limit; return the whole content
return content
}
}

69
output/printer.go Normal file
View File

@ -0,0 +1,69 @@
package output
import (
"fmt"
"github.com/fatih/color"
)
const (
MaxContentLines = 50
SnipMark = "----- cut at 50 entries -----\n"
)
var (
DidSnip bool
)
// Prints a block of information with the given headline
// If content is empty, printing the headline is omitted.
// If the content is longer than MaxContentLines, content is cut.
func PrintBlock(headline string, content string) {
// Print a newline to add some space between blocks
fmt.Println("")
// Check if we need to print a headline
if len(content) > 0 {
// We have content, we can print the headline
headlineColor := color.New(color.FgRed, color.Bold)
headlineColor.Println(headline)
}
// Cut to MaxContentLines if required
if !(*fullOutput) {
// User states that they don't want to see the whole output - cut content.
content = cutContent(content)
}
// And print our content.
fmt.Print(content)
}
// Cut content after MaxContentLines lines
func cutContent(content string) string {
numNewlines := 0
// iterate over every character
for i, c := range content {
// check if character is newline
if c == '\n' {
// it is, count occurrence
numNewlines++
// Check if we already hit our limit yet
if numNewlines == MaxContentLines {
// Found nth newline, return content up to this position and add a notice about it.
DidSnip = true
return addSnipMark(content[:i])
}
}
}
// We are done before reaching the cut limit; return the whole content
return content
}
// Adds a notice about the snipping process
func addSnipMark(content string) string {
printer := color.New(color.Bold)
return content + "\n" + printer.Sprint(SnipMark)
}