This repository was archived by the owner on Feb 23, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #441 from liquidz/dev
Next release
- Loading branch information
Showing
36 changed files
with
875 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
if !has('vim9script') | finish | endif | ||
vim9script | ||
|
||
def EncodeString(s: string): string | ||
return printf('%d:%s', strlen(s), s) | ||
enddef | ||
|
||
def EncodeNumber(i: number): string | ||
return printf('i%de', i) | ||
enddef | ||
|
||
def EncodeList(coll: list<any>): string | ||
var result: list<string> = [] | ||
for x in coll | ||
add(result, Encode(x)) | ||
endfor | ||
return printf('l%se', join(result, '')) | ||
enddef | ||
|
||
def EncodeDictionary(dict: dict<any>): string | ||
var result: list<string> = [] | ||
for k in keys(dict) | ||
add(result, Encode(k)) | ||
add(result, Encode(dict[k])) | ||
endfor | ||
return printf('d%se', join(result, '')) | ||
enddef | ||
|
||
export def Encode(x: any): string | ||
const t = type(x) | ||
if t == v:t_string | ||
return EncodeString(x) | ||
elseif t == v:t_number | ||
return EncodeNumber(x) | ||
elseif t == v:t_list | ||
return EncodeList(x) | ||
elseif t == v:t_dict | ||
return EncodeDictionary(x) | ||
elseif t == 7 # v:none or v:null | ||
return EncodeString('') | ||
endif | ||
return '' | ||
enddef | ||
|
||
def DecodeString(s: string): dict<any> | ||
const i = stridx(s, ':') | ||
if i == -1 | ||
throw 'Failed to parse string token' | ||
endif | ||
const len = str2nr(strpart(s, 0, i)) | ||
return {'value': strpart(s, i + 1, len), 'rest': strpart(s, i + len + 1)} | ||
enddef | ||
|
||
def DecodeNumber(s: string): dict<any> | ||
const i = stridx(s, 'e') | ||
if i == -1 | ||
throw 'Failed to parse integer token' | ||
endif | ||
return {'value': str2nr(strpart(s, 1, i - 1)), 'rest': strpart(s, i + 1)} | ||
enddef | ||
|
||
def DecodeList(s: string): dict<any> | ||
var result: list<any> = [] | ||
var tmp = strpart(s, 1) | ||
|
||
while tmp[0] != 'e' | ||
const decoded = DecodeRaw(tmp) | ||
add(result, decoded.value) | ||
tmp = trim(decoded.rest) | ||
endwhile | ||
|
||
return {'value': result, 'rest': strpart(tmp, 1)} | ||
enddef | ||
|
||
def DecodeDictionary(s: string): dict<any> | ||
var result: dict<any> = {} | ||
var tmp = strpart(s, 1) | ||
|
||
while tmp[0] != 'e' | ||
const k = DecodeRaw(tmp) | ||
const v = DecodeRaw(trim(k.rest)) | ||
result[k.value] = v.value | ||
tmp = trim(v.rest) | ||
endwhile | ||
|
||
return {'value': result, 'rest': strpart(tmp, 1)} | ||
enddef | ||
|
||
def DecodeRaw(s: string): dict<any> | ||
const c = s[0] | ||
var result: dict<any> = {} | ||
|
||
if c == 'i' | ||
result = DecodeNumber(s) | ||
elseif c =~ '[0-9]' | ||
result = DecodeString(s) | ||
elseif c == 'l' | ||
result = DecodeList(s) | ||
elseif c == 'd' | ||
result = DecodeDictionary(s) | ||
else | ||
throw 'Failed to parse bencode.' | ||
endif | ||
|
||
return result | ||
enddef | ||
|
||
export def Decode(s: string): any | ||
var result: list<any> = [] | ||
var decoding: bool = true | ||
var tmp = s | ||
|
||
while decoding | ||
const decoded = DecodeRaw(tmp) | ||
if ! has_key(decoded, 'value') | ||
decoding = false | ||
else | ||
add(result, decoded.value) | ||
tmp = trim(decoded.rest) | ||
|
||
if tmp == '' | ||
decoding = false | ||
endif | ||
endif | ||
endwhile | ||
|
||
if len(result) == 1 | ||
return result[0] | ||
endif | ||
return result | ||
enddef | ||
# vim:ft=vim |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
let s:save_cpo = &cpoptions | ||
set cpoptions&vim | ||
|
||
let s:script_path = printf('%s/bencode.vim9script', expand('<sfile>:h')) | ||
import s:script_path as that | ||
let s:bencode = {} | ||
|
||
function! s:bencode.encode(v) abort | ||
return s:that.Encode(a:v) | ||
endfunction | ||
|
||
function! s:bencode.decode(s) abort | ||
return s:that.Decode(a:s) | ||
endfunction | ||
|
||
function! iced#component#bencode#vim9#start(_) abort | ||
call iced#util#debug('start', 'vim9 bencode') | ||
return s:bencode | ||
endfunction | ||
|
||
let &cpoptions = s:save_cpo | ||
unlet s:save_cpo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
let s:save_cpo = &cpoptions | ||
set cpoptions&vim | ||
|
||
let s:spinner = { | ||
\ 'timer': '', | ||
\ 'virtual_text': '', | ||
\ 'working_spinners': {}, | ||
\ } | ||
|
||
function! s:start_spinner__spinner(uniq_key, opt) abort dict | ||
let buffer = get(a:opt, 'buffer', bufnr('%')) | ||
let line = get(a:opt, 'line', line('.')) | ||
let idx = get(a:opt, 'index', 0) | ||
let texts = get(a:opt, 'texts', [' |', ' /', '-', ' \']) | ||
let highlight = get(a:opt, 'highlight', 'Comment') | ||
let align = get(a:opt, 'align', 'after') | ||
let interval = get(a:opt, 'interval', 200) | ||
|
||
let idx = idx >= len(texts) ? 0 : idx | ||
|
||
if has_key(self.working_spinners, a:uniq_key) | ||
call self.virtual_text.set(texts[idx], { | ||
\ 'highlight': highlight, | ||
\ 'align': align, | ||
\ 'buffer': buffer, | ||
\ 'line': line, | ||
\ 'auto_clear': v:false, | ||
\ 'indent': 0, | ||
\ }) | ||
|
||
let new_opt = copy(a:opt) | ||
let new_opt['index'] = idx + 1 | ||
let new_opt['buffer'] = buffer | ||
let new_opt['line'] = line | ||
return self.timer.start( | ||
\ interval, | ||
\ {-> call(funcref('s:start_spinner__spinner'), [a:uniq_key, new_opt], self)}) | ||
endif | ||
endfunction | ||
|
||
function! s:spinner.start(uniq_key, opt) abort | ||
" NOTE: Spinner is only supported by Vim9/Neovim | ||
if self.virtual_text.env ==# 'vim8' | ||
return | ||
endif | ||
|
||
let self.working_spinners[a:uniq_key] = v:true | ||
return self.timer.start(200, {-> call(funcref('s:start_spinner__spinner'), [a:uniq_key, a:opt], self)}) | ||
endfunction | ||
|
||
function! s:spinner.stop(uniq_key) abort | ||
if has_key(self.working_spinners, a:uniq_key) | ||
unlet self.working_spinners[a:uniq_key] | ||
endif | ||
endfunction | ||
|
||
function! iced#component#spinner#start(this) abort | ||
call iced#util#debug('start', 'spinner') | ||
let d = deepcopy(s:spinner) | ||
let d['timer'] = a:this.timer | ||
let d['virtual_text'] = a:this.virtual_text | ||
return d | ||
endfunction | ||
|
||
let &cpoptions = s:save_cpo | ||
unlet s:save_cpo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.