| 
									
										
										
										
											2019-08-17 15:55:25 +03:00
										 |  |  | // For more tutorials: https://blog.learngoprogramming.com | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Copyright © 2018 Inanc Gumus | 
					
						
							|  |  |  | // Learn Go Programming Course | 
					
						
							|  |  |  | // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"text/tabwriter" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TODO: make this configurable? or exercise? | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	minWidth = 0 | 
					
						
							|  |  |  | 	tabWidth = 4 | 
					
						
							|  |  |  | 	padding  = 4 | 
					
						
							|  |  |  | 	flags    = 0 | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-26 14:37:58 +03:00
										 |  |  | type textReport struct{} | 
					
						
							| 
									
										
										
										
											2019-08-17 15:55:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-26 14:37:58 +03:00
										 |  |  | func newTextReport() *textReport { | 
					
						
							|  |  |  | 	return new(textReport) | 
					
						
							| 
									
										
										
										
											2019-08-17 15:55:25 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-26 14:37:58 +03:00
										 |  |  | func (s *textReport) report(results iterator) error { | 
					
						
							| 
									
										
										
										
											2019-08-17 15:55:25 +03:00
										 |  |  | 	w := tabwriter.NewWriter(os.Stdout, minWidth, tabWidth, padding, ' ', flags) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	write := fmt.Fprintf | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	write(w, "DOMAINS\tPAGES\tVISITS\tUNIQUES\n") | 
					
						
							|  |  |  | 	write(w, "-------\t-----\t------\t-------\n") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var total result | 
					
						
							|  |  |  | 	results.each(func(r result) { | 
					
						
							|  |  |  | 		total = total.add(r) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		write(w, "%s\t%s\t%d\t%d\n", r.domain, r.page, r.visits, r.uniques) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	write(w, "\t\t\t\n") | 
					
						
							|  |  |  | 	write(w, "%s\t%s\t%d\t%d\n", "TOTAL", "", total.visits, total.uniques) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return w.Flush() | 
					
						
							|  |  |  | } |