-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-functions.red
133 lines (119 loc) · 2.97 KB
/
custom-functions.red
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
121
122
123
124
125
126
127
128
129
130
131
132
133
Red []
cls: does [call/console pick ["cls" "clear"] system/platform = 'Windows exit]
parse-clipboard: has [
"Parse Clipboard (Split from newline)"
][
data: replace/all read-clipboard crlf lf
split data either find data cr [cr][lf]
]
; replace-clipboard: function [
; "Replaces given string in clipboard"
; find-value [char! string!]
; replace-value [char! string!]
; ] [
; write-clipboard replace/all read-clipboard find-value replace-value
; ]
; "Write unixtime to clipboard"
get-unixtime: does [
write-clipboard to-string probe to-integer now
print "Copied."
]
md5: function [
"MD5"
arg
][
result: lowercase trim/with form checksum to-string arg 'MD5 "#{}"
write-clipboard probe result
print "Copied."
]
bcrypt: function [
"Php Bcrypt"
arg
][
result: make string! 100
call/output form reduce [{php -r "echo password_hash('} arg {', PASSWORD_BCRYPT);"}] result
write-clipboard probe result
print "Copied."
]
generate-string: function [
"Generates random string"
string-length [number!] "String length"
][
random/seed now
result: random copy/part random rejoin [
"98765432"
"ASDFGHJKLZXCVBNMQWERTYUP"
"asdfghijkzxcvbnmqwertyup"
"!.*=?+%&-()"
] string-length
write-clipboard probe result
print "Copied."
]
zip: function [
"Combine two blocks"
b1 [block!]
b2 [block!]
/pad "Include none values"
][
temp: copy []
repeat x max length? b1 length? b2 [
if any [
f1: pick b1 x
pad
not tail? at b1 x
] [append temp f1] if any [
f2: pick b2 x
pad
not tail? at b2 x
] [append temp f2]
] temp
]
substitute: function [
"Do variable substitution inside a string"
string [any-string!]
vars [object! block!]
][
out: make string 2 + length? string
emit: func [str] [insert tail out :str]
rule: make block! 256
if block? vars [vars: context vars]
foreach word next first vars [
insert tail rule compose/deep [
(either empty? rule [] ['|])
(form word) ">" (to paren! bind compose [emit (word)] in vars 'self)
]
]
parse/all string [
any [
start:
to "<" end: (insert/part tail out start end)
["<" rule | skip (emit "<")]
]
(insert tail out start)
]
out
]
; https://github.com/red/red/wiki/[DOC]-Guru-Meditations#how-to-make-http-requests
http-post: function [
"Make Http POST request"
url [url!]
parameters [string!]
/headers
http-headers [block!]
/info
] [
unless headers [http-headers: []]
either info [
write/info url reduce ['POST http-headers parameters]
][
write url reduce ['POST http-headers parameters]
]
]
run-console: function [
"Runs shell command and returns output"
command [string!]
][
o: copy ""
call/console/output reduce command o
o
]