This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ghfetch.ts
120 lines (100 loc) · 4.16 KB
/
ghfetch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import * as Colors from 'https://deno.land/[email protected]/fmt/colors.ts'
import { getImageStrings } from "https://x.nest.land/[email protected]/mod.ts"
import { Command } from "https://deno.land/x/[email protected]/command/mod.ts"
import { Image } from 'https://deno.land/x/[email protected]/mod.ts'
import ky from 'https://cdn.skypack.dev/ky?dts';
const { options } = await new Command()
.version("1.0.0")
.option("-u, --username <string>", "Set the username of the user.")
.option("-b, --base-url <string>", "Set the base github api url.", { default: "https://api.github.com" })
.parse(Deno.args);
let user
// Fetch user info from the internet
if (options.username === undefined) {
// Try to grab from `gh` tool
try {
const p = Deno.run({cmd: ["gh", "api", "user"], stdout: "piped", stderr: "piped"})
const output = await p.output() // "piped" must be set
const outStr = new TextDecoder().decode(output);
p.close()
try {
const parsed = JSON.parse(outStr)
user = {
Login: parsed.login,
Blog: parsed.blog,
Followers: parsed.followers,
Gists: parsed.public_gists,
Location: parsed.location,
Repos: parsed.public_repos,
Twitter: `@${parsed.twitter_username}`,
avatarUrl: parsed.avatar_url
}
} catch(err) {
// They arent logged into GH
console.log(`${Colors.red("gh returned non-json.")}\n${Colors.yellow("TIP: You can just use --username=[USERNAME] to fix this error")}`)
console.log(err)
Deno.exit()
}
} catch(err) {
// gh isn't installed
console.log(`${Colors.red("No username was given, and gh is not installed (or we dont have permission to use it)")}\n${Colors.yellow("If it is installed, you didn't run me with --allow-run ?")}\n${Colors.yellow("TIP: You can just use --username=[USERNAME] to fix this error")}`)
console.log(err)
Deno.exit()
}
} else {
// Use ky if username isn't given
const response = await ky.get(`${options.baseUrl}/users/${options.username}`)
const data = await response.json();
user = {
Login: data.login,
Blog: data.blog,
Followers: data.followers,
Gists: data.public_gists,
Location: data.location,
Repos: data.public_repos,
Twitter: `@${data.twitter_username}`,
avatarUrl: data.avatar_url
}
}
const color = Colors.brightBlue
// Create a title
const title = `${color(user.Login)}@${color(new URL(options.baseUrl).hostname)}`
// Calculate barrier size for title
let barrier = ""
// Start at 1
for (let index = 1; index < `${options.username}@${new URL(options.baseUrl).hostname}`.length; index++) {
barrier += `-`
}
// Download image to memory
const response = await ky.get(user.avatarUrl).blob()
// Edit image to be round
const image = await Image.decode(new Uint8Array(await response.arrayBuffer()))
image.roundCorners(245)
// Generate image ascii
const ascii = await getImageStrings({ rawFile: await image.encode(), width: 35 })
// Split image ascii into a string[]
const splitAscii = ascii[0].split(/\r\n|\n\r|\n|\r/)
// Spacing between the image and the text
const spacing = " "
// Get a array of keys of the object
const userKeys = Object.keys(user)
// Iterate user keys for some filtering
for (let index = 0; index < userKeys.length; index++) {
// Remove empty points
if (user[userKeys[index]] === null || user[userKeys[index]] === "" || user[userKeys[index]] === "@null") {
userKeys.splice(index, 1)
}
}
for (let index = 0; index < splitAscii.length; index++) {
const element = splitAscii[index]
if (index === 0) { console.log(`${element}${spacing}${title}`); continue }
if (index === 1) { console.log(`${element}${spacing}${barrier}`); continue }
if (userKeys.length > index - 2) {
// Dont show avatarUrl
if (userKeys[index - 2] !== "avatarUrl") {
console.log(`${element}${spacing}${color(userKeys[index - 2])}: ${user[userKeys[index - 2]]}`)
continue
}
}
console.log(element)
}