aboutsummaryrefslogtreecommitdiffstats
path: root/i_events.go
blob: 372623e06c8dcc6f133f2eae7ddfc8d2e683d69e (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * ========================
 * =====    ===============
 * ======  ================
 * ======  ================
 * ======  ====   ====   ==
 * ======  ===     ==  =  =
 * ======  ===  =  ==     =
 * =  ===  ===  =  ==  ====
 * =  ===  ===  =  ==  =  =
 * ==     =====   ====   ==
 * ========================
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 2023-2024, Joe
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the organization nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * hardflip: src/i_events.go
 * Wed Jan 10 12:04:16 2024
 * Joe
 *
 * events in the code
 */

package main

import (
	"os"

	"github.com/gdamore/tcell/v2"
	"golang.org/x/term"
)

// func i_update_folded_count(dir *DirsNode, ui *HardUI) {
// 	delta := 0
// 	delta += dir.count_elements()
// 	if dir.Folded == false {
// 		delta *= -1
// 	}
// 	ui.folded_count += delta
// }

func i_list_follow_cursor(litems *ItemsList, ui *HardUI) {
	if litems.draw_start == nil || litems.curr == nil {
		return
	}
	virt_id := litems.curr.ID - (ui.dim[H] - 4) // - ui.folded_count
	for litems.draw_start.ID < virt_id &&
		litems.draw_start.next != nil {
		litems.draw_start = litems.draw_start.next
	}
	for litems.draw_start.ID > litems.curr.ID &&
		litems.draw_start.prev != nil {
		litems.draw_start = litems.draw_start.prev
	}
	// fmt.Println(">>>>> DRAW_START:", litems.draw_start.ID, "<<<<<<< >>>>>>>> VIRT_ID:", virt_id)
	// if litems.draw_start.prev != nil &&
	//    litems.draw_start.prev.is_dir() == true &&
	//    litems.draw_start.prev.Dirs.Folded == true {
		// tmp := litems.draw_start.prev.Dirs.count_elements()
		// for i := 0; i < tmp && litems.draw_start != nil; i++ {
		// 	litems.draw_start = litems.draw_start.next
		// }
	// }
}

func i_unfold_dir(data *HardData, item *ItemsNode) {
	if item == nil {
		return
	}
	fold := data.folds[item]
	if fold == nil {
		return
	}
	after := item.next
	item.next = fold.head
	fold.head.prev = item
	fold.last.next = after
	if after != nil {
		after.prev = fold.last
	} else {
		data.litems.last = fold.last
	}
	delete(data.folds, item)
	for ptr := data.litems.head; ptr.next != nil; ptr = ptr.next {
		ptr.next.ID = ptr.ID + 1
	}
}

func i_fold_dir(data *HardData, item *ItemsNode) {
	if item == nil || item.Dirs == nil {
		return
	}
	folds := data.folds
	folded_start := item.next
	folded_start.prev = nil
	folded_end := item
	for i := 0; i < item.Dirs.count_elements(true) && folded_end != nil; i++ {
		folded_end = folded_end.next
	}
	after := folded_end.next
	folded_end.next = nil
	tmp := ItemsList{
		folded_start,
		folded_end,
		nil,
		nil,
	}
	item.next = after
	if after != nil {
		after.prev = item
	} else {
		data.litems.last = item
	}

	folds[item] = &tmp
	for ptr := data.litems.head; ptr.next != nil; ptr = ptr.next {
		ptr.next.ID = ptr.ID + 1
	}
}

func i_reload_data(data *HardData) {
	data.data_dir = c_get_data_dir()
	data.ldirs = c_load_data_dir(data.data_dir, data.opts)
	data.litems = c_load_litems(data.ldirs)
	data.ui.sel_max = data.litems.last.ID
}

func i_delete_host(data *HardData) {
	curr := data.litems.curr	
	if curr.is_dir() == true {
		return
	}
	host := curr.Host
	if host == nil {
		return
	}
	file_path := data.data_dir + host.Parent.path() + host.Filename

	if err := os.Remove(file_path); err != nil {
		c_die("can't remove " + file_path, err)
	}
	var tmp *ItemsNode
	host.Parent.lhost.del(host)
	if data.litems.curr != nil {
		tmp = data.litems.curr.prev
	}
	data.litems.del(curr)
	if tmp == nil {
		tmp = data.litems.head
	}
	data.litems.curr = tmp
	if data.litems.last != nil {
		data.ui.sel_max = data.litems.last.ID
	} else {
		data.ui.sel_max = 0
	}
}

// screen events such as keypresses
func i_events(data *HardData) {
	var err error
	ui := &data.ui
	event := ui.s.PollEvent()
	switch event := event.(type) {
	case *tcell.EventResize:
		ui.dim[W], ui.dim[H], _ = term.GetSize(0)
		i_list_follow_cursor(data.litems, ui)
		ui.s.Sync()
	case *tcell.EventKey:
		switch ui.mode {
		case NORMAL_MODE:
			if event.Key() == tcell.KeyCtrlC ||
			   event.Rune() == 'q' {
				ui.s.Fini()
				os.Exit(0)
			} else if event.Rune() == 'j' ||
				      event.Key() == tcell.KeyDown {
				data.litems.inc(+1)
			} else if event.Rune() == 'k' ||
					  event.Key() == tcell.KeyUp {
				data.litems.inc(-1)
			} else if event.Key() == tcell.KeyCtrlD {
				data.litems.inc(+(ui.dim[H] / 3))
			} else if event.Key() == tcell.KeyCtrlU {
				data.litems.inc(-(ui.dim[H] / 3))
			} else if event.Rune() == 'g' {
				data.litems.curr = data.litems.head
				data.litems.draw_start = data.litems.head
			} else if event.Rune() == 'G' {
				data.litems.curr = data.litems.last
				// for data.litems.curr.prev != nil &&
					// data.litems.curr.folded_parents() == true {
					// data.litems.curr = data.litems.curr.prev
				// }
			} else if event.Rune() == 'D' &&
					  data.ldirs.head != nil &&
					  ui.sel_max != 0 {
				ui.mode = DELETE_MODE
			} else if event.Key() == tcell.KeyEnter {
				if data.litems.curr == nil {
					break
				} else if data.litems.curr.is_dir() == false {
					ui.s.Fini()
					c_exec(data.litems.curr.Host)
					if data.opts.Loop == false {
						os.Exit(0)
					} else {
						if ui.s, err = tcell.NewScreen(); err != nil {
							c_die("view", err)
						}
						if err := ui.s.Init(); err != nil {
							c_die("view", err)
						}
						ui.s.SetStyle(ui.def_style)
					}
				} else {
					if data.litems.curr.Dirs.Folded == false {
						data.litems.curr.Dirs.Folded = true
						i_fold_dir(data, data.litems.curr)
					} else {
						data.litems.curr.Dirs.Folded = false
						i_unfold_dir(data, data.litems.curr)
					}
				}
			} else if event.Rune() == ' ' {
				if data.litems.curr == nil ||
				   data.litems.curr.is_dir() == false {
					break
				}
				if data.litems.curr.Dirs.Folded == false {
					data.litems.curr.Dirs.Folded = true
				} else {
					data.litems.curr.Dirs.Folded = false
				}
				// i_update_folded_count(data.litems.curr.Dirs, ui)
			} else if event.Key() == tcell.KeyCtrlR {
				i_reload_data(data)
			}
			i_list_follow_cursor(data.litems, ui)
		case DELETE_MODE:
			if event.Key() == tcell.KeyEscape ||
			   event.Key() == tcell.KeyCtrlC ||
			   event.Rune() == 'q' ||
			   event.Rune() == 'n' {
				ui.mode = NORMAL_MODE
			} else if event.Rune() == 'y' {
				i_delete_host(data)
				ui.mode = NORMAL_MODE
			}
		}	
	}
}