-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
splash_view.v
211 lines (187 loc) · 5.43 KB
/
splash_view.v
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2023 The Lilly Editor contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
module main
import math
import term { strikethrough }
import lib.draw
const logo_contents = $embed_file('./src/splash-logo.txt')
struct Logo {
mut:
data []string
width int
}
struct SplashScreen {
commit_hash string
pub:
file_path string
mut:
logo Logo
leader_state LeaderState
leader_key string
}
struct LeaderState {
mut:
special bool
normal bool
x_count int
f_count int
b_count int
leader_mode bool
}
fn reset_leader_state(mut state LeaderState) {
state.leader_mode = false
state.f_count = 0
state.b_count = 0
state.special = false
state.normal = false
}
pub fn new_splash(commit_hash string, leader_key string) Viewable {
assert commit_hash.len > 0
assert leader_key.len == 1
mut splash := SplashScreen{
commit_hash: commit_hash
file_path: '**lss**'
logo: Logo{
data: logo_contents.to_string().split_into_lines()
}
leader_key: leader_key
}
for l in splash.logo.data {
assert l.len >= 1
if l.len > splash.logo.width {
splash.logo.width = l.len
}
}
return splash
}
pub fn (splash SplashScreen) draw(mut ctx draw.Contextable) {
offset_x := 1
mut offset_y := 1 + f64(ctx.window_height()) * 0.1
assert offset_y > 1
ctx.set_color(r: 245, g: 191, b: 243)
for i, l in splash.logo.data {
start_x := offset_x + (ctx.window_width() / 2) - (l.runes().len / 2)
assert start_x > 2
if has_colouring_directives(l) {
for j, c in l.runes() {
mut to_draw := '${c}'
if to_draw == 'g' {
to_draw = ' '
ctx.set_color(r: 97, g: 242, b: 136)
}
if to_draw == 'p' {
to_draw = ' '
ctx.set_color(r: 245, g: 191, b: 243)
}
ctx.draw_text(start_x + j, int(math.floor(offset_y)) + i, to_draw)
}
continue
}
ctx.draw_text(offset_x + (ctx.window_width() / 2) - (l.runes().len / 2),
int(math.floor(offset_y)) + i, l)
}
ctx.reset_color()
offset_y += splash.logo.data.len
offset_y += (ctx.window_height() - offset_y) * 0.05
version_label := 'lilly - dev version (#${splash.commit_hash}) leader = ${resolve_whitespace_to_name(splash.leader_key)}'
// version_label := "lilly - dev version (#${gitcommit_hash})"
ctx.draw_text(offset_x + (ctx.window_width() / 2) - (version_label.len / 2), int(math.floor(offset_y)),
version_label)
offset_y += 2
basic_command_help := [
' Find File <leader>ff',
]
disabled_command_help := [
' Find Word <leader>fg',
' Recent Files <leader>fo',
' File Browser <leader>fv',
' Colorschemes <leader>cs',
' New File <leader>nf',
]
for h in basic_command_help {
ctx.draw_text(offset_x + (ctx.window_width() / 2) - (h.len / 2), int(math.floor(offset_y)),
h)
offset_y += 2
}
for dh in disabled_command_help {
ctx.draw_text(offset_x + (ctx.window_width() / 2) - (dh.len / 2), int(math.floor(offset_y)),
strikethrough(dh))
offset_y += 2
}
exit_label_str := 'Exit/Quit ESC'
ctx.draw_text(offset_x + (ctx.window_width() / 2) - (exit_label_str.len / 2), int(math.floor(offset_y)),
exit_label_str)
offset_y += 2
copyright_footer := 'the lilly editor authors ©'
ctx.draw_text(offset_x + (ctx.window_width() / 2) - (copyright_footer.len / 2), int(math.floor(offset_y)),
copyright_footer)
}
fn resolve_whitespace_to_name(leader_key string) string {
match leader_key {
" " { return "space" }
else { return leader_key }
}
}
fn has_colouring_directives(line string) bool {
for c in line.split('') {
if c == 'g' || c == 'p' {
return true
}
}
return false
}
pub fn (mut splash SplashScreen) on_key_down(e draw.Event, mut root Root) {
match e.utf8 {
splash.leader_key { splash.leader_state.leader_mode = true }
else {}
}
match e.code {
.escape {
if splash.leader_state.leader_mode {
reset_leader_state(mut splash.leader_state)
return
}
root.quit() or {}
}
// leader_key { splash.leader_mode = true }
// TODO(tauraamui): move to f() method, this line is a too complicated/long statement now
.x {
if splash.leader_state.leader_mode {
splash.leader_state.x_count += 1
if !splash.leader_state.normal { splash.leader_state.special = true }
}
}
.f {
if splash.leader_state.leader_mode {
splash.leader_state.f_count += 1
if !splash.leader_state.special { splash.leader_state.normal = true }
}
if splash.leader_state.f_count == 2 {
root.open_file_finder(splash.leader_state.special)
reset_leader_state(mut splash.leader_state)
}
}
.b {
if splash.leader_state.leader_mode {
splash.leader_state.b_count += 1
if !splash.leader_state.special { splash.leader_state.normal = true }
if splash.leader_state.f_count == 1 && splash.leader_state.b_count >= 1 {
root.open_inactive_buffer_finder(splash.leader_state.special)
reset_leader_state(mut splash.leader_state)
}
}
}
else {}
}
}