// File: /workspace/github_cpp_makefile_star2_O3/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 0x5f1e
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 0x5f88
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
movapd %xmm0, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
mulpd 0x5d9c(%rip), %xmm1     # 0xa010
cvttpd2dq %xmm1, %xmm2
movapd %xmm2, %xmm3
psrad $0x1f, %xmm3
addpd 0x5d97(%rip), %xmm1     # 0xa020
cvttpd2dq %xmm1, %xmm1
andpd %xmm3, %xmm1
orpd %xmm2, %xmm1
mulsd 0x5de3(%rip), %xmm0     # 0xa080
cvttsd2si %xmm0, %rax
movlpd %xmm1, 0x44(%rdi)
movl %eax, 0x4c(%rdi)
decl %ebp
movl %ebp, 0x8(%rdi)
leaq 0x1c0(%rsp), %rbx
movq %rbx, %rsi
callq 0x4084
leaq 0x1a0(%rsp), %r14
movq %r14, %rdi
callq 0x60c0
movq %r14, %rdi
callq 0x4b06
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 0x4b5e
leaq 0x2a0(%rsp), %r15
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4bcc
leaq 0x180(%rsp), %rdx
movq %r12, (%rdx)
movq %r14, 0x8(%rdx)
leaq 0xb98(%rip), %rax       # 0x4ee0
movq %rax, 0x18(%rdx)
leaq 0xc65(%rip), %rax       # 0x4fb8
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x3a, %esi
callq 0x6164
movq 0x190(%rsp), %rax
testq %rax, %rax
je 0x4385
leaq 0x180(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
xorpd %xmm0, %xmm0
leaq 0x100(%rsp), %rdx
movapd %xmm0, (%rdx)
leaq 0xc44(%rip), %rax       # 0x4fe0
movq %rax, 0x18(%rdx)
leaq 0xc55(%rip), %rax       # 0x4ffc
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x71, %esi
callq 0x6164
movq 0x110(%rsp), %rax
testq %rax, %rax
je 0x43d9
leaq 0x100(%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 0xbfe(%rip), %rax       # 0x5014
movq %rax, 0x18(%rdx)
leaq 0xc7b(%rip), %rax       # 0x509c
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x6a, %esi
callq 0x6164
movq 0xd0(%rsp), %rax
testq %rax, %rax
je 0x4453
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 0xc70(%rip), %rax       # 0x5100
movq %rax, 0x18(%rdx)
leaq 0xced(%rip), %rax       # 0x5188
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x6b, %esi
callq 0x6164
movq 0xb0(%rsp), %rax
testq %rax, %rax
je 0x44cd
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 0xce2(%rip), %rax       # 0x51ec
movq %rax, 0x18(%rdx)
leaq 0xd5f(%rip), %rax       # 0x5274
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x68, %esi
callq 0x6164
movq 0x90(%rsp), %rax
testq %rax, %rax
je 0x4547
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 0xd5d(%rip), %rax       # 0x52d8
movq %rax, 0x18(%rdx)
leaq 0xdda(%rip), %rax       # 0x5360
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x6c, %esi
callq 0x6164
movq 0x70(%rsp), %rax
testq %rax, %rax
je 0x45b2
leaq 0x60(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0x160(%rsp), %rdx
movq $0x0, 0x8(%rdx)
movq %rbx, (%rdx)
leaq 0xdf8(%rip), %rax       # 0x53c4
movq %rax, 0x18(%rdx)
leaq 0xfd7(%rip), %rax       # 0x55ae
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x73, %esi
callq 0x6164
movq 0x170(%rsp), %rax
testq %rax, %rax
je 0x4609
leaq 0x160(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0x140(%rsp), %rdx
movq $0x0, 0x8(%rdx)
movq %rbx, (%rdx)
leaq 0xfb1(%rip), %rax       # 0x55d4
movq %rax, 0x18(%rdx)
leaq 0x1174(%rip), %rax      # 0x57a2
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x6f, %esi
callq 0x6164
movq 0x150(%rsp), %rax
testq %rax, %rax
je 0x4660
leaq 0x140(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x4c28
leaq 0x120(%rsp), %rdx
movq $0x0, 0x8(%rdx)
movq %rbx, (%rdx)
leaq 0x1337(%rip), %rax      # 0x59bc
movq %rax, 0x18(%rdx)
leaq 0x14e2(%rip), %rax      # 0x5b72
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x65, %esi
callq 0x6164
movq 0x130(%rsp), %rax
testq %rax, %rax
je 0x46c2
leaq 0x120(%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 0x14a2(%rip), %rax      # 0x5b98
movq %rax, 0x18(%rdx)
leaq 0x1521(%rip), %rax      # 0x5c22
movq %rax, 0x10(%rdx)
leaq 0x8(%rsp), %rdi
movl $0x64, %esi
callq 0x6164
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0x472d
leaq 0x40(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0xe0(%rsp), %rdi
leaq 0x2a0(%rsp), %rsi
callq 0x61cc
leaq 0x1a0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x618e
leaq 0xf0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4778
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x3200
leaq 0x8(%rsp), %rbx
leaq 0x308(%rsp), %r14
movq %rbx, %rdi
movl $0x3a, %esi
callq 0x6b14
movq %r14, %rdi
movl $0x1b, %esi
callq 0x6bb0
jmp 0x4785
jmp 0x4989
movq %rdx, %rbx
movq %rax, %r14
leaq 0xf0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x49e7
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x49e7
jmp 0x4991
jmp 0x4989
movq %rdx, %rbx
movq %rax, %r14
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0x49e7
leaq 0x40(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x49e7
jmp 0x4991
jmp 0x4991
jmp 0x4989
jmp 0x4991
movq %rdx, %rbx
movq %rax, %r14
movq 0x130(%rsp), %rax
testq %rax, %rax
je 0x49e7
leaq 0x120(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x49e7
jmp 0x4991
jmp 0x4991
movq %rdx, %rbx
movq %rax, %r14
movq 0x150(%rsp), %rax
testq %rax, %rax
je 0x49e7
leaq 0x140(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x49e7
jmp 0x4991
jmp 0x4991
movq %rdx, %rbx
movq %rax, %r14
movq 0x170(%rsp), %rax
testq %rax, %rax
je 0x49e7
leaq 0x160(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x49e7
jmp 0x4991
movq %rdx, %rbx
movq %rax, %r14
movq 0x70(%rsp), %rax
testq %rax, %rax
je 0x49e7
leaq 0x60(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x49e7
jmp 0x4991
jmp 0x4991
jmp 0x4989
movq %rdx, %rbx
movq %rax, %r14
movq 0x90(%rsp), %rax
testq %rax, %rax
je 0x49e7
leaq 0x80(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x49e7
jmp 0x4991
jmp 0x4991
jmp 0x4989
movq %rdx, %rbx
movq %rax, %r14
movq 0xb0(%rsp), %rax
testq %rax, %rax
je 0x49e7
leaq 0xa0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x49e7
jmp 0x4991
jmp 0x4991
jmp 0x4989
movq %rdx, %rbx
movq %rax, %r14
movq 0xd0(%rsp), %rax
testq %rax, %rax
je 0x49e7
leaq 0xc0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x49e7
jmp 0x4991
jmp 0x4991
movq %rdx, %rbx
movq %rax, %r14
jmp 0x49e7
movq %rax, %rdi
callq 0x6ee9
movq %rdx, %rbx
movq %rax, %r14
movq 0x110(%rsp), %rax
testq %rax, %rax
je 0x49e7
leaq 0x100(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x49e7
jmp 0x4991
movq %rdx, %rbx
movq %rax, %r14
movq 0x190(%rsp), %rax
testq %rax, %rax
je 0x49e7
leaq 0x180(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0x308(%rsp), %rdi
callq 0x6d66
leaq 0x1a0(%rsp), %rdi
callq 0x6dbe
jmp 0x4a0d
jmp 0x4991
jmp 0x4a07
movq %rdx, %rbx
movq %rax, %r14
leaq 0x2b0(%rsp), %r15
leaq 0x2f0(%rsp), %rdi
callq 0x8df0
leaq 0x2c8(%rsp), %rdi
callq 0x5e12
movq %r15, %rdi
callq 0x5e12
movq 0x290(%rsp), %rax
testq %rax, %rax
je 0x4a56
leaq 0x280(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0x268(%rsp), %rdi
callq 0x5e12
leaq 0x250(%rsp), %rdi
callq 0x5e12
leaq 0x238(%rsp), %rdi
callq 0x5e12
leaq 0x220(%rsp), %rdi
callq 0x5e12
leaq 0x208(%rsp), %rdi
callq 0x5e12
leaq 0x1f0(%rsp), %rdi
callq 0x5e12
leaq 0x1c8(%rsp), %rdi
callq 0x751e
jmp 0x4abe
jmp 0x4991
movq %rdx, %rbx
movq %rax, %r14
leaq 0x8(%rsp), %rdi
callq 0x8e38
leaq 0x374(%rsp), %rdx
xorl %edi, %edi
movl $0x2, %esi
callq 0x3280
movq %r14, %rdi
cmpl $0x1, %ebx
jne 0x4b00
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
nop