2020-01-18 05:38:21 -03:00
/*
2021-05-01 13:26:44 -03:00
* Copyright ( c ) 2018 - 2021 , Andreas Kling < kling @ serenityos . org >
2020-01-18 05:38:21 -03:00
*
2021-04-22 05:24:48 -03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-01-18 05:38:21 -03:00
*/
2021-03-06 04:13:22 -03:00
# include "FileArgument.h"
2021-05-01 13:26:44 -03:00
# include "MainWidget.h"
2020-07-06 11:18:49 -03:00
# include <LibCore/ArgsParser.h>
2021-04-13 11:18:20 -03:00
# include <LibGUI/Menubar.h>
2020-01-11 19:50:05 -03:00
# include <stdio.h>
2021-03-12 13:29:37 -03:00
# include <unistd.h>
2019-05-15 21:47:28 -03:00
2021-05-01 13:26:44 -03:00
using namespace TextEditor ;
2019-03-06 20:31:06 -03:00
int main ( int argc , char * * argv )
{
2021-01-17 04:18:11 -03:00
if ( pledge ( " stdio recvfd sendfd thread rpath accept cpath wpath unix fattr " , nullptr ) < 0 ) {
2020-01-11 19:50:05 -03:00
perror ( " pledge " ) ;
return 1 ;
}
2020-07-04 09:05:19 -03:00
auto app = GUI : : Application : : construct ( argc , argv ) ;
2019-03-06 20:31:06 -03:00
2021-01-17 04:18:11 -03:00
if ( pledge ( " stdio recvfd sendfd thread rpath accept cpath wpath unix " , nullptr ) < 0 ) {
2020-01-11 19:50:05 -03:00
perror ( " pledge " ) ;
return 1 ;
}
2020-07-06 11:18:49 -03:00
const char * preview_mode = " auto " ;
const char * file_to_edit = nullptr ;
Core : : ArgsParser parser ;
parser . add_option ( preview_mode , " Preview mode, one of 'none', 'html', 'markdown', 'auto' " , " preview-mode " , ' \0 ' , " mode " ) ;
2021-03-06 04:13:22 -03:00
parser . add_positional_argument ( file_to_edit , " File to edit, with optional starting line and column number " , " file[:line[:column]] " , Core : : ArgsParser : : Required : : No ) ;
2020-07-06 11:18:49 -03:00
parser . parse ( argc , argv ) ;
StringView preview_mode_view = preview_mode ;
2020-11-02 16:30:17 -03:00
auto app_icon = GUI : : Icon : : default_icon ( " app-text-editor " ) ;
2020-02-02 11:07:41 -03:00
auto window = GUI : : Window : : construct ( ) ;
2020-07-31 19:12:11 -03:00
window - > resize ( 640 , 400 ) ;
2019-04-13 11:59:55 -03:00
2021-05-01 13:26:44 -03:00
auto & text_widget = window - > set_main_widget < MainWidget > ( ) ;
2019-07-11 15:52:33 -03:00
2020-03-04 05:46:23 -03:00
text_widget . editor ( ) . set_focus ( true ) ;
2019-11-30 11:34:08 -03:00
2020-02-02 11:07:41 -03:00
window - > on_close_request = [ & ] ( ) - > GUI : : Window : : CloseRequestDecision {
2020-03-04 05:46:23 -03:00
if ( text_widget . request_close ( ) )
2020-02-02 11:07:41 -03:00
return GUI : : Window : : CloseRequestDecision : : Close ;
return GUI : : Window : : CloseRequestDecision : : StayOpen ;
2019-08-27 15:37:41 -03:00
} ;
2020-07-06 11:18:49 -03:00
if ( preview_mode_view = = " auto " ) {
text_widget . set_auto_detect_preview_mode ( true ) ;
} else if ( preview_mode_view = = " markdown " ) {
2021-05-01 13:26:44 -03:00
text_widget . set_preview_mode ( MainWidget : : PreviewMode : : Markdown ) ;
2020-07-06 11:18:49 -03:00
} else if ( preview_mode_view = = " html " ) {
2021-05-01 13:26:44 -03:00
text_widget . set_preview_mode ( MainWidget : : PreviewMode : : HTML ) ;
2020-07-06 11:18:49 -03:00
} else if ( preview_mode_view = = " none " ) {
2021-05-01 13:26:44 -03:00
text_widget . set_preview_mode ( MainWidget : : PreviewMode : : None ) ;
2020-07-06 11:18:49 -03:00
} else {
2020-10-06 14:25:44 -03:00
warnln ( " Invalid mode '{}' " , preview_mode ) ;
2020-07-06 11:18:49 -03:00
return 1 ;
}
2021-04-13 11:18:20 -03:00
auto menubar = GUI : : Menubar : : construct ( ) ;
2021-02-27 07:10:37 -03:00
text_widget . initialize_menubar ( menubar ) ;
2021-03-25 17:41:39 -03:00
window - > set_menubar ( menubar ) ;
2021-02-27 07:10:37 -03:00
2021-05-01 13:45:20 -03:00
if ( file_to_edit ) {
2021-03-06 04:13:22 -03:00
// A file name was passed, parse any possible line and column numbers included.
FileArgument parsed_argument ( file_to_edit ) ;
2021-05-06 07:32:50 -03:00
if ( ! text_widget . open_file ( parsed_argument . filename ( ) ) )
2021-03-18 19:07:59 -03:00
return 1 ;
2021-03-06 04:13:22 -03:00
text_widget . editor ( ) . set_cursor_and_focus_line ( parsed_argument . line ( ) . value_or ( 1 ) - 1 , parsed_argument . column ( ) . value_or ( 0 ) ) ;
2021-05-01 13:45:20 -03:00
}
2021-03-18 19:07:59 -03:00
text_widget . update_title ( ) ;
2019-07-11 15:52:33 -03:00
2019-07-15 17:58:37 -03:00
window - > show ( ) ;
2020-11-02 16:30:17 -03:00
window - > set_icon ( app_icon . bitmap_for_size ( 16 ) ) ;
2019-07-15 17:58:37 -03:00
2021-03-25 17:41:39 -03:00
window - > set_menubar ( menubar ) ;
2020-07-04 09:05:19 -03:00
return app - > exec ( ) ;
2019-07-23 13:20:00 -03:00
}