built converter
Will probably need to spend more time in the future rebuilding regex to check for only scientific notation.
This commit is contained in:
		
							
								
								
									
										145
									
								
								main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										145
									
								
								main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,145 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bufio"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"regexp"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/pterm/pterm"
 | 
				
			||||||
 | 
						"github.com/schollz/progressbar/v3"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func main() {
 | 
				
			||||||
 | 
						// Set Welcome Header
 | 
				
			||||||
 | 
						// pterm.DefaultHeader.WithBackgroundStyle(pterm.NewStyle(pterm.BgCyan)).WithTextStyle(pterm.NewStyle(pterm.FgBlack)).Println("Welcome to Checkdigit Calculator")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pterm.DefaultSection.Printfln(pterm.Cyan("Welcome to Scientific Notation Converter"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Check what directory the commandline is on when the program is launched.
 | 
				
			||||||
 | 
						directory, err := os.Getwd()
 | 
				
			||||||
 | 
						Error(err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Get name of file to be processed.
 | 
				
			||||||
 | 
						args := os.Args[1:]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var input string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if len(args) > 0 {
 | 
				
			||||||
 | 
							input = args[0]
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							pterm.Printf(pterm.LightGreen("What is the name of the file in this directory?: "))
 | 
				
			||||||
 | 
							reader := bufio.NewReader(os.Stdin)
 | 
				
			||||||
 | 
							input, err = reader.ReadString('\n')
 | 
				
			||||||
 | 
							Error(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Create upcs array and push all upc strings into it
 | 
				
			||||||
 | 
						input = strings.TrimSpace(input)
 | 
				
			||||||
 | 
						file, err := os.Open(input)
 | 
				
			||||||
 | 
						Error(err)
 | 
				
			||||||
 | 
						var upcs []string
 | 
				
			||||||
 | 
						scanner := bufio.NewScanner(file)
 | 
				
			||||||
 | 
						for scanner.Scan() {
 | 
				
			||||||
 | 
							upcs = append(upcs, scanner.Text())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						err = file.Close()
 | 
				
			||||||
 | 
						Error(err)
 | 
				
			||||||
 | 
						if scanner.Err() != nil {
 | 
				
			||||||
 | 
							fmt.Println(scanner.Err())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Create gtins array that will eventually be returned
 | 
				
			||||||
 | 
						var gtins []string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Using regex compile so the program stays fast
 | 
				
			||||||
 | 
						regex, err := regexp.Compile("^[^0-9]")
 | 
				
			||||||
 | 
						Error(err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						checkBar := progressbar.NewOptions(len(upcs),
 | 
				
			||||||
 | 
							progressbar.OptionEnableColorCodes(true),
 | 
				
			||||||
 | 
							progressbar.OptionSetDescription("[cyan][1/2][reset] Converting Scientific Notation Number..."),
 | 
				
			||||||
 | 
							progressbar.OptionUseANSICodes(true),
 | 
				
			||||||
 | 
							progressbar.OptionSetTheme(progressbar.Theme{
 | 
				
			||||||
 | 
								Saucer:        "[green]=[reset]",
 | 
				
			||||||
 | 
								SaucerHead:    "[green]>[reset]",
 | 
				
			||||||
 | 
								SaucerPadding: " ",
 | 
				
			||||||
 | 
								BarStart:      "[",
 | 
				
			||||||
 | 
								BarEnd:        "]",
 | 
				
			||||||
 | 
							}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, upc := range upcs {
 | 
				
			||||||
 | 
							checkForNonStartingNumber := regex.MatchString(upc)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if checkForNonStartingNumber {
 | 
				
			||||||
 | 
								gtins = append(gtins, upc)
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Split scientific notation at the e+ point
 | 
				
			||||||
 | 
							upcArray := strings.Split(upc, "e+")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							fmt.Println(upcArray)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Create separate variables for the split values
 | 
				
			||||||
 | 
							workingUpc := upcArray[0]
 | 
				
			||||||
 | 
							upcENumber := upcArray[1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// At one to the scientific notation length to allow for the decimal point to be removed
 | 
				
			||||||
 | 
							upcLength, err := strconv.Atoi(upcENumber)
 | 
				
			||||||
 | 
							Error(err)
 | 
				
			||||||
 | 
							upcLength = upcLength + 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Remove the decimal point
 | 
				
			||||||
 | 
							workingUpc = strings.ReplaceAll(workingUpc, ".", "")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Append 0 to UPC to match notation length
 | 
				
			||||||
 | 
							for len(workingUpc) < upcLength {
 | 
				
			||||||
 | 
								workingUpc = workingUpc + "0"
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							upc = workingUpc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							gtins = append(gtins, upc)
 | 
				
			||||||
 | 
							checkBar.Add(1)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						answerBar := progressbar.NewOptions(len(gtins),
 | 
				
			||||||
 | 
							progressbar.OptionEnableColorCodes(true),
 | 
				
			||||||
 | 
							progressbar.OptionSetDescription("[cyan][2/2][reset] Writing answers into file..."),
 | 
				
			||||||
 | 
							progressbar.OptionUseANSICodes(true),
 | 
				
			||||||
 | 
							progressbar.OptionClearOnFinish(),
 | 
				
			||||||
 | 
							progressbar.OptionSetTheme(progressbar.Theme{
 | 
				
			||||||
 | 
								Saucer:        "[green]=[reset]",
 | 
				
			||||||
 | 
								SaucerHead:    "[green]>[reset]",
 | 
				
			||||||
 | 
								SaucerPadding: " ",
 | 
				
			||||||
 | 
								BarStart:      "[",
 | 
				
			||||||
 | 
								BarEnd:        "]",
 | 
				
			||||||
 | 
							}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Check if answer.txt already exists and if so, delete it
 | 
				
			||||||
 | 
						if _, err := os.Stat(directory + "/answer.txt"); err == nil {
 | 
				
			||||||
 | 
							e := os.Remove(directory + "/answer.txt")
 | 
				
			||||||
 | 
							Error(e)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Create new answer.txt
 | 
				
			||||||
 | 
						answerFile, err := os.Create(directory + "/answer.txt")
 | 
				
			||||||
 | 
						Error(err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						defer answerFile.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Fill the answers.txt file
 | 
				
			||||||
 | 
						for _, gtin := range gtins {
 | 
				
			||||||
 | 
							_, err := answerFile.WriteString(gtin + "\n")
 | 
				
			||||||
 | 
							Error(err)
 | 
				
			||||||
 | 
							answerBar.Add(1)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Print Success message and exit
 | 
				
			||||||
 | 
						pterm.Printf("\n")
 | 
				
			||||||
 | 
						pterm.Success.Println("The answer.txt file has been successfully generated with check digits.")
 | 
				
			||||||
 | 
						os.Exit(0)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user