curl.go (2466B)
1 package handlers 2 3 import ( 4 "fmt" 5 "net/http" 6 "strings" 7 "sync" 8 "time" 9 10 "git.prime8.dev/moncheeta/prime8dev/internal/config" 11 "git.prime8.dev/moncheeta/prime8dev/internal/utils" 12 "github.com/charmbracelet/x/ansi" 13 ) 14 15 const ( 16 curlLogoWidth = 32 17 ansiReset = "\x1b[0m" 18 ansiDim = "\x1b[38;5;242m" 19 ansiAccent = "\x1b[38;5;179m" 20 ansiGreen = "\x1b[38;5;71m" 21 ansiBlue = "\x1b[38;5;110m" 22 ) 23 24 var curlLogo = sync.OnceValue(func() string { return utils.LogoASCII(curlLogoWidth, true) }) 25 26 // isTerminalClient reports whether the request comes from a CLI http client. 27 func isTerminalClient(r *http.Request) bool { 28 ua := strings.ToLower(r.UserAgent()) 29 for _, p := range []string{"curl/", "wget/", "httpie/", "xh/"} { 30 if strings.HasPrefix(ua, p) { 31 return true 32 } 33 } 34 return false 35 } 36 37 func (h *Handler) serveTerminalFetch(w http.ResponseWriter) { 38 w.Header().Set("Content-Type", "text/plain; charset=utf-8") 39 w.Header().Set("Vary", "User-Agent") 40 fmt.Fprint(w, terminalFetch(config.GetAbout(), config.CurrentJob(config.GetExperience()))) 41 } 42 43 func terminalFetch(about config.About, job *config.Experience) string { 44 role := about.Profession 45 if job != nil { 46 role = job.Role + " @ " + job.Company 47 } 48 title := about.Handle + "@prime8" 49 50 row := func(key, value string) string { return ansiAccent + key + ansiReset + ": " + value } 51 lines := []string{ 52 ansiGreen + about.Handle + ansiReset + ansiDim + "@" + ansiReset + ansiAccent + "prime8" + ansiReset, 53 ansiDim + strings.Repeat("-", len(title)) + ansiReset, 54 row("name", about.Name), 55 row("role", role), 56 row("location", about.Location), 57 row("uptime", utils.Uptime(config.Birthday, time.Now())), 58 } 59 lines = append(lines, 60 row("email", ansiBlue+"damian@prime8.dev"+ansiReset), 61 row("web", ansiBlue+"https://prime8.dev"+ansiReset), 62 "", 63 ansiDim+"the full thing: "+ansiReset+ansiGreen+"ssh prime8.dev"+ansiReset, 64 ) 65 66 logo := strings.Split(curlLogo(), "\n") 67 if len(logo) == 1 && logo[0] == "" { 68 return "\n" + strings.Join(lines, "\n") + "\n\n" 69 } 70 71 var b strings.Builder 72 b.WriteString("\n") 73 for i := 0; i < max(len(logo), len(lines)); i++ { 74 l := "" 75 if i < len(logo) { 76 l = logo[i] 77 } 78 // pad by visible width: the logo lines carry truecolor escapes 79 b.WriteString(l + ansiReset + strings.Repeat(" ", max(0, curlLogoWidth-ansi.StringWidth(l))) + " ") 80 if i < len(lines) { 81 b.WriteString(lines[i]) 82 } 83 b.WriteString("\n") 84 } 85 b.WriteString("\n") 86 return b.String() 87 }