Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git
src/main/java/com/keenwrite/MainPane.java
if( inputFile.isFile() ) {
getRecentFiles().add( inputFile.getAbsolutePath() );
+
+ final var dir = inputFile.getParentFile();
+ mWorkspace.fileProperty( KEY_UI_RECENT_DIR ).setValue( dir );
}
}
src/main/java/com/keenwrite/constants/Constants.java
import static com.keenwrite.Bootstrap.USER_DATA_DIR;
import static com.keenwrite.io.SysFile.toFile;
+import static com.keenwrite.io.UserDataDir.getUserHome;
import static com.keenwrite.preferences.LocaleScripts.withScript;
import static com.keenwrite.util.SystemUtils.*;
public static File getFontDirectory() {
final var FONT_PATH = Path.of( "fonts" );
- final var USER_HOME = System.getProperty( "user.home" );
+ final var USER_HOME = getUserHome();
final String fontBase;
src/main/java/com/keenwrite/editors/TextResource.java
void clearModifiedProperty();
- private String asString( final byte[] text, final Charset encoding ) {
- return new String( text, encoding );
- }
/**
detector.dataEnd();
- final var charset = detector.getDetectedCharset();
+ final var detectedCharset = detector.getDetectedCharset();
- return charset == null
- ? DEFAULT_CHARSET
- : forName( charset.toUpperCase( ENGLISH ) );
+ // TODO: Revert when the issue has been fixed.
+ // https://github.com/albfernandez/juniversalchardet/issues/35
+ return switch( detectedCharset ) {
+ case null -> DEFAULT_CHARSET;
+ case "US-ASCII", "TIS620" -> DEFAULT_CHARSET;
+ default -> forName( detectedCharset.toUpperCase( ENGLISH ) );
+ };
}
default boolean supports( final MediaType mediaType ) {
return isMediaType( mediaType );
+ }
+
+ private static String asString( final byte[] text, final Charset encoding ) {
+ return new String( text, encoding );
}
}
src/main/java/com/keenwrite/editors/markdown/MarkdownEditor.java
public final class MarkdownEditor extends BorderPane implements TextEditor {
/**
- * Represents a failed index search.
- */
- private static final int INDEX_NOT_FOUND = -1;
-
- /**
* Regular expression that matches the type of markup block. This is used
* when Enter is pressed to continue the block environment.
src/main/java/com/keenwrite/io/UserDataDir.java
private UserDataDir() { }
+ public static String getUserHome() {
+ return PROP_USER_HOME;
+ }
+
/**
* Makes a valiant attempt at determining where to create application-specific
src/main/java/com/keenwrite/ui/explorer/FilePickerFactory.java
import static com.keenwrite.io.SysFile.toFile;
+import static com.keenwrite.io.UserDataDir.getUserHome;
import static com.keenwrite.preferences.AppKeys.KEY_UI_RECENT_DIR;
import static com.keenwrite.ui.explorer.FilePickerFactory.SelectionType.*;
mChooser.setInitialDirectory(
- file.exists() ? file : new File( System.getProperty( "user.home" ) )
+ file.exists() ? file : new File( getUserHome() )
);
}
src/test/java/com/keenwrite/encoding/EncodingTest.java
+package com.keenwrite.encoding;
+
+import org.junit.jupiter.api.Test;
+import org.mozilla.universalchardet.UniversalDetector;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class EncodingTest {
+ @Test
+ public void test_Encoding_UTF8_UTF8() {
+ final var bytes = testBytes();
+
+ final var detector = new UniversalDetector( null );
+ detector.handleData( bytes, 0, bytes.length );
+ detector.dataEnd();
+
+ final var expectedCharset = StandardCharsets.UTF_8;
+ final var detectedCharset = detector.getDetectedCharset();
+
+ assertNotNull( detectedCharset );
+
+ final var actualCharset = Charset.forName( detectedCharset );
+
+ assertEquals( expectedCharset, actualCharset );
+ }
+
+ private static byte[] testBytes() {
+ return
+ """
+ One humid afternoon during the harrowing heatwave of 2060, Renato
+ Salvatierra, a man with blood sausage fingers and a footfall that
+ silenced rooms, received a box at his police station. Taped to the
+ box was a ransom note; within were his wife's eyes. By year's end,
+ a supermax prison overflowed with felons, owing to Salvatierra's
+ efforts to find his beloved. Soon after, he flipped profession into
+ an entry-level land management position that, his wife insisted,
+ would be, in her words, *infinitamente más relajante*---infinitely
+ more relaxing.
+ """
+ .getBytes();
+ }
+}

Remembers last directory, fixes encoding for small texts

Author DaveJarvis <email>
Date 2024-04-07 14:44:11 GMT-0700
Commit 900fae9000ab7bf3809e48cc7075e144edf54360
Parent ea09a4f
Delta 69 lines added, 14 lines removed, 55-line increase