From 45c4c5f09a60bbd76a95289eb6faf57d6d8c284f Mon Sep 17 00:00:00 2001 From: h7x4 Date: Fri, 16 May 2025 18:47:26 +0200 Subject: [PATCH] lib/cli/query-word: stringify --- lib/cli/commands/query_word.dart | 8 ++++---- lib/models/word_search/word_search_result.dart | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/cli/commands/query_word.dart b/lib/cli/commands/query_word.dart index b5a39b1..d019ab3 100644 --- a/lib/cli/commands/query_word.dart +++ b/lib/cli/commands/query_word.dart @@ -1,5 +1,3 @@ - -import 'dart:convert'; import 'dart:io'; import 'package:jadb/_data_ingestion/open_local_db.dart'; @@ -38,8 +36,10 @@ class QueryWord extends Command { } else if (result.isEmpty) { print("No matches"); } else { - print(JsonEncoder.withIndent(' ') - .convert(result.map((e) => e.toJson()).toList())); + for (final e in result) { + print(e.toString()); + print(""); + } } print("Query took ${time.elapsedMilliseconds}ms"); diff --git a/lib/models/word_search/word_search_result.dart b/lib/models/word_search/word_search_result.dart index dcaeb50..1d74e78 100644 --- a/lib/models/word_search/word_search_result.dart +++ b/lib/models/word_search/word_search_result.dart @@ -80,4 +80,21 @@ class WordSearchResult { jlptLevel: JlptLevel.fromJson(json['jlptLevel'] as Object?), sources: WordSearchSources.fromJson(json['sources']), ); + + String _formatJapaneseWord(WordSearchRuby word) => + word.furigana == null ? word.base : "${word.base} (${word.furigana})"; + + @override + String toString() { + final japaneseWord = _formatJapaneseWord(japanese[0]); + final isCommonString = isCommon ? '(C)' : ''; + final jlptLevelString = "(${jlptLevel.toString()})"; + + return ''' +${score} | [$entryId] $japaneseWord $isCommonString $jlptLevelString + Other forms: ${japanese.skip(1).map(_formatJapaneseWord).join(', ')} + Senses: ${senses.map((s) => s.englishDefinitions).join(', ')} + ''' + .trim(); + } }