@@ -53,6 +53,36 @@ export function showCursor(term: Terminal) {
5353
5454export const systemMessageColor = chalk . blue . bold . italic ;
5555
56+ function computeTerminalTheme ( ) {
57+ const fromCSS = ( varName : string ) =>
58+ window . getComputedStyle ( document . body ) . getPropertyValue ( varName ) ;
59+ // "--color-" + color_name のように文字列を分割するとTailwindCSSが認識せずCSSの値として出力されない場合があるので注意
60+ return {
61+ // DaisyUIの変数を使用してテーマを設定している
62+ background : fromCSS ( "--color-base-300" ) ,
63+ foreground : fromCSS ( "--color-base-content" ) ,
64+ cursor : fromCSS ( "--color-base-content" ) ,
65+ selectionBackground : fromCSS ( "--color-primary" ) ,
66+ selectionForeground : fromCSS ( "--color-primary-content" ) ,
67+ black : fromCSS ( "--color-black" ) ,
68+ red : fromCSS ( "--color-red-600" ) ,
69+ green : fromCSS ( "--color-green-700" ) ,
70+ yellow : fromCSS ( "--color-yellow-700" ) ,
71+ blue : fromCSS ( "--color-indigo-600" ) ,
72+ magenta : fromCSS ( "--color-fuchsia-500" ) ,
73+ cyan : fromCSS ( "--color-cyan-600" ) ,
74+ white : fromCSS ( "--color-neutral-100" ) ,
75+ brightBlack : fromCSS ( "--color-neutral-500" ) ,
76+ brightRed : fromCSS ( "--color-red-400" ) ,
77+ brightGreen : fromCSS ( "--color-green-500" ) ,
78+ brightYellow : fromCSS ( "--color-yellow-500" ) ,
79+ brightBlue : fromCSS ( "--color-indigo-400" ) ,
80+ brightMagenta : fromCSS ( "--color-fuchsia-300" ) ,
81+ brightCyan : fromCSS ( "--color-cyan-400" ) ,
82+ brightWhite : fromCSS ( "--color-white" ) ,
83+ } as const ;
84+ }
85+
5686interface TerminalProps {
5787 getRows ?: ( cols : number ) => number ;
5888 onReady ?: ( ) => void ;
@@ -83,9 +113,6 @@ export function useTerminal(props: TerminalProps) {
83113 document . fonts . load ( "1rem Inconsolata Variable" ) ,
84114 ] ) . then ( ( [ { Terminal } , { FitAddon } ] ) => {
85115 if ( ! abortController . signal . aborted ) {
86- const fromCSS = ( varName : string ) =>
87- window . getComputedStyle ( document . body ) . getPropertyValue ( varName ) ;
88- // "--color-" + color_name のように文字列を分割するとTailwindCSSが認識せずCSSの値として出力されない場合があるので注意
89116 const term = new Terminal ( {
90117 cursorBlink : true ,
91118 convertEol : true ,
@@ -98,31 +125,7 @@ export function useTerminal(props: TerminalProps) {
98125 letterSpacing : 0 ,
99126 fontFamily :
100127 "'Inconsolata Variable', 'Noto Sans JP', 'Noto Sans CJK JP', 'Source Han Sans JP', '源ノ角ゴシック', 'Noto Sans JP Variable', monospace" ,
101- theme : {
102- // DaisyUIの変数を使用してテーマを設定している
103- // TODO: ダークテーマ/ライトテーマを切り替えたときに再設定する?
104- background : fromCSS ( "--color-base-300" ) ,
105- foreground : fromCSS ( "--color-base-content" ) ,
106- cursor : fromCSS ( "--color-base-content" ) ,
107- selectionBackground : fromCSS ( "--color-primary" ) ,
108- selectionForeground : fromCSS ( "--color-primary-content" ) ,
109- black : fromCSS ( "--color-black" ) ,
110- brightBlack : fromCSS ( "--color-neutral-500" ) ,
111- red : fromCSS ( "--color-red-600" ) ,
112- brightRed : fromCSS ( "--color-red-400" ) ,
113- green : fromCSS ( "--color-green-600" ) ,
114- brightGreen : fromCSS ( "--color-green-400" ) ,
115- yellow : fromCSS ( "--color-yellow-700" ) ,
116- brightYellow : fromCSS ( "--color-yellow-400" ) ,
117- blue : fromCSS ( "--color-indigo-600" ) ,
118- brightBlue : fromCSS ( "--color-indigo-400" ) ,
119- magenta : fromCSS ( "--color-fuchsia-600" ) ,
120- brightMagenta : fromCSS ( "--color-fuchsia-400" ) ,
121- cyan : fromCSS ( "--color-cyan-600" ) ,
122- brightCyan : fromCSS ( "--color-cyan-400" ) ,
123- white : fromCSS ( "--color-base-100" ) ,
124- brightWhite : fromCSS ( "--color-white" ) ,
125- } ,
128+ theme : computeTerminalTheme ( ) ,
126129 } ) ;
127130 terminalInstanceRef . current = term ;
128131
@@ -185,14 +188,8 @@ export function useTerminal(props: TerminalProps) {
185188 // テーマが変わったときにterminalのテーマを更新する
186189 useEffect ( ( ) => {
187190 if ( terminalInstanceRef . current ) {
188- const fromCSS = ( varName : string ) =>
189- window . getComputedStyle ( document . body ) . getPropertyValue ( varName ) ;
190-
191191 terminalInstanceRef . current . options = {
192- theme : {
193- background : fromCSS ( "--color-base-300" ) ,
194- foreground : fromCSS ( "--color-base-content" ) ,
195- } ,
192+ theme : computeTerminalTheme ( ) ,
196193 } ;
197194 }
198195 } , [ theme ] ) ;
0 commit comments