// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x3b0, %rsp            # imm = 0x3B0
leaq 0x374(%rsp), %rdi
callq 0x5fb4
leaq 0x1c0(%rsp), %rbx
movl $0x5413, %esi           # imm = 0x5413
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x32b0
movzwl 0x2(%rbx), %r14d
leaq 0x1c0(%rsp), %rbx
movl $0x5413, %esi           # imm = 0x5413
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x32b0
movzwl (%rbx), %ebp
leaq 0x38(%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 0x1c0(%rsp), %rdi
callq 0x6014
movabsq $0x200000014, %rax      # imm = 0x200000014
leaq 0x2a0(%rsp), %rdi
movq %rax, (%rdi)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rdi)
movupd %xmm0, 0x20(%rdi)
movupd %xmm0, 0x30(%rdi)
movl $0x1, 0x40(%rdi)
movupd %xmm0, 0x50(%rdi)
movq $0x0, 0x60(%rdi)
addl $-0x6, %r14d
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd 0x5d8f(%rip), %xmm1     # 0xa008
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movl %eax, 0x44(%rdi)
movsd 0x5d83(%rip), %xmm1     # 0xa010
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
mulsd 0x5d7a(%rip), %xmm0     # 0xa018
cvttsd2si %xmm0, %rcx
movl %eax, 0x48(%rdi)
movl %ecx, 0x4c(%rdi)
decl %ebp
movl %ebp, 0x8(%rdi)
leaq 0x1c0(%rsp), %rbx
movq %rbx, %rsi
callq 0x4091
leaq 0x1a0(%rsp), %r14
movq %r14, %rdi
callq 0x614c
movq %r14, %rdi
callq 0x4b20
leaq 0x320(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rax)
movupd %xmm0, 0x20(%rax)
movupd %xmm0, 0x30(%rax)
movupd %xmm0, 0x40(%rax)
movl %ebp, -0x18(%rax)
leaq 0x308(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x4b86
leaq 0x2a0(%rsp), %r15
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4c00
leaq 0x180(%rsp), %rdx
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdx)
movapd %xmm0, 0x10(%rdx)
movq %r12, (%rdx)
movq %r14, 0x8(%rdx)
leaq 0xbe4(%rip), %rax       # 0x4f38
movq %rax, 0x18(%rdx)
leaq 0xcb1(%rip), %rax       # 0x5010
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x3a, %esi
callq 0x61f0
movq 0x190(%rsp), %rax
testq %rax, %rax
je 0x4391
leaq 0x180(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
xorpd %xmm0, %xmm0
leaq 0x160(%rsp), %rdx
movapd %xmm0, (%rdx)
leaq 0xc90(%rip), %rax       # 0x5038
movq %rax, 0x18(%rdx)
leaq 0xca1(%rip), %rax       # 0x5054
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x71, %esi
callq 0x61f0
movq 0x170(%rsp), %rax
testq %rax, %rax
je 0x43e5
leaq 0x160(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
xorpd %xmm0, %xmm0
movapd %xmm0, 0xd0(%rsp)
movapd %xmm0, 0xc0(%rsp)
movl $0x18, %edi
callq 0x31f0
movq %rbx, (%rax)
movq %r15, 0x8(%rax)
movq %r14, 0x10(%rax)
leaq 0xc0(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xc4a(%rip), %rax       # 0x506c
movq %rax, 0x18(%rdx)
leaq 0xcc7(%rip), %rax       # 0x50f4
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x6a, %esi
callq 0x61f0
movq 0xd0(%rsp), %rax
testq %rax, %rax
je 0x445f
leaq 0xc0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
xorpd %xmm0, %xmm0
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0xa0(%rsp)
movl $0x18, %edi
callq 0x31f0
movq %rbx, (%rax)
movq %r15, 0x8(%rax)
movq %r14, 0x10(%rax)
leaq 0xa0(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xcc2(%rip), %rax       # 0x515e
movq %rax, 0x18(%rdx)
leaq 0xd3f(%rip), %rax       # 0x51e6
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x6b, %esi
callq 0x61f0
movq 0xb0(%rsp), %rax
testq %rax, %rax
je 0x44d9
leaq 0xa0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
movl $0x18, %edi
callq 0x31f0
movq %rbx, (%rax)
movq %r15, 0x8(%rax)
movq %r14, 0x10(%rax)
leaq 0x80(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xd3a(%rip), %rax       # 0x5250
movq %rax, 0x18(%rdx)
leaq 0xdb7(%rip), %rax       # 0x52d8
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x68, %esi
callq 0x61f0
movq 0x90(%rsp), %rax
testq %rax, %rax
je 0x4553
leaq 0x80(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movl $0x18, %edi
callq 0x31f0
movq %rbx, (%rax)
movq %r15, 0x8(%rax)
movq %r14, 0x10(%rax)
leaq 0x60(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xdbb(%rip), %rax       # 0x5342
movq %rax, 0x18(%rdx)
leaq 0xe38(%rip), %rax       # 0x53ca
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x6c, %esi
callq 0x61f0
movq 0x70(%rsp), %rax
testq %rax, %rax
je 0x45be
leaq 0x60(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
xorpd %xmm0, %xmm0
leaq 0x140(%rsp), %rdx
movapd %xmm0, (%rdx)
movapd %xmm0, 0x10(%rdx)
movq %rbx, (%rdx)
leaq 0xe57(%rip), %rax       # 0x5434
movq %rax, 0x18(%rdx)
leaq 0x1036(%rip), %rax      # 0x561e
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x73, %esi
callq 0x61f0
movq 0x150(%rsp), %rax
testq %rax, %rax
je 0x461a
leaq 0x140(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
xorpd %xmm0, %xmm0
leaq 0x120(%rsp), %rdx
movapd %xmm0, (%rdx)
movapd %xmm0, 0x10(%rdx)
movq %rbx, (%rdx)
leaq 0x100b(%rip), %rax      # 0x5644
movq %rax, 0x18(%rdx)
leaq 0x11ce(%rip), %rax      # 0x5812
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x6f, %esi
callq 0x61f0
movq 0x130(%rsp), %rax
testq %rax, %rax
je 0x4676
leaq 0x120(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x4c76
xorpd %xmm0, %xmm0
leaq 0x100(%rsp), %rdx
movapd %xmm0, (%rdx)
movapd %xmm0, 0x10(%rdx)
movq %rbx, (%rdx)
leaq 0x138c(%rip), %rax      # 0x5a2c
movq %rax, 0x18(%rdx)
leaq 0x1537(%rip), %rax      # 0x5be2
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x65, %esi
callq 0x61f0
movq 0x110(%rsp), %rax
testq %rax, %rax
je 0x46dd
leaq 0x100(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movl $0x18, %edi
callq 0x31f0
movq %rbx, (%rax)
movq %r15, 0x8(%rax)
movq %r14, 0x10(%rax)
leaq 0x40(%rsp), %rdx
movq %rax, (%rdx)
leaq 0x14f7(%rip), %rax      # 0x5c08
movq %rax, 0x18(%rdx)
leaq 0x1576(%rip), %rax      # 0x5c92
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x64, %esi
callq 0x61f0
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0x4748
leaq 0x40(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0xe0(%rsp), %rdi
leaq 0x2a0(%rsp), %rsi
callq 0x6258
leaq 0x1a0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x621a
leaq 0xf0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4793
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x3200
leaq 0x8(%rsp), %rbx
leaq 0x308(%rsp), %r14
movq %rbx, %rdi
movl $0x3a, %esi
callq 0x6bde
movq %r14, %rdi
movl $0x1b, %esi
callq 0x6c58
jmp 0x47a0
jmp 0x49a4
movq %rdx, %rbx
movq %rax, %r14
leaq 0xf0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4a02
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x4a02
jmp 0x49ac
jmp 0x49a4
movq %rdx, %rbx
movq %rax, %r14
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0x4a02
leaq 0x40(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x4a02
jmp 0x49ac
jmp 0x49ac
jmp 0x49a4
jmp 0x49ac
movq %rdx, %rbx
movq %rax, %r14
movq 0x110(%rsp), %rax
testq %rax, %rax
je 0x4a02
leaq 0x100(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x4a02
jmp 0x49ac
jmp 0x49ac
movq %rdx, %rbx
movq %rax, %r14
movq 0x130(%rsp), %rax
testq %rax, %rax
je 0x4a02
leaq 0x120(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x4a02
jmp 0x49ac
jmp 0x49ac
movq %rdx, %rbx
movq %rax, %r14
movq 0x150(%rsp), %rax
testq %rax, %rax
je 0x4a02
leaq 0x140(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x4a02
jmp 0x49ac
movq %rdx, %rbx
movq %rax, %r14
movq 0x70(%rsp), %rax
testq %rax, %rax
je 0x4a02
leaq 0x60(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x4a02
jmp 0x49ac
jmp 0x49ac
jmp 0x49a4
movq %rdx, %rbx
movq %rax, %r14
movq 0x90(%rsp), %rax
testq %rax, %rax
je 0x4a02
leaq 0x80(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x4a02
jmp 0x49ac
jmp 0x49ac
jmp 0x49a4
movq %rdx, %rbx
movq %rax, %r14
movq 0xb0(%rsp), %rax
testq %rax, %rax
je 0x4a02
leaq 0xa0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x4a02
jmp 0x49ac
jmp 0x49ac
jmp 0x49a4
movq %rdx, %rbx
movq %rax, %r14
movq 0xd0(%rsp), %rax
testq %rax, %rax
je 0x4a02
leaq 0xc0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x4a02
jmp 0x49ac
jmp 0x49ac
movq %rdx, %rbx
movq %rax, %r14
jmp 0x4a02
movq %rax, %rdi
callq 0x6f97
movq %rdx, %rbx
movq %rax, %r14
movq 0x170(%rsp), %rax
testq %rax, %rax
je 0x4a02
leaq 0x160(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x4a02
jmp 0x49ac
movq %rdx, %rbx
movq %rax, %r14
movq 0x190(%rsp), %rax
testq %rax, %rax
je 0x4a02
leaq 0x180(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0x308(%rsp), %rdi
callq 0x6e14
leaq 0x1a0(%rsp), %rdi
callq 0x6e6c
jmp 0x4a28
jmp 0x49ac
jmp 0x4a22
movq %rdx, %rbx
movq %rax, %r14
leaq 0x2b0(%rsp), %r15
leaq 0x2f0(%rsp), %rdi
callq 0x8f12
leaq 0x2c8(%rsp), %rdi
callq 0x5e86
movq %r15, %rdi
callq 0x5e86
movq 0x290(%rsp), %rax
testq %rax, %rax
je 0x4a71
leaq 0x280(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0x268(%rsp), %rdi
callq 0x5e86
leaq 0x250(%rsp), %rdi
callq 0x5e86
leaq 0x238(%rsp), %rdi
callq 0x5e86
leaq 0x220(%rsp), %rdi
callq 0x5e86
leaq 0x208(%rsp), %rdi
callq 0x5e86
leaq 0x1f0(%rsp), %rdi
callq 0x5e86
leaq 0x1c8(%rsp), %rdi
callq 0x75d6
jmp 0x4ad9
jmp 0x49ac
movq %rdx, %rbx
movq %rax, %r14
leaq 0x8(%rsp), %rdi
callq 0x8f54
leaq 0x374(%rsp), %rdx
xorl %edi, %edi
movl $0x2, %esi
callq 0x3280
movq %r14, %rdi
cmpl $0x1, %ebx
jne 0x4b1b
callq 0x30d0
callq 0x3370
xorl %eax, %eax
addq $0x3b0, %rsp            # imm = 0x3B0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x33b0