mjunolainen

ascii-art2

Oct 19th, 2021
628
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "os"
  7.     "strings"
  8. )
  9.  
  10. func main() {
  11.     args := ""
  12.     if len(os.Args) == 3 {
  13.         args = string(os.Args[2])
  14.         args = args + ".txt"
  15.     } else if len(os.Args) == 2 {
  16.         args = "font.txt"
  17.     } else {
  18.         fmt.Println("Program uses os.Args as input. To run the program from terminal do it like this: \n\nExample:  go run . 'string you want to print' font")
  19.     }
  20.     if _, err := os.Stat(args); os.IsNotExist(err) {
  21.         fmt.Println("Program uses os.Args as input. To run the program from terminal do it like that: \n\nExample:  go run . 'string you want to print' font")
  22.         os.Exit(0)
  23.     }
  24.  
  25.     font, _ := readLines(args)
  26.     text := os.Args[1]
  27.     textLines := strings.Split(text, "\\n")
  28.  
  29.     writeText(textLines, font)
  30. }
  31.  
  32. // This function prints the lines
  33. func writeText(textLines []string, font []string) {
  34.     for i := 0; i < len(textLines); i++ {
  35.         x := 0
  36.         for j := 0; j < 8; j++ {
  37.             for k := 0; k < len(textLines[i]); k++ {
  38.                 // Find the right line for the letter
  39.                 pos := 1 + (int(textLines[i][k])-32)*9 + x
  40.                 fmt.Printf(font[pos])
  41.             }
  42.             x++
  43.             fmt.Println()
  44.         }
  45.     }
  46. }
  47.  
  48. // This function reads the font file
  49. func readLines(path string) ([]string, error) {
  50.     file, err := os.Open(path)
  51.     if err != nil {
  52.         return nil, err
  53.     }
  54.     defer file.Close()
  55.  
  56.     var lines []string
  57.     scanner := bufio.NewScanner(file)
  58.     for scanner.Scan() {
  59.         lines = append(lines, scanner.Text())
  60.     }
  61.     return lines, scanner.Err()
  62. }
  63.  
RAW Paste Data