Skip to content

Instantly share code, notes, and snippets.

@sentientmachine
Created January 13, 2019 04:21
Show Gist options
  • Save sentientmachine/e09ea2ffa50bfb6444fdd18deda07053 to your computer and use it in GitHub Desktop.
Save sentientmachine/e09ea2ffa50bfb6444fdd18deda07053 to your computer and use it in GitHub Desktop.
function! CommaAQueryChtServer()
"setting command hight sets a tolerance for that many echos before you get a "press enter to continue"
execute "set cmdheight=5"
let word_under_cursor = expand("<cword>")
"risky negative 1 at end there, if selection is nothing, that's off screen could be an error
let visual_mode_selection = getline("'<")[getpos("'<")[2]-1:getpos("'>")[2]-1]
if visual_mode_selection != ''
"if there is a visual selection, then just use the first word of the selection
"zero or more questionmarks at the end should be trimmed
let visual_mode_selection = substitute(visual_mode_selection, '\s*[?\!.]*$', '', '')
"trim leading and trailing whitespace the string
let visual_mode_selection = substitute(visual_mode_selection, '^\s*\(.\{-}\)\s*$', '\1', '')
let visual_mode_selection = substitute(visual_mode_selection, '^\s*//\s*', '', '')
let visual_mode_selection = substitute(visual_mode_selection, '^\s*#\s*', '', '')
"replace newlines with nothing
let visual_mode_selection = substitute(visual_mode_selection, '\n', '', 'g')
let word_under_cursor = visual_mode_selection
endif
let current_filetype = &ft
let cht_translated_current_filetype = Resolve_filetype_to_cht_filetype(current_filetype)
let color_msg = "\e[0;92m\e[1mQuery cht.sh servers, type '" . cht_translated_current_filetype . "' for help on: \e[0m'"
echo
let color_msg .= word_under_cursor . "'? \e[0;92m\e[1m(y/n)\e[0m\n"
echohl WarningMsg | echo "Don't Panic!" | echohl None
let choice = input(color_msg . ">")
if choice == 'y' || choice == "Y"
"IDEOLOGY:
"We only follow through to hit cht.sh when the timestamp on the file in .vim/cht/garcpp/double_query
"has an age over an hour. That when when we hammer ,a it doesn't annoy them.
"Also this enables us later for when cht.sh goes down, our internet goes down, or data gets polluted,
"I've got the historical records.
"cht.sh servers are machine learning from cht.sh over google and stackoverflow and take a few seconds
"they are in a state of evolution and the responses may change, deteriorate, disappear, or improve,
"much like a google search response. We need to be tolerant to transience
"Make a foldier ~/.vim/cht/ and under has an identical to snippets, except they are the saved cht pulls.
"If the cht response is identical to what is saved under ~/.vim/cht/garcpp/whatever then it just
"shows the latest.
"Check the timestamp on the latest cht pull, and if it's from less than an hour, just feed that as if
"it was the real pull. Things don't change every hour bro.
"Each search like cpp/write_to_file is stored and historical is stored with whatever_yyyy_mm_dd_hh_mm_ss
"timestamp. Eventually this directory will be shared across all linux instances. If the response from
"cht is a shrug, it uses whatever is from history.
"This way we are completely tolerant of network loss, and all possible problems. YEAH!
"Google search for terminal!!!!!!!!!!!!!
" I want to compare lists ok
let cht_translated_user_query = substitute(word_under_cursor, ' ', '+', 'g')
"echo "cht_translated_current_filetype is:" . cht_translated_current_filetype
"sleep 1000m
"5. Get the current date timestamp in yyyy_mm_dd_hh_mm_ss format store it in variable:
" (varname: current_timestamp)
let current_timestamp = strftime('%Y_%m_%d__%H_%M_%S')
"echo "current_timestamp for now is: " . current_timestamp
"sleep 1000m
"6. Construct the name of the file for current cache: (varname: cache_response_buffer_filename)
" Set this to /home/el/.vim/cht/garcpp/write+to+file
let cache_response_buffer_filename = "/home/el/.vim/cht/"
let cache_response_buffer_filename .= cht_translated_current_filetype . "/" . cht_translated_user_query
execute "silent !mkdir /home/el/.vim/cht/" . cht_translated_current_filetype . " 2>/dev/null"
"echo "cache_response_buffer_filename" . cache_response_buffer_filename
" filename/query
"7. Check if the file: cache_response_buffer_filename exists, if so put boolean true into
" (varname: cache_response_exists)
let cache_response_exists = filereadable(cache_response_buffer_filename)
"echo "cache_response_exists" . cache_response_exists
"8. if variable cache_response_exists is false, then this means cht was never ever hit for this query
" hit cht for a response by continuing at step 14
let hit_cht = 0
let cache_response_buffer_data = []
if cache_response_exists == 1
"9. If variable cache_response_exists is true, then it means cht was hit at some point.
" So we need to determine how long ago this was.
"10. Make a (variable: cached_response_timestamp) that receives the yyyy_mm_dd_hh_mm_ss of the file
" cache_response_buffer_filename.
let cache_response_timestamp = strftime("%Y_%m_%d__%H_%M_%S", getftime(cache_response_buffer_filename))
"echo "cache_response_timestamp: " . cache_response_timestamp
"11. If cached_response_timestamp is more than an hour ago, then we need to hit cht again
" proceed to step 14.
"if the file's timestamp is less than now minus 60*60
if getftime(cache_response_buffer_filename) < (localtime() - (60*60))
"echo "Need to repull from cht"
let hit_cht = 1
else
"echo "existing file is fine"
let hit_cht = 0
endif
"the old file exists, it has a timestamp, get its contents
let cache_response_buffer_data = readfile(cache_response_buffer_filename)
"echo "Here's what is there before: " . join(cache_response_buffer_data)
else
"continue to step 14
"echo "file doesn't exist, build it: " . cache_response_buffer_filename
let hit_cht = 1
endif
"14. Construct the curl expression to ping cht: as in: 'curl cht.sh/cpp/write+to+disk'
" put this in (varname: cht_hit_bash_expression)
if hit_cht == 1
"let cht_hit_bash_expression = "timeout 8 curl cht.sh/" . cht_translated_current_filetype
let cht_hit_bash_expression = "curl cht.sh/" . cht_translated_current_filetype
let cht_hit_bash_expression .= "/" . cht_translated_user_query . "?T 2>/dev/null"
"execute "! echo 'running this: '" . cht_hit_bash_expression
"15. Use vimscript to execute cht_hit_bash_expression and get the std output containing just the code.
" Put the (varname: cht_stdout_response)
"echo "2"
"sleep 1000m
let cht_stdout_response = system(cht_hit_bash_expression)
"echo "3"
"sleep 1000m
"echo "cht_stdout_response: " . cht_stdout_response
"16. Make a new (varname: cht_network_reachable) which is false if cht is unresponsive or our network
" is down
"string has barbaz so it prints yes
if cht_stdout_response =~ "Could not resolve host:"
""cht is not returning a response due to network problem
let cht_network_problem = 1
else
""cht is not returning a response due to network problem
let cht_network_problem = 0
endif
"echo "Network problem? " . cht_network_problem
"echo "4"
"sleep 1000m
"cht returned a non response, but it's blank or nonhelpful
if cht_stdout_response =~ "Please try to reformulate your query"
""cht is not returning a response due to network problem
let cht_no_good_response = 1
else
""cht is not returning a response due to network problem
let cht_no_good_response = 0
endif
"echo "No Good Response? " . cht_no_good_response
"17. if (varname: cht_network_reachable is false): Spit a big ass warning that cht IS DOWN,
" OR NETWORK IS DOWN. Sleep for 3 seconds then serve up tabedit: cache_response_buffer_filename.
if cht_network_problem == 1
echo "cht IS DOWN OR NETWORK IS DOWN"
let choice = input("press enter to continue")
execute "set cmdheight=1"
return
endif
if cht_no_good_response == 1
"This query is bad.
echo "this search string returned no results"
let choice = input("press enter to continue")
execute "set cmdheight=1"
return
endif
"echo "5"
"sleep 1000m
"let cht_tmp = "/tmp/cht_tmp"
"if !exists(cht_tmp)
" let foo = mkdir(cht_tmp)
"endif
if cht_translated_user_query != ""
let temporary_file = "/tmp/cht_tmp/" . cht_translated_user_query
else
echo "Fail, user query must not be blank"
execute "set cmdheight=1"
return
endif
"echo "6"
"sleep 1000m
"echo temporary_file
"TODO: Save the contents of cht_stdout_response to the temporary_file
call writefile(split( cht_stdout_response, "\n", 1), temporary_file)
"echo "7"
"sleep 1000m
else
"Decided against hitting the internets
"move along
execute "tabedit " . cache_response_buffer_filename | execute "set ft=" . current_filetype
"echo "1"
"sleep 1000m
execute "set cmdheight=1"
return
endif
"18. if (varname: cht_network_reachable is true), then we have some delicious (varname: cht_stdout_response)
" got result of execution from cht. don't present to screen yet, we need to store this appropriately
let file_contents = []
if (exists(cache_response_buffer_filename))
let file_contents = readfile(cache_response_buffer_filename)
endif
"Turn this off it's blanking the screen?
"echo readfile(cache_response_buffer_filename)
"TODO: Create a variable temporary_file that points to /tmp/ cht_translated_current_filetype
"echo "5"
"sleep 1000m
"read the tempfile back into variable lines, compare that to
let lines = readfile( temporary_file )
"echo "9"
"sleep 1000m
"TODO: make a boolean variable current_cht_pull_same_as_last_saved
let current_cht_pull_same_as_last_saved = Compare_list(file_contents, lines)
"echo "6"
"sleep 1000m
"echo "is last saved file the same as temporary file latest from cht?:" . current_cht_pull_same_as_last_saved
"let choice = input("\nenter to continue")
"TODO: do a diff between file cache_response_buffer_filename and temporary_file, if identitical
" set current_cht_pull_same_as_last_saved to true
"echo "10"
"sleep 1000m
if file_contents == [] || current_cht_pull_same_as_last_saved == 1
"It's the same, so just overwrite file_contents
"echo "11"
"sleep 1000m
call writefile(split( cht_stdout_response, "\n", 1), cache_response_buffer_filename)
"echo "12"
"sleep 1000m
execute "tabedit " . cache_response_buffer_filename | execute "set ft=" . current_filetype
else
"If the fresh pull isn't the same as file, then we need to moving existing to history plus timestamp
execute "silent !mkdir /home/el/.vim/cht/history/" . cht_translated_current_filetype . " 2>/dev/null"
let history_file_with_timestamp_name = "/home/el/.vim/cht/history/" . cht_translated_current_filetype . "/"
let history_file_with_timestamp_name .= cht_translated_user_query . current_timestamp
"echo "14"
"sleep 1000m
call writefile(split( join(file_contents, "\n"), "\n", 1), history_file_with_timestamp_name)
"echo "writing to file: " . history_file_with_timestamp_name
"echo "15"
"sleep 1000m
call writefile(split( cht_stdout_response, "\n", 1), cache_response_buffer_filename)
execute "set cmdheight=1"
"sleep 100m
execute "tabedit " . cache_response_buffer_filename | execute "set ft=" . current_filetype
endif
else
echo "cancelled"
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment