/
util.vim
158 lines (132 loc) · 4.2 KB
/
util.vim
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
" ============================================================================
" FileName: util.vim
" Author: voldikss <dyzplus@gmail.com>
" GitHub: https://github.com/voldikss
" ============================================================================
scriptencoding utf-8
function! translator#util#echo(group, msg) abort
if a:msg ==# '' | return | endif
execute 'echohl' a:group
echo a:msg
echon ' '
echohl NONE
endfunction
function! translator#util#echon(group, msg) abort
if a:msg ==# '' | return | endif
execute 'echohl' a:group
echon a:msg
echon ' '
echohl NONE
endfunction
function! translator#util#show_msg(message, ...) abort
if a:0 == 0
let msg_type = 'info'
else
let msg_type = a:1
endif
if type(a:message) != 1
let message = string(a:message)
else
let message = a:message
endif
call translator#util#echo('Constant', '[vim-translator]')
if msg_type ==# 'info'
call translator#util#echon('Normal', message)
elseif msg_type ==# 'warning'
call translator#util#echon('WarningMsg', message)
elseif msg_type ==# 'error'
call translator#util#echon('Error', message)
endif
endfunction
function! translator#util#padding(text, width, char) abort
let padding_size = (a:width - strdisplaywidth(a:text)) / 2
let padding = repeat(a:char, padding_size / strdisplaywidth(a:char))
let padend = repeat(a:char, (a:width - strdisplaywidth(a:text)) % 2)
let text = padding . a:text . padding
if a:width >= strdisplaywidth(text) + strdisplaywidth(padend)
let text .= padend
endif
return text
endfunction
function! translator#util#fit_lines(linelist, width) abort
for i in range(len(a:linelist))
let line = a:linelist[i]
if match(line, '───') ==# 0 && a:width > strdisplaywidth(line)
let a:linelist[i] = translator#util#padding(a:linelist[i], a:width, '─')
elseif match(line, '⟦') ==# 0 && a:width > strdisplaywidth(line)
let a:linelist[i] = translator#util#padding(a:linelist[i], a:width, ' ')
endif
endfor
return a:linelist
endfunction
" Style always makes me frantic
function! translator#util#build_lines(translations) abort
let marker = '• '
let content = []
if len(a:translations['text']) > 30
let text = a:translations['text'][:30] . '...'
else
let text = a:translations['text']
endif
call add(content, printf('⟦ %s ⟧', text))
for t in a:translations['results']
if empty(t.paraphrase) && empty(t.explain)
continue
endif
call add(content, '')
call add(content, printf('─── %s ───', t.engine))
if !empty(t.phonetic)
let phonetic = marker . printf('[%s]', t.phonetic)
call add(content, phonetic)
endif
if !empty(t.paraphrase)
let paraphrase = marker . t.paraphrase
call add(content, paraphrase)
endif
if !empty(t.explain)
for expl in t.explain
let expl = translator#util#safe_trim(expl)
if !empty(expl)
let explain = marker . expl
call add(content, explain)
endif
endfor
endif
endfor
call translator#debug#info(content)
return content
endfunction
function! translator#util#visual_select(range, line1, line2) abort
if a:range == 0
let lines = [expand('<cword>')]
elseif a:range == 1
let lines = [getline('.')]
else
if a:line1 == a:line2
" https://vi.stackexchange.com/a/11028/17515
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
let lines = getline(lnum1, lnum2)
if empty(lines)
call floaterm#util#show_msg('No lines were selected', 'error')
return
endif
let lines[-1] = lines[-1][: col2 - 1]
let lines[0] = lines[0][col1 - 1:]
else
let lines = getline(a:line1, a:line2)
endif
endif
return join(lines)
endfunction
function! translator#util#safe_trim(text) abort
return substitute(a:text,'\%#=1^[[:space:]]\+\|[[:space:]]\+$', '', 'g')
endfunction
function! translator#util#text_proc(text) abort
let text = substitute(a:text, "\n", ' ', 'g')
let text = substitute(text, "\n\r", ' ', 'g')
let text = substitute(text, '\v^\s+', '', '')
let text = substitute(text, '\v\s+$', '', '')
let text = shellescape(text)
return text
endfunction