Skip to content

Instantly share code, notes, and snippets.

@maestrith
Created July 19, 2013 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maestrith/6041876 to your computer and use it in GitHub Desktop.
Save maestrith/6041876 to your computer and use it in GitHub Desktop.
Posted using AHK Studio
#SingleInstance,Force
IniRead,access_token,settings.ini,access,token,0
if !access_token{
InputBox,access_token,Please enter your access token from Github,Without an access token this will not work.
if (ErrorLevel||access_token="")
ExitApp
IniWrite,%access_token%,settings.ini,access,token
}
Gui,+hwndmain
Gui,Add,Edit,w600 h500 WantTab
Gui,Add,ListView,w200 h500 section x+10 gg vget AltSubmit,Filename|Gist#
Gui,Add,Button,xs ys+510 gg vrefresh,Get Gist List
Gui,Add,Button,xm y510 gg vpost,Update Gist
Gui,Add,Button,x+10 gg vnew,New Gist
Gui,Show
g:=new gist(access_token)
global g,access_token,currentfile,main,currentid
return
g:
do:=A_GuiControl,g[do]()
return
GuiClose:
GuiEscape:
ExitApp
return
class gist{
__New(){
this.http:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
}
net(verb,url,code=""){
this.http.open(verb,url)
this.http.SetRequestHeader("Authorization","Bearer " access_token)
this.http.send(code)
return this.http.ResponseText
}
refresh(){
codes:=this.net("Get","https://api.github.com/gists"),LV_Delete()
pos=1
Gui,1:Default
while,pos:=RegExMatch(codes,"U)" Chr(34) "id" Chr(34) ":" Chr(34) "(.*)" chr(34),found,pos){
RegExMatch(codes,"U)" Chr(34) "filename" Chr(34) ":" Chr(34) "(.*)" chr(34),fn,pos)
LV_Add("",fn1,found1)
pos+=3
}
LV_Modify(1,"Select Focus Vis")
Loop,2
LV_ModifyCol(A_Index,"AutoHDR")
}
get(){
if A_GuiEvent!=Normal
return
LV_GetText(number,LV_GetNext(),2)
if !number
return
SplashTextOn,200,50,Downloading text,Please wait
codes:=this.net("Get","https://api.github.com/gists/" number)
for a,b in {url:"raw_url",currentfile:"filename",currentid:"id"}{
RegExMatch(codes,"U)"Chr(34) b Chr(34) ":" Chr(34) "(.*)" chr(34),found)
%a%:=found1
}
file:=this.net("GET",url)
StringReplace,file,file,`n,`r`n,All
ControlSetText,Edit1,%file%
SplashTextOff
}
post(){
url:=currentid?"https://api.github.com/gists/" currentid:"https://api.github.com/gists"
verb:=currentid?"PATCH":"POST"
ControlGetText,info,Edit1,ahk_id%main%
if !info
return m("The Gist must have text.")
if !currentfile
return m("Please either get a gist from the internet or click 'New Gist' to create a new gist")
info:=RegExReplace(info,"([" Chr(34) "\\])","\$1")
for a,b in {"`n":"\n","`t":"\t","`r":""}
StringReplace,info,info,%a%,%b%,All
desc=testing
json=
(
{"description":"%desc%","public":true,"files":{"%currentfile%":{"content":"%info%"}}}
)
code:=this.net(verb,url,json)
if !currentid
this.refresh()
if InStr(this.http.GetResponseHeader("status"),200)
m("Sent")
else
m("Something went wrong. Server message:",code)
}
new(){
InputBox,newfile,Name your new file,Please enter a name for your new file
if (ErrorLevel||newfile="")
return
currentfile:=RegExReplace(RegExReplace(RegExReplace(newfile,"&")," ","_"),"[^a-zA-Z0-9_]")
currentid:=""
ControlSetText,Edit1,,ahk_id%main%
ControlFocus,Edit1,ahk_id%main%
}
}
m(x*){
for a,b in x
list.=b "`n"
MsgBox,% list
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment