From 7dfc4cb514fd8af27459eb6a6599ac365c49ddc0 Mon Sep 17 00:00:00 2001 From: maride Date: Wed, 4 Dec 2019 00:10:42 +0100 Subject: [PATCH] Add notice about snipping process, if it happened --- main.go | 5 ++++ output/output.go | 54 +++++++++---------------------------- output/printer.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 41 deletions(-) create mode 100644 output/printer.go diff --git a/main.go b/main.go index 4233f98..6f789c2 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/output/output.go b/output/output.go index 2f6757d..e6d01ad 100644 --- a/output/output.go +++ b/output/output.go @@ -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 -} \ No newline at end of file +} diff --git a/output/printer.go b/output/printer.go new file mode 100644 index 0000000..9f24ba4 --- /dev/null +++ b/output/printer.go @@ -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) +}