// File: /workspace/github_cpp_makefile_star2_O2/juli9797[P]ssf/src/main.cpp
// Function: main
int main()
{
	try
	{
		ssf::log << "Start\n";

		ssf::ConsoleSettings console_settings; // Enables Raw Mode

		auto cols = console_settings.get_col();
		auto rows = console_settings.get_row();

		ssf::ConsoleInputHandler input_handler;

		ssf::Filetree tree;

		ssf::ConsolePage page;
                page.set_col_width(0, (cols - 6) * 0.2)
                    .set_col_width(1, (cols - 6) * 0.25)
                    .set_col_width(2, (cols - 6) * 0.55)
                    .set_spacing(2)
                    .set_row_count(rows - 1);

                draw_to_page(page, tree);

		ssf::ConsoleScreen screen;
		screen.set_post_draw_callback([&]() {
			// Add default screen print here
			// updates on every draw (<<)
		});

		ssf::CommandLine cmd_line;
		cmd_line.set_row(rows - 1);

		// needs acess to draw function bc it handles user input
		cmd_line.set_draw_cb([&](std::string s) {
			screen << s;
		});
		cmd_line.set_command_cb([&](std::string s) {
			screen << page.str(); // Redraw to remove cmd_line
			ssf::log << "Command: " << s << "\n";
		});

		// Command input
		char const cmd_mode_key = ':';
		input_handler.register_callback(cmd_mode_key, [&]() {
			cmd_line.clear_text();
			cmd_line << ":";
			screen << cmd_line.str();
		});

		input_handler.register_callback('q', []() {
			throw ssf::close_program_ex_t();
		});

		input_handler.register_callback('j', [&]() {
			tree.move_down();
			draw_to_page(page, tree);
			screen << page.str();
		});

		input_handler.register_callback('k', [&]() {
			tree.move_up();
			draw_to_page(page, tree);
			screen << page.str();
		});

		input_handler.register_callback('h', [&]() {
			tree.move_left();
			draw_to_page(page, tree);
			screen << page.str();
		});

		input_handler.register_callback('l', [&]() {
			tree.move_right();
			draw_to_page(page, tree);
			screen << page.str();
		});

		// Test Callback to open a shell
		// cmd and call logic needs to be implemented elsewhere
		input_handler.register_callback('s', [&]() {
			std::string cmd = {"gnome-terminal --working-directory="};
			cmd += ssf::enquote(tree.get_current_path().string());
			ssf::sys_call_silent(cmd);
		});
		// Quick and dirty xdg-open
		input_handler.register_callback('o', [&]() {
			std::string cmd = "xdg-open ";
			cmd += ssf::enquote(tree.get_selected_path().string());
			ssf::sys_call_silent(cmd);
		});

		tree.set_move_right_on_file_cb([&]() {
			std::string cmd = "xdg-open ";
			cmd += ssf::enquote(tree.get_selected_path().string());
                        ssf::sys_call_silent(cmd);
		});

		//Quick and dirty vim edit
		input_handler.register_callback('e', [&]() {
			std::string cmd = "vim ";
			cmd += ssf::enquote(tree.get_selected_path().string());
			ssf::sys_call(cmd);
		});
		//Toggle dotfiles
		input_handler.register_callback('d', [&]() {
			tree.set_hide_dot_files(!tree.get_hide_dot_files());
			draw_to_page(page, tree);
			screen << page.str();
		});
		ssf::log << "Main keypress loop\n";

		screen << page.str();

		while (true)
		{
			input_handler.loop_until(cmd_mode_key);
			cmd_line.loop_until(27); // ESC
		}
	}
	catch (ssf::close_program_ex_t)
	{
		// Makes sure to call all dtors
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3f8, %rsp            # imm = 0x3F8
leaq 0x32c(%rsp), %rbx
movq %rbx, %rdi
callq 0x513a
movq %rbx, %rdi
callq 0x51a2
movl %eax, %r14d
movq %rbx, %rdi
callq 0x51c8
movl %eax, %ebp
leaq 0x58(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
leaq 0x1e0(%rsp), %rdi
callq 0x51ee
movabsq $0x200000014, %rax      # imm = 0x200000014
leaq 0x368(%rsp), %rbx
movq %rax, (%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rbx)
movupd %xmm0, 0x20(%rbx)
movupd %xmm0, 0x30(%rbx)
movl $0x1, 0x40(%rbx)
movupd %xmm0, 0x50(%rbx)
andq $0x0, 0x60(%rbx)
addl $-0x6, %r14d
cvtsi2sd %r14d, %xmm0
movapd %xmm0, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
mulpd 0x5230(%rip), %xmm1     # 0x9010
cvttpd2dq %xmm1, %xmm2
movapd %xmm2, %xmm3
psrad $0x1f, %xmm3
addpd 0x522b(%rip), %xmm1     # 0x9020
cvttpd2dq %xmm1, %xmm1
andpd %xmm3, %xmm1
orpd %xmm2, %xmm1
mulsd 0x5277(%rip), %xmm0     # 0x9080
cvttsd2si %xmm0, %rax
movlpd %xmm1, 0x44(%rbx)
movl %eax, 0x4c(%rbx)
decl %ebp
movl %ebp, 0x8(%rbx)
leaq 0x1e0(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3c21
movq %rsp, %rdi
callq 0x5310
movaps (%rsp), %xmm0
movaps 0x10(%rsp), %xmm1
leaq 0x2c0(%rsp), %r12
movaps %xmm0, (%r12)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
leaq 0x62a(%rip), %rax       # 0x4484
movq %rax, 0x10(%rsp)
movaps %xmm1, 0x10(%r12)
leaq 0x616(%rip), %rax       # 0x4482
movq %rax, 0x18(%rsp)
movq %r12, %rdi
callq 0x7af6
leaq 0x2d8(%rsp), %r15
movq %r15, -0x10(%r15)
andq $0x0, -0x8(%r15)
movb $0x0, (%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r15)
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x30(%r15)
movups %xmm0, 0x40(%r15)
movl %ebp, -0x18(%r15)
leaq 0x140(%rsp), %r13
movups %xmm0, 0x8(%r13)
movq %rsp, %rax
movq %rax, (%r13)
movq (%r13), %rax
movq 0x8(%r13), %rcx
movaps %xmm0, (%r13)
movq %rax, 0x30(%r15)
movq %rcx, 0x38(%r15)
andq $0x0, 0x18(%r13)
leaq 0x632(%rip), %rax       # 0x4510
movq %rax, 0x40(%r15)
leaq 0x5b3(%rip), %rax       # 0x449c
movq %rax, 0x48(%r15)
movq %r13, %rdi
callq 0x7af6
movupd 0x10(%r15), %xmm0
movupd 0x20(%r15), %xmm1
movapd %xmm0, (%r13)
movq %rsp, %rax
movq %rax, 0x10(%r15)
movq %rbx, 0x18(%r15)
leaq 0x68f(%rip), %rax       # 0x45a8
movq %rax, 0x20(%r15)
movapd %xmm1, 0x10(%r13)
leaq 0x60c(%rip), %rax       # 0x4536
movq %rax, 0x28(%r15)
movq %rsp, %r15
leaq 0x140(%rsp), %rdi
callq 0x7af6
leaq 0x1c0(%rsp), %rdx
movq %r12, (%rdx)
movq %r15, 0x8(%rdx)
leaq 0x67c(%rip), %rax       # 0x45d0
movq %rax, 0x18(%rdx)
leaq 0x70d(%rip), %rax       # 0x466c
movq %rax, 0x10(%rdx)
leaq 0x28(%rsp), %rdi
pushq $0x3a
popq %rsi
callq 0x537c
leaq 0x1c0(%rsp), %rdi
callq 0x7af6
leaq 0x120(%rsp), %rdx
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdx)
leaq 0x700(%rip), %rax       # 0x4694
movq %rax, 0x18(%rdx)
leaq 0x70f(%rip), %rax       # 0x46ae
movq %rax, 0x10(%rdx)
leaq 0x28(%rsp), %rdi
pushq $0x71
popq %rsi
callq 0x537c
leaq 0x120(%rsp), %rdi
callq 0x7af6
xorpd %xmm0, %xmm0
movapd %xmm0, 0xf0(%rsp)
movapd %xmm0, 0xe0(%rsp)
pushq $0x18
popq %rdi
callq 0x3220
movq %r14, (%rax)
movq %rbx, 0x8(%rax)
movq %r15, 0x10(%rax)
leaq 0xe0(%rsp), %rdx
movq %rax, (%rdx)
leaq 0x6ce(%rip), %rax       # 0x46c6
movq %rax, 0x18(%rdx)
leaq 0x729(%rip), %rax       # 0x472c
movq %rax, 0x10(%rdx)
leaq 0x28(%rsp), %rdi
pushq $0x6a
popq %rsi
callq 0x537c
leaq 0xe0(%rsp), %rdi
callq 0x7af6
xorpd %xmm0, %xmm0
movapd %xmm0, 0xd0(%rsp)
movapd %xmm0, 0xc0(%rsp)
pushq $0x18
popq %rdi
callq 0x3220
movq %r14, (%rax)
movq %rbx, 0x8(%rax)
movq %r15, 0x10(%rax)
leaq 0xc0(%rsp), %rdx
movq %rax, (%rdx)
leaq 0x730(%rip), %rax       # 0x478c
movq %rax, 0x18(%rdx)
leaq 0x78b(%rip), %rax       # 0x47f2
movq %rax, 0x10(%rdx)
leaq 0x28(%rsp), %rdi
pushq $0x6b
popq %rsi
callq 0x537c
leaq 0xc0(%rsp), %rdi
callq 0x7af6
xorpd %xmm0, %xmm0
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0xa0(%rsp)
pushq $0x18
popq %rdi
callq 0x3220
movq %r14, (%rax)
movq %rbx, 0x8(%rax)
movq %r15, 0x10(%rax)
leaq 0xa0(%rsp), %rdx
movq %rax, (%rdx)
leaq 0x792(%rip), %rax       # 0x4852
movq %rax, 0x18(%rdx)
leaq 0x7ed(%rip), %rax       # 0x48b8
movq %rax, 0x10(%rdx)
leaq 0x28(%rsp), %rdi
pushq $0x68
popq %rsi
callq 0x537c
leaq 0xa0(%rsp), %rdi
callq 0x7af6
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
pushq $0x18
popq %rdi
callq 0x3220
movq %r14, (%rax)
movq %rbx, 0x8(%rax)
movq %r15, 0x10(%rax)
leaq 0x80(%rsp), %rdx
movq %rax, (%rdx)
leaq 0x7f4(%rip), %rax       # 0x4918
movq %rax, 0x18(%rdx)
leaq 0x84f(%rip), %rax       # 0x497e
movq %rax, 0x10(%rdx)
leaq 0x28(%rsp), %rdi
pushq $0x6c
popq %rsi
callq 0x537c
leaq 0x80(%rsp), %rdi
callq 0x7af6
leaq 0x1a0(%rsp), %rdx
andq $0x0, 0x8(%rdx)
movq %r14, (%rdx)
leaq 0x87a(%rip), %rax       # 0x49de
movq %rax, 0x18(%rdx)
leaq 0x975(%rip), %rax       # 0x4ae4
movq %rax, 0x10(%rdx)
leaq 0x28(%rsp), %rdi
pushq $0x73
popq %rsi
callq 0x537c
leaq 0x1a0(%rsp), %rdi
callq 0x7af6
leaq 0x180(%rsp), %rdx
andq $0x0, 0x8(%rdx)
movq %r14, (%rdx)
leaq 0x966(%rip), %rax       # 0x4b0a
movq %rax, 0x18(%rdx)
leaq 0xa5d(%rip), %rax       # 0x4c0c
movq %rax, 0x10(%rdx)
leaq 0x28(%rsp), %rdi
pushq $0x6f
popq %rsi
callq 0x537c
leaq 0x180(%rsp), %rdi
callq 0x7af6
leaq 0x140(%rsp), %rdi
andq $0x0, 0x8(%rdi)
movq %r14, (%rdi)
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
movupd 0x2a0(%rsp), %xmm0
movupd 0x2b0(%rsp), %xmm1
movapd %xmm0, (%rdi)
movq %rax, 0x2a0(%rsp)
movq %rcx, 0x2a8(%rsp)
leaq 0xb23(%rip), %rax       # 0x4d34
movq %rax, 0x2b0(%rsp)
movapd %xmm1, 0x10(%rdi)
leaq 0xa0d(%rip), %rax       # 0x4c32
movq %rax, 0x2b8(%rsp)
callq 0x7af6
leaq 0x160(%rsp), %rdx
andq $0x0, 0x8(%rdx)
movq %r14, (%rdx)
leaq 0xb11(%rip), %rax       # 0x4d5a
movq %rax, 0x18(%rdx)
leaq 0xc08(%rip), %rax       # 0x4e5c
movq %rax, 0x10(%rdx)
leaq 0x28(%rsp), %rdi
pushq $0x65
popq %rsi
callq 0x537c
leaq 0x160(%rsp), %rdi
callq 0x7af6
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
pushq $0x18
popq %rdi
callq 0x3220
movq %r14, (%rax)
movq %rbx, 0x8(%rax)
movq %rsp, %rcx
movq %rcx, 0x10(%rax)
leaq 0x60(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xbdb(%rip), %rax       # 0x4e82
movq %rax, 0x18(%rdx)
leaq 0xc3c(%rip), %rax       # 0x4eee
movq %rax, 0x10(%rdx)
leaq 0x28(%rsp), %rdi
pushq $0x64
popq %rsi
callq 0x537c
leaq 0x60(%rsp), %rdi
callq 0x7af6
leaq 0x100(%rsp), %rdi
leaq 0x368(%rsp), %rsi
callq 0x53dc
movq %rsp, %rdi
leaq 0x100(%rsp), %rsi
callq 0x53a6
leaq 0x100(%rsp), %rdi
callq 0x30f0
leaq 0x28(%rsp), %rbx
pushq $0x3a
popq %rbp
leaq 0x2c0(%rsp), %r14
pushq $0x1b
popq %r15
movq %rbx, %rdi
movl %ebp, %esi
callq 0x5970
movq %r14, %rdi
movl %r15d, %esi
callq 0x59de
jmp 0x4313
jmp 0x4344
movq %rdx, %rbx
movq %rax, %r14
leaq 0x100(%rsp), %rdi
callq 0x30f0
jmp 0x43ff
movq %rdx, %rbx
movq %rax, %r14
jmp 0x43ff
jmp 0x4351
movq %rdx, %rbx
movq %rax, %r14
leaq 0x60(%rsp), %rdi
jmp 0x43fa
movq %rdx, %rbx
movq %rax, %r14
leaq 0x160(%rsp), %rdi
jmp 0x43fa
movq %rdx, %rbx
movq %rax, %r14
leaq 0x180(%rsp), %rdi
jmp 0x43fa
movq %rdx, %rbx
movq %rax, %r14
leaq 0x1a0(%rsp), %rdi
jmp 0x43fa
jmp 0x4396
movq %rdx, %rbx
movq %rax, %r14
leaq 0x80(%rsp), %rdi
jmp 0x43fa
jmp 0x43a8
movq %rdx, %rbx
movq %rax, %r14
leaq 0xa0(%rsp), %rdi
jmp 0x43fa
jmp 0x43ba
movq %rdx, %rbx
movq %rax, %r14
leaq 0xc0(%rsp), %rdi
jmp 0x43fa
jmp 0x43cc
movq %rdx, %rbx
movq %rax, %r14
leaq 0xe0(%rsp), %rdi
jmp 0x43fa
movq %rdx, %rbx
movq %rax, %r14
leaq 0x120(%rsp), %rdi
jmp 0x43fa
movq %rdx, %rbx
movq %rax, %r14
leaq 0x1c0(%rsp), %rdi
callq 0x7af6
leaq 0x2c0(%rsp), %rdi
callq 0x5b2a
movq %rsp, %rdi
callq 0x5b4e
jmp 0x441e
jmp 0x4418
movq %rdx, %rbx
movq %rax, %r14
leaq 0x368(%rsp), %rdi
callq 0x5bce
leaq 0x1e0(%rsp), %rdi
callq 0x5bf2
jmp 0x4440
movq %rdx, %rbx
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x7b5a
leaq 0x32c(%rsp), %rdi
callq 0x5c4c
movq %r14, %rdi
cmpl $0x1, %ebx
jne 0x447d
callq 0x30c0
callq 0x3390
xorl %eax, %eax
addq $0x3f8, %rsp            # imm = 0x3F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x33d0