-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathdatetimecontroller.cpp
More file actions
128 lines (107 loc) · 4.37 KB
/
Copy pathdatetimecontroller.cpp
File metadata and controls
128 lines (107 loc) · 4.37 KB
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
#include "datetimecontroller.hpp"
#include "appconfig.hpp"
#include "plugins/plugincontextprovider.hpp"
#include <cctype>
#include <ctime>
#include <eepp/scene/scenemanager.hpp>
#include <eepp/ui/tools/uicodeeditorsplitter.hpp>
#include <eepp/ui/uicodeeditor.hpp>
#include <eepp/ui/uimessagebox.hpp>
#include <eepp/ui/uitextinput.hpp>
#include <string_view>
namespace ecode {
DateTimeController::DateTimeController( PluginContextProvider* context ) : mContext( context ) {}
void DateTimeController::registerCommands( TextDocument& doc ) {
doc.setCommand( "insert-date-dd-mm-yyyy", [this] { insertDate( "%d.%m.%Y" ); } );
doc.setCommand( "insert-date-mm-dd-yyyy", [this] { insertDate( "%m.%d.%Y" ); } );
doc.setCommand( "insert-date-yyyy-mm-dd", [this] { insertDate( "%Y/%m/%d" ); } );
doc.setCommand( "insert-date-time-dd-mm-yyyy", [this] { insertDate( "%d.%m.%Y %H:%M:%S" ); } );
doc.setCommand( "insert-date-time-mm-dd-yyyy", [this] { insertDate( "%m.%d.%Y %H:%M:%S" ); } );
doc.setCommand( "insert-date-time-yyyy-mm-dd", [this] { insertDate( "%Y/%m/%d %H:%M:%S" ); } );
doc.setCommand( "insert-date-custom",
[this] { insertDate( mContext->getConfig().editor.customDateFormat ); } );
doc.setCommand( "set-custom-date-format", [this] { setCustomDateFormat(); } );
}
bool DateTimeController::isValidDateFormat( const std::string& format ) {
if ( String::trim( format ).empty() )
return false;
for ( size_t i = 0; i < format.size(); ++i ) {
if ( format[i] != '%' )
continue;
if ( ++i >= format.size() )
return false;
while ( i < format.size() && ( format[i] == '_' || format[i] == '-' || format[i] == '0' ||
format[i] == '^' || format[i] == '#' ) )
++i;
while ( i < format.size() && std::isdigit( static_cast<unsigned char>( format[i] ) ) )
++i;
if ( i < format.size() && ( format[i] == 'E' || format[i] == 'O' ) )
++i;
if ( i >= format.size() )
return false;
static constexpr std::string_view VALID_SPECIFIERS =
"aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%";
if ( VALID_SPECIFIERS.find( format[i] ) == std::string_view::npos )
return false;
}
return true;
}
std::string DateTimeController::formatCurrentDate( const std::string& format ) {
std::time_t rawtime;
std::time( &rawtime );
std::tm* timeinfo = std::localtime( &rawtime );
if ( nullptr == timeinfo )
return {};
char buf[256];
if ( std::strftime( buf, sizeof( buf ), format.c_str(), timeinfo ) == 0 )
return {};
return std::string( buf );
}
void DateTimeController::insertDate( const std::string& format ) {
if ( !mContext->getSplitter()->curEditorExistsAndFocused() )
return;
UICodeEditor* editor = mContext->getSplitter()->getCurEditor();
if ( nullptr == editor || editor->isLocked() )
return;
if ( !isValidDateFormat( format ) )
return;
std::string date( formatCurrentDate( format ) );
if ( !date.empty() )
editor->getDocument().textInput( String::fromUtf8( date ) );
}
void DateTimeController::setCustomDateFormat() {
UIMessageBox* msgBox =
UIMessageBox::New( UIMessageBox::INPUT,
mContext
->i18n( "set_custom_date_format_message",
"Set Custom Date Format:\nUses strftime format specifiers." )
.unescape() );
msgBox->setTitle( mContext->i18n( "set_custom_date_format", "Set Custom Date Format" ) );
msgBox->setCloseShortcut( { KEY_ESCAPE, 0 } );
msgBox->getTextInput()->setText( mContext->getConfig().editor.customDateFormat );
msgBox->getTextInput()->on( Event::OnTextChanged, [msgBox]( const Event* ) {
msgBox->getTextInput()->removeClass( "error" );
} );
msgBox->showWhenReady();
msgBox->on( Event::OnConfirm, [this, msgBox]( const Event* ) {
std::string format( msgBox->getTextInput()->getText().toUtf8() );
if ( !isValidDateFormat( format ) || formatCurrentDate( format ).empty() ) {
msgBox->getTextInput()->addClass( "error" );
mContext->errorMsgBox(
mContext->i18n( "invalid_date_format", "Invalid date format." ) );
return;
}
mContext->getConfig().editor.customDateFormat = std::move( format );
msgBox->closeWindow();
} );
setFocusEditorOnClose( msgBox );
}
void DateTimeController::setFocusEditorOnClose( EE::UI::UIMessageBox* msgBox ) {
UICodeEditor* editor = mContext->getSplitter()->getCurEditor();
msgBox->on( Event::OnWindowClose, [editor]( const Event* ) {
if ( editor && SceneManager::existsSingleton() &&
!SceneManager::instance()->isShuttingDown() )
editor->setFocus();
} );
}
} // namespace ecode